How to create a dropdown of time?
Why not use the DateTime object which is specifically designed for this type of thing?
$starttime = '00:00:00';
$time = new DateTime($starttime);
$interval = new DateInterval('PT30M');
$temptime = $time->format('H:i:s');
do {
echo $temptime . '<br />';
$time->add($interval);
$temptime = $time->format('H:i:s');
} while ($temptime !== $starttime);
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>';
$hoursArray = range(0, 12);
$minutesArray = range(0, 60);
$amPmArray = array('AM', 'PM');
//Pad the min and hr fields with a leading zero
array_walk($hoursArray, 'padLeadingZero');
array_walk($minutesArray, 'padLeadingZero');
/**
* Pad give input with a leading 0
* @param int $val Value that needs to be padded with a leading 0
*/
function padLeadingZero(&$val){
$val = str_pad($val, $padLength = 2, $padString = '0', $padType = STR_PAD_LEFT);
}
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>