首页 > 解决方案 > 计算可用时隙

问题描述

我有以下显示可用时间段的脚本:

$businessHours = [
    ['09:00:00', '13:00:00'],
    ['14:00:00', '18:00:00'] // notice the 1 hour break
];

$appointments = [
    ['10:00:00', '10:30:00'], // format is [from, until]
    ['15:30:00', '16:10:00']
];

$result = [];
foreach ($businessHours as $businessHour) {
    list($start, $end) = [strtotime($businessHour[0]), strtotime($businessHour[1])];

    while ($start < $end) {
        $first = $start;
        $start += 15 * 60; // this is the interval, equals 15 minutes, will be dynamic based on service duration

        $result[] = [date('H:i:s', $first), date('H:i:s', $start)];
    }
}

foreach ($appointments as $appointment) {

    foreach ($result as $k => $item) {
        if (strtotime($item[0]) >= strtotime($appointment[0]) &&  strtotime($item[1]) <= strtotime($appointment[1])){
            unset($result[$k]);
        }
    }
}

print_r($result);

使用 , , 等时间时它16:00可以16:15正常16:30工作16:45

但是,只要有像16:10, 16:20,之类的时间16:40,它就会返回不正确的可用时隙。

如何解决这个问题?还是有更好的方法来做到这一点?

标签: phpphp-carbon

解决方案


推荐阅读