1 回答
TA贡献1828条经验 获得超6个赞
如果您的预订未按开始时间排序,则需要对其进行排序。然后您可以遍历预订,在一个预订结束和下一个预订开始之间创建空闲时间条目。请注意,由于您的时间是HH:mm:ss格式,您可以直接将它们作为字符串进行比较;无需将它们转换为整数时间值。
$hours = ["08:00:00", "18:00:00"];
$bookings = [["10:00:00", "15:00:00"],
["08:05:00", "09:00:00"],
["16:00:00", "17:00:00"]
];
$start = $hours[0];
$end = $hours[1];
// sort the bookings
$bstart = array_column($bookings, 0);
array_multisort($bstart, $bookings);
// get the free times
$time = $start;
$bindex = 0;
$free = array();
while ($time < $end && $bindex < count($bookings)) {
if ($time < $bookings[$bindex][0]) {
$free[] = [$time, $bookings[$bindex][0]];
}
$time = $bookings[$bindex][1];
$bindex++;
}
// end of day free?
if ($time < $end) {
$free[] = [$time, $end];
}
print_r($free);
输出:
Array
(
[0] => Array
(
[0] => 08:00:00
[1] => 08:05:00
)
[1] => Array
(
[0] => 09:00:00
[1] => 10:00:00
)
[2] => Array
(
[0] => 15:00:00
[1] => 16:00:00
)
[3] => Array
(
[0] => 17:00:00
[1] => 18:00:00
)
)
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报