简体   繁体   English

使用开始时间和结束时间制定给定分钟的时间表

[英]making schedule of given minutes using start time and end time

I am working on booking appointment project. 我正在做预约项目。 Using start time and end time fromm schedule.I want to make timeslots like below for every day avoiding booked slots. 使用开始时间和结束时间的时间表。我想每天像下面这样安排时间,避免预定时间。

"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"
        }]

I have tried code as below using this link Partion 20 min slot of time in php from start and end time 我已经尝试过使用以下链接从开始和结束时间在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;

    }

Above code works fine when i have some slots are booked. 当我预订了一些空位时,以上代码可以正常工作。 But when $bookedSlots is empty that means no slot is booked for day i have to show all available slots, but it returns empty array. 但是当$ bookedSlots为空时,这意味着当天没有预订任何插槽,我必须显示所有可用的插槽,但是它返回空数组。 Can anybody help? 有人可以帮忙吗?

Where is the $i++ in your for() loop? for()循环中的$i++在哪里?

// this
for ($i = $start; $i <= $end;) {  //for loop 

// should be this
for ($i = $start; $i <= $end; $i++) {  //for loop 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM