php - 如何检查给定日期的预订时段
问题描述
这是我的数组,我想返回已经按日期预订的数组。
目前它在时间上是有效的,但是当我这样做超过一天时它会停止工作:
$bookedData = array(
Id : 1
date : "2020-03-31"
endDate : "2020-04-01"
time : "19:00:00"
endTime : "11:30:00"
)
foreach($bookedData as $booked) { /* checking fr all slots*/
if (strlen($booked->time) > 0 && strlen($booked->endTime) > 0 {
if ($booked->time !=$booked->endTime) {
$appStrtTime = strtotime($booked->time); /* It gives time startTime */
$appEndTime = strtotime($booked->endtime); /* It gives endTime */
$AddMins = 5*60;
while ($appStartTime < $appEndTime) { /* if starttime is less than endtime then return false */
$time = date("G:i:s", $appStartTime);
$key = array_search($time, $ReturnArray);
if($key!= false) {
unset($ReturnArray[$key]); /* if slot already available then unset it */
}
$appStartTime +=$AddMins;
}
$appStartTime += $AddMins;
}
} else {
$key = array_search($booked->time, $ReturnArray);
if ($key != false) {
unset($ReturnArray[$key]);
}
}
}
}
if (count($ReturnArray) > 0) {
unset($ReturnArray[count($ReturnArray) - 1]);
}
$ReturnArray = array_values($ReturnArray);
}
解决方案
根据你的评论
例如,如果我选择开始时间日期为 3 月 31 日,时间选择为 19:00,那么我想获得该时间之后的所有时段,例如 19:00,19:15,... 直到结束日期
我会在这里寻找这段代码:
$start = new \DateTime('2020-03-31 19:00');
$end = new \DateTime('2020-03-31 21:30');
$slotSize = 15; // 15 minute intervals
$slots = [];
do {
// Add here your custom code to skip specific timeslots
// As an example I used a custom function as an example
if (slot_is_blocked_by_existing_bookings($slot, $bookings)) {
continue;
}
$slots[] = clone $start;
} while ($start->add(new \DateInterval("PT{$slotSize}M")) <= $end);
这是$slots
-list 的示例输出,其中包含 DateTime 对象(出于可读性原因,字符串格式化为 RFC3339)
array:11 [
0 => "2020-03-31T19:00:00+00:00"
1 => "2020-03-31T19:15:00+00:00"
2 => "2020-03-31T19:30:00+00:00"
3 => "2020-03-31T19:45:00+00:00"
4 => "2020-03-31T20:00:00+00:00"
5 => "2020-03-31T20:15:00+00:00"
6 => "2020-03-31T20:30:00+00:00"
7 => "2020-03-31T20:45:00+00:00"
8 => "2020-03-31T21:00:00+00:00"
9 => "2020-03-31T21:15:00+00:00"
10 => "2020-03-31T21:30:00+00:00"
]
推荐阅读
- c# - 当今天更改为第二天时,如何在asp.net中触发事件?
- performance - Selenium webdriver 需要 25 分钟来识别并单击一个元素
- r - 在 gt 中的行组行上重复列标签
- redis - 如何在 ubuntu 服务器 18.04 上将 redis 作为服务激活
- c++ - 关于作为类成员的对象
- javascript - 如何制作 SVG 组件?
- karate - “匹配每个”失败,不是 json 数组:+ [类型:JSON,值:com.jayway.jsonpath.internal.JsonContext@68c87fc3],路径:$
- javascript - 异步调度应该使用什么类型?
- python - ZMQ 中的 Req/rep 中的系统调用中断
- mysql - MySQL Join 更新因索引太大而失败