I am working on an appointment script that takes office opening and closing hours from database and then displays the time in between the opening and closing hours to book f
$start = new DateTime('09:00:00');
$end = new DateTime('16:00:01'); // add 1 second because last one is not included in the loop
$interval = new DateInterval('PT30M');
$period = new DatePeriod($start, $interval, $end);
$previous = '';
foreach ($period as $dt) {
$current = $dt->format("h:ia");
if (!empty($previous)) {
echo "<input name='time' type='radio' value='{$previous}|{$current}'> {$previous}-{$current}<br/>";
}
$previous = $current;
}
See it in action
strtotime
's magic can do this
$start = '09:00:00';
$end = '16:00:00';
$time = strtotime($start);
$timeStop = strtotime($end);
while($time<$timeStop) {
echo date('H:i', $time);
$time = strtotime('+30 minutes', $time);
echo ' - ' . date('H:i', $time) . '<br/>';
}
Result:
09:30 - 10:00
10:00 - 10:30
10:30 - 11:00
11:00 - 11:30
11:30 - 12:00
12:00 - 12:30
12:30 - 13:00
13:00 - 13:30
13:30 - 14:00
14:00 - 14:30
14:30 - 15:00
15:00 - 15:30
15:30 - 16:00
How about
<?php
for ($i = 0; $i < $total_hours; $i++)
{
$time = $start[0] + $i;
echo "<input name='time' type='radio' value='0" . $time . ":00|0" . $time . ":30'> " . $time . ":00am-" . $time . ":30am<br/>";
}
?>