Page 1 of 1

getdate function not working correctly

Posted: Tue Jul 16, 2002 5:48 pm
by mike
hi all- i am using this script; for getdate in my database-- it works as such but their is a small problem with it....

<input type=hidden name="date" value="<?php echo "$mday/$mon/$year $hours:$minutes"; ?>">

the problem with it is that when it shows the time it shows 21:1 rather than 21:01 or 19:4 instead of 19:04 and so on and so on (you get the picture) (obviously 21:45 shows as 21:45 just time that has 0 in between)

i tried to see if their wa sa different type for minutes than 'minutes' but it is the only one i think......
does anyone know why it would do this???

Posted: Tue Jul 16, 2002 6:17 pm
by gnu2php
Use printf(), with %02d for each number:

Code: Select all

<input type=hidden name="date" value="<?php printf('%02d/%02d/%02d %02d:%02d', $mday, $mon, $year, $hours, $minutes); ?>">

brilliant

Posted: Wed Jul 17, 2002 3:47 am
by mike
worked a treat... thank you mate

Posted: Wed Jul 17, 2002 12:18 pm
by fariquzeli
I was looking around for how to make a date print, this works great, but I was just wondering if you could explain what putting in %02d did? how does it work?

Posted: Wed Jul 17, 2002 1:08 pm
by gnu2php
The 0 is a flag, which means "pad with leading zeros." The number after the 0 (2 in this case) is the amount of padding you want to use. And finally, the d means that it is an integer.