getdate function not working correctly

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mike
Forum Newbie
Posts: 8
Joined: Mon Jul 15, 2002 4:18 pm

getdate function not working correctly

Post 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???
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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); ?>">
mike
Forum Newbie
Posts: 8
Joined: Mon Jul 15, 2002 4:18 pm

brilliant

Post by mike »

worked a treat... thank you mate
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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?
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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.
Post Reply