Jump to content
BrainPill

date() i doesnt give leading zeros correct

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?

 

 

Share this post


Link to post
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

Share this post


Link to post
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>
	

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...