I have to make some piece of code for scheduling software which shows whether the instructor is available or not.
So I have made the following (month) period:
$begin = new \DateTime('first day of this month');
$end = new \DateTime('last day of this month');
$dailyInterval = new \DateInterval('P1D');
$dailyPeriod = new \DatePeriod($begin, $dailyInterval, $end);
$data = [];
/** @var \DateTime $date */
foreach($dailyPeriod as $date)
{
$currentDate = $date->format('Y-m-d');
$start = new \DateTime("$currentDate 08:00");
$finish = new \DateTime("$currentDate 20:00");
$appointmentInterval = new \DateInterval($appointmentVariant->getInterval());
$appointmentPeriod = new \DatePeriod($start, $appointmentInterval, $finish);
/** @var \DateTime $duration */
foreach($appointmentPeriod as $duration)
{
$endDate = clone $duration;
$data['availability'][$currentDate][] = [
'start' => $duration->format('H:i:s'),
'end' => $endDate->modify('+90 minutes')->format('H:i:s')
];
}
}
The output of this is as follows:
availability: [
'2016-12-01': [
0: ['start': '08:00:00', 'end': '09:30:00'],
1: ['start': '09:30:00', 'end': '11:00:00'],
2: ['start': '11:00:00', 'end': '12:30:00'],
3: ['start': '12:30:00', 'end': '14:00:00'],
4: ['start': '14:00:00', 'end': '15:30:00'],
5: ['start': '15:30:00', 'end': '17:00:00'],
6: ['start': '17:00:00', 'end': '18:30:00'],
7: ['start': '18:30:00', 'end': '20:00:00']
]
]
But now we have to get out from the database when instructor is unavailable (this can come from multiple sources, appointments, vacations, recurring events etc), so finally we have the array as the above one, but with unavailability:
unavailability: [
'2016-12-01': [
0: ['start': '14:30:00', 'end': '16:30:00'],
1: ['start': '18:00:00', 'end': '18:20:00'],
]
]
And now is the question, how to diff those two arrays to get one final when the instructor is available? It's pretty hard to loop over available dates and times and then unavailable dates and times.
The diff should give me back the following result (DEL = to delete):
availability: [
'2016-12-01': [
0: ['start': '08:00:00', 'end': '09:30:00'],
1: ['start': '09:30:00', 'end': '11:00:00'],
2: ['start': '11:00:00', 'end': '12:30:00'],
3: ['start': '12:30:00', 'end': '14:00:00'],
DEL 4: ['start': '14:00:00', 'end': '15:30:00'],
DEL 5: ['start': '15:30:00', 'end': '17:00:00'],
DEL 6: ['start': '17:00:00', 'end': '18:30:00'],
7: ['start': '18:30:00', 'end': '20:00:00']
]
]
Does anybody know the most efficient way to do it?