php - 计算可用时隙
问题描述
我有以下显示可用时间段的脚本:
$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
,它就会返回不正确的可用时隙。
如何解决这个问题?还是有更好的方法来做到这一点?
解决方案
推荐阅读
- c# - 使用 xamarin 表单的音频控制
- django - django - 具有关系查询的搜索功能
- azure - Oracle 云基础架构中与 Azure“资源组”有何相似之处?
- c# - 编写 C# 列表时出现 ArgumentOutOfRangeException
到多个文本框 - python - 如何在 Python 中从 Outlook Web App 获取附件
- docker - 将 Docker 内部生成的 ssh 密钥用作“假”SSH 密钥是否安全?
- c++ - 在 Gnuplot 的直方图上叠加一条线
- php - 在 PHP 和 Jquery 中使用 md5 更改密码
- docker - Jenkins Docker 插件无法连接到 Docker 服务器主机 url
- mysql - 如何创建 SQL 查询来更新表中的数据?