php - 使用开始时间和结束时间制定给定分钟的时间表
问题描述
我正在做预约项目。使用时间表中的开始时间和结束时间。我想为每天制作如下的时间段,避免预订时间段。
"availableSlots": [
{
"start": "10:30",
"stop": "11:00"
},
{
"start": "11:00",
"stop": "11:30"
},
{
"start": "11:30",
"stop": "12:00"
},
{
"start": "12:00",
"stop": "12:30"
},
{
"start": "12:30",
"stop": "13:00"
},
{
"start": "13:00",
"stop": "13:30"
},
{
"start": "13:30",
"stop": "14:00"
},
{
"start": "14:00",
"stop": "14:30"
},
{
"start": "14:30",
"stop": "15:00"
},
{
"start": "15:00",
"stop": "15:30"
}]
我已经尝试使用此链接在php中从开始到结束时间划分20分钟时间段的代码如下
Public function availableTimeSlots($start_time, $end_time, $bookedSlots, $timePerSlot) {
$start = DateTime::createFromFormat('Y-m-d H:i:s', $start_time);//createdate time objects
$end = DateTime::createFromFormat('Y-m-d H:i:s', $end_time); //create date time objects
$count = 0; //number of slots
$out = array(); //array of slots
for ($i = $start; $i <= $end;) { //for loop
$avoid = false; //booked slot?
$time1 = $i->format('H:i'); //take hour and minute
$i->modify("+" . $timePerSlot . " minutes"); //add time per slot minutes
$time2 = $i->format('H:i'); //take hour and minute
$slot = $time1 . "-" . $time2; //create a format 12:40-13:00 etc
for ($k = 0; $k < sizeof($bookedSlots); $k++) { //if booked hour
if ($bookedSlots[$k] == $slot) { //check
$avoid = true; //yes. booked
}
}
if (!$avoid && $i <= $end) { //if not booked and less than end time
$count++; //add count
$slots = ['start' => $time1, 'stop' => $time2]; //add count
array_push($out, $slots); //add slot to array
}
}
return $out;
}
当我预订了一些插槽时,上面的代码可以正常工作。但是当 $bookedSlots 为空时,这意味着当天没有预订插槽,我必须显示所有可用插槽,但它返回空数组。有人可以帮忙吗?
解决方案
$i++
你的for()
循环在哪里?
// this
for ($i = $start; $i <= $end;) { //for loop
// should be this
for ($i = $start; $i <= $end; $i++) { //for loop
推荐阅读
- java - Spring Boot 不注入具有 Web 感知范围的 Bean
- typescript - 赛普拉斯在开玩笑断言中导致类型错误
- ajax - 如何触发
我需要触发面板内的 ajax 事件,并且面板位于 ac:forEach 内,我该怎么做?我尝试这样做并放置示例的方式,它不会触发 bean 中的事件。
这是xhtml:
<p:panel header="Configuração de Dashboard"> <p:d
- vb.net - 如何从我的表单上的按钮单击另一个程序上的按钮?
- javascript - 量角器:如果一个场景失败,如何解决承诺并停止跳过场景
- vue.js - 来自祖父母的组件,来自父母的数据,在孩子中呈现
- c# - .NET Core 和 .NET Framework 之间的 CultureInfo 不同
- r - 在 R 中找到一个截止值
- java - 如何禁用 Spring 执行器的内容协商?
- android - Android更改默认权限保护级别