Forum Moderators: coopster

Message Too Old, No Replies

turn zero into 00

need it for time select boxes

         

hughie

10:37 am on Apr 4, 2006 (gmt 0)

10+ Year Member



A funny little problem i've had a few times, i've always gone for a work-around but i'm sure there is a simple morsel to solve the issue

I am using select boxes for people to choose the time from.

These are coming out from a simple loop, starting at 0 and finishing at 24:

----------
$count=0;
while($count<24)
{

$text.='<option value="'.$count.'">'.$count.'</option>';
$count+=1;
}
-----------

what i would like to do is display $count as 00 instead of just 0 at 12am and 01 instead of 1 at 1am and so on.

I'm sure there is a quick fix, i've just never found it...

Cheers.
hughie

omoutop

11:24 am on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Since you want to manipulate time check the date() function....
date("h") will give you 01-12
date("H") will give you 00-23

coopster

12:20 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sprintf [php.net] also allows you to format as you like.

hughie

1:11 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



sprintf was the key, many thanks!

$display_hours=sprintf("%02s", $hours);

cheers,
hughie