Jump to content

date() i doesnt give leading zeros correct


BrainPill

Recommended Posts

I want a string input of minutes into minutes with leading zeros

I use date() for this

    <php 
    $time_in_min ='7';
	$minutes = date("i" ,$time_in_min)	;
	var_dump($minutes);
     		?>

but all I get is 00 while i according to the date() param explanation should give a leading zero. Like :07

How should this be solved?

 

 

Link to comment
Share on other sites

Try wrapping this in an if statement that tests for string length:

$var = 7; 
echo sprintf('%02d', $var); 

 

Edited by niche
  • Like 1
Link to comment
Share on other sites

If you use date() you have to provide a unix timestamp made up of seconds. 7 is seven seconds, therefore minutes indeed will show 00.

You need to give 7 min made up as seconds.

	<!DOCTYPE html>
<html>
<body>
	<?php
$d=60*7; //60sec x 7 = 7mins
echo "Created date is " . date("i", $d);
?>
	</body>
</html>
	

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...