Forum Moderators: coopster

Message Too Old, No Replies

List to show time

         

sfast

5:53 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



I have to show a select list of time in half an hour interval.
I dont want to insert it manually like -

<option value="oneThirty">1:30AM</option>
<option value="two">2AM</option>

Is there way I can do it using loop in PHP and how?

Thanks

cameraman

7:26 pm on Apr 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here you go. This generates values like 1200,1230,1300, which would be easier to process than your textual oneThirty. If you really want to use a textual representation, then you could do an array to convert the hours and conditionally add on 'Thirty' if $mins isn't zero.

<?php
$qty = 10;// How many to generate
// Use this line to start at an arbitrary time:
$st = strtotime('12:00pm',time());
// Uncomment this line to start at current time:
// $st = time();
$mins = date('i',$st);
$hrs = date('G',$st);
// These next lines roll an odd time to the next half-hour interval.
// If you start at an arbitrary time, you can comment/delete
// From here - - - - -
if($mins > 30) {
$mins = 0;
$hrs++;
}
elseif($mins > 0)
$mins = 30;
$st = strtotime("$hrs:$mins",time());
// - - - to here
for($i = 0; $i < $qty; $i++) {
echo '<option value="' . sprintf('%02d%02d',$hrs,$mins) . '">' . date('g:i a',$st) . "</option>\n";
$st += 1800;// 30 minutes
$hrs = date('G',$st);
$mins = date('i',$st);
}// EndFor
?>

ksks

7:35 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



Here is another way that starts at 12:00am

$t = strtotime("Midnight"); // set this to your start point...:)
echo "<select name=\"whatever\">\n";
for($i=0; $i<48; $i++)
{
echo "\t<option value=\"" . strftime("%I:%M%p", $t) . "\">" . strftime("%I:%M%p", $t) . "</option>\n";
$t += (30 * 60);
}
echo "</select>";

sfast

8:47 pm on Apr 13, 2007 (gmt 0)

10+ Year Member



Thanks a lot guys.