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 " {$previous}-{$current}
";
}
$previous = $current;
}
See it in action