<?php
$arr = [
[
'hours' => '09:00:00',
'freeend' => '12:00:01'
],
[
'hours' => '14:00:00',
'freeend' => '15:00:01'
],
[
'hours' => '16:00:00',
'freeend' => '19:00:01'
],
[
'hours' => '10:00:00',
'freeend' => '23:00:01'
]
];
$appointment_duration = new DateInterval('PT2H');
$next_hour = new DateInterval('PT1H');
foreach($arr as $row){
$start_time = new DateTime($row['hours']);
$end_time = new DateTime($row['freeend']);
echo "Appointments available between $row[hours] and $row[freeend] <br/>";
$curr_start_time = $start_time;
$curr_end_time = new DateTime($start_time->format("H:i:s"));
$curr_end_time = $curr_end_time->add($appointment_duration);
do{
if($curr_end_time > $end_time){
echo "$row[hours]-$row[freeend] <br/>";
break;
}
echo $curr_start_time->format("H:i:s"),"-",$curr_end_time->format("H:i:s"),"<br/>";
$curr_start_time = $curr_start_time->add($next_hour);
$curr_end_time = new DateTime($curr_start_time->format("H:i:s"));
$curr_end_time = $curr_end_time->add($appointment_duration);
}while($curr_end_time <= $end_time);
echo "<br/>";
}
输出
Appointments available between 09:00:00 and 12:00:01
09:00:00-11:00:00
10:00:00-12:00:00
Appointments available between 14:00:00 and 15:00:01
14:00:00-15:00:01
Appointments available between 16:00:00 and 19:00:01
16:00:00-18:00:00
17:00:00-19:00:00
Appointments available between 10:00:00 and 23:00:01
10:00:00-12:00:00
11:00:00-13:00:00
12:00:00-14:00:00
13:00:00-15:00:00
14:00:00-16:00:00
15:00:00-17:00:00
16:00:00-18:00:00
17:00:00-19:00:00
18:00:00-20:00:00
19:00:00-21:00:00
20:00:00-22:00:00
21:00:00-23:00:00