I have shifts created for different user groups. The shift includes shift start-time and end-time, days of the week(multi-select) and date period(from the date - to date) applicable.
Where shift select for a particular user is multiple select as one user can have multiple shifts on the same day or different day on different time. So how do I check that selected shifts do not overlap the way that one user can not have two shifts at the same time?
I have tried checking first, weekdays intersect or not, if it intersects then check the date and time period overlaps or not using the code below but did not get a correct result.
/**
* @Route("/shift/checkTimeConflict", methods={"POST"})
*/
public function checkTimeConflict(Request $request)
{
$data = $request->request->all();
if (isset($data['shifts']) && count($data['shifts']) > 1) {
$shifts = $this->getDoctrine()
->getRepository(Shift::class)
->findById($data['shifts']);
foreach ($shifts as $key => $val) {
$shift_data[$key]['id'] = $val->getId();
$shift_data[$key]['title'] = $val->getTitle();
$shift_data[$key]['weekday'] = $val->getWeekday();
$shift_data[$key]['start_time'] = $val->getStartTime()->format('H:i');
$shift_data[$key]['end_time'] = $val->getEndTime()->format('H:i');
$shift_data[$key]['time_period_start'] = $val->getTimePeriodStart()->format('Y-m-d');
$shift_data[$key]['time_period_end'] = $val->getTimePeriodEnd()->format('Y-m-d');
}
for ($i=0; $i < count($shift_data); $i++) {
if ($this->checkDateTimeOverlaps($shift_data[$i], $shift_data))
return new JsonResponse(true);
}
}
return new JsonResponse(false);
}
/**
* Check provided shift time for the day and period overlaps with other shifts
*/
public function checkDateTimeOverlaps($shift, $shift_data)
{
$start = strtotime($shift['time_period_start'].' '.$shift['start_time']);
$end = strtotime($shift['time_period_end'].' '.$shift['end_time']);
for ($i=0; $i < count($shift_data); $i++) {
if ($shift['id'] != $shift_data[$i]['id']) {
$check_start = strtotime($shift['time_period_start'].' '.$shift['start_time']);
$check_end = strtotime($shift['time_period_end'].' '.$shift['end_time']);
if (!empty(array_intersect($shift['weekday'], $shift_data[$i]['weekday']))) {
if (($start > $check_start && $start < $check_end) || ($end > $check_start && $end < $check_end) || ($check_start > $start && $check_start < $end) || ($check_end > $start && $check_end < $end)) {
return true; // intersects
}
}
}
}
return false; // no conflict
}
For example, selected shifts have the below data:
(1) 09:00-13:00, Monday-Friday, 2019-01-01 - 2020-01-01, First-Half
(2) 14:00-18:00, Monday-Friday, 2019-01-01 - 2020-01-01, Second-Half
(3) 18:00-20:00, Monday-Friday, 2019-01-01 - 2020-01-01, Over-Time
(4) 09:00-14:00, Saturday, 2019-01-01 - 2020-01-01, Saturday Half Day
(5) 09:00-13:00, Monday-Friday, 2020-02-01 - 2021-02-28, Part-Time
The above should return no conflict [checkDateTimeOverlaps() should return false], as there is no intersection of time for the same day and date period. However, if I replace the 5th row as below:
(5) 09:00-13:00, Monday-Friday, 2019-01-01 - 2019-01-31, Part-Time
it should return conflict [checkDateTimeOverlaps() should return true], as one person can not have two shifts at the same time. In the above case, row 1 and row 5 have an intersection.