Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "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"
- }]
- Public function availableTimeSlots($start_time, $end_time, $bookedSlots, $timePerSlot) {
- $start = DateTime::createFromFormat('Y-m-d H:i:s', $start_time); //create date 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;
- }
Add Comment
Please, Sign In to add comment