Php, to fix bug when trying to find available places to write to the Google calendar?

My script searches for available recording space in the calendar. User selects the day (weekday/weekend/whatever), time of day (morning/lunch/evening/whatever), duration of reception and a few more criteria and according to this I'm looking for free to record the locations in Google calendar for the next three weeks.

But if you choose duration of two hours, and has a record of 9 and 11 am, the script will show that there is free entry for 10 in the morning. For several hours scratching their heads how to fix it. Here is the function

private function getFree($start, $end, $events, $data){
 $selected_day = $data[0]['selected_day'];
 $selected_time = $data[0]['selected_time'];
 $how_long = $data[0]['how_long'];

 $start = strtotime($start);
 $end = strtotime($end);
 $result = [];
 // Kick off first appt time at the beginning of the day.
 $appt_start_time = $start;

 // Loop through each appt slot in the search range.
 while ($appt_start_time < $end) {
 // Add 29:59 to the appt start time so we know where the appt will end.
 $appt_end_time = ($appt_start_time + 1799);

 // For each appt slot, loop through the current appts to see if it falls
 // in a slot that is already taken.
 $slot_available = true;
 foreach ($events as $event) {
 $this_start = strtotime($event['start']);
 $this_end = strtotime($event['end']);

 // If the appt appt start time or end time falls on a current appt slot is taken.
 if (($appt_start_time >= $this_start && $appt_start_time < $this_end) ||
 ($appt_end_time >= $this_start && $appt_end_time < $this_end)) {
 $slot_available = false;
 break; // No need to continue if it's taken.
}
}

 // If we made it through all appts and the slot is still available, it's an open slot.
 if ($slot_available) {
 $date = new DateTime();
$date->setTimestamp($appt_start_time);
 if ($selected_day === '0') {
//weekday
 if ($date->format('l') == 'Tuesday' || $date->format('l') == 'Thursday') {
 if ($how_long == 150 || $how_long == 180) {
 if ($date->format('H') <= 17) {
 if ($date->format('i') == '00' || $date->format('i') == '30') {
 $result[] = $date->format("Y-m-d H:i");
}
}
 } elseif ($date->format('H') >= 9 && $date->format('H') <= 19) {
 $result[] = $date->format("Y-m-d H:i");
}
}
 } elseif ($selected_day === '1') {
//Saturday
 if ($date->format('l') == 'Saturday') {
 if ($how_long == 150 || $how_long == 180) {
 if ($date->format('H') <= 17) {
 if ($date->format('i') == '00' || $date->format('i') == '30') {
 $result[] = $date->format("Y-m-d H:i");
}
}
 } elseif ($date->format('H') >= 9 && $date->format('H') <= 19) {
 $result[] = $date->format("Y-m-d H:i");
}
}
 } elseif ($selected_day === '2') {
//no matter
 if ($date->format('l') == 'Tuesday' || $date->format('l') == 'Thursday' || $date->format('l') == 'Saturday') {
 if ($how_long == 150 || $how_long == 180) {
 if ($date->format('H') <= 17) {
 if ($date->format('i') == '00' || $date->format('i') == '30') {
 $result[] = $date->format("Y-m-d H:i");
}
}
 } elseif ($date->format('H') >= 9 && $date->format('H') <= 19) {
 $result[] = $date->format("Y-m-d H:i");
}
}
}
}
 // + n minutes
 $appt_start_time += (60 * $how_long);
}
 $acc_result = [];
 $time_arr = [
 '1' => [
 'start' => 9,
 'end' => 12
],
 '2' => [
 'start' => 12,
 'end' => 17
],
 '3' => [
 'start' => 17,
 'end' => 19
],
 '4' => [
 'start' => 9,
 'end' => 19
],
];
 foreach ($result as $time) {
 if (date('H', strtotime($time)) >= $time_arr[$selected_time]['start'] && date('H', strtotime($time)) <= $time_arr[$selected_time]['end']) {
 $acc_result[] = [
 'title' => $time,
 'id' => $time,
 'aid' => $aid,
];
}
}
 return $acc_result;
}
April 7th 20 at 15:59
0 answer

Find more questions by tags PHP