I want to create a dropdown for time in PHP. It should be of 24 hour format with an interval of 30 minutes like 00:00
then 00:30
then 13:00
and so on.I want it round the clock.
I have used this code
<?php
$start = strtotime('12:00 AM');
$end = strtotime('11:59 PM');
?>
<select style="width:85px;" name="select1" id="select1">
<?php for($i = $start;$i<=$end;$i+=1800){ ?>
<option value='<?php echo date('G:i', $i); ?>'><?php echo date('G:i', $i); ?></option>;
<?php } ?>
</select>
The problem is that it doesn't cover the last span of 11:30 to 12:00 AM as the increment value exceeds the end, Is there a direct way to do this in PHP?
You could use that function to create time list anywhere on your page:
/**
*
* Get times as option-list.
*
* @return string List of times
*/
function get_times ($default = '19:00', $interval = '+30 minutes') {
$output = '';
$current = strtotime('00:00');
$end = strtotime('23:59');
while ($current <= $end) {
$time = date('H:i', $current);
$sel = ($time == $default) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date('h.i A', $current) .'</option>';
$current = strtotime($interval, $current);
}
return $output;
}
Get times:
<select name="time"><?php echo get_times(); ?></select>
Here is the result:
<select name="time">
<option value="00:00">12.00 AM</option>
<option value="00:30">12.30 AM</option>
<option value="01:00">01.00 AM</option>
<option value="01:30">01.30 AM</option>
<option value="02:00">02.00 AM</option>
<option value="02:30">02.30 AM</option>
<option value="03:00">03.00 AM</option>
<option value="03:30">03.30 AM</option>
<option value="04:00">04.00 AM</option>
<option value="04:30">04.30 AM</option>
<option value="05:00">05.00 AM</option>
<option value="05:30">05.30 AM</option>
<option value="06:00">06.00 AM</option>
<option value="06:30">06.30 AM</option>
<option value="07:00">07.00 AM</option>
<option value="07:30">07.30 AM</option>
<option value="08:00">08.00 AM</option>
<option value="08:30">08.30 AM</option>
<option value="09:00">09.00 AM</option>
<option value="09:30">09.30 AM</option>
<option value="10:00">10.00 AM</option>
<option value="10:30">10.30 AM</option>
<option value="11:00">11.00 AM</option>
<option value="11:30">11.30 AM</option>
<option value="12:00">12.00 PM</option>
<option value="12:30">12.30 PM</option>
<option value="13:00">01.00 PM</option>
<option value="13:30">01.30 PM</option>
<option value="14:00">02.00 PM</option>
<option value="14:30">02.30 PM</option>
<option value="15:00">03.00 PM</option>
<option value="15:30">03.30 PM</option>
<option value="16:00">04.00 PM</option>
<option value="16:30">04.30 PM</option>
<option value="17:00">05.00 PM</option>
<option value="17:30">05.30 PM</option>
<option value="18:00">06.00 PM</option>
<option value="18:30">06.30 PM</option>
<option value="19:00" selected="">07.00 PM</option>
<option value="19:30">07.30 PM</option>
<option value="20:00">08.00 PM</option>
<option value="20:30">08.30 PM</option>
<option value="21:00">09.00 PM</option>
<option value="21:30">09.30 PM</option>
<option value="22:00">10.00 PM</option>
<option value="22:30">10.30 PM</option>
<option value="23:00">11.00 PM</option>
<option value="23:30">11.30 PM</option>
</select>
Uhm, it is extremely unlikely that in the future, our clocks suddenly gain a 25th hour, so a normal loop should do fine:
for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'
.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With