简体   繁体   中英

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

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. Can anybody help?

Where is the $i++ in your for() loop?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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