我正在做预约项目。使用时间表中的开始时间和结束时间。我想为每天制作如下的时间段,避免预订时间段。
"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 为空时,这意味着当天没有预订插槽,我必须显示所有可用插槽,但它返回空数组。有人可以帮忙吗?