I am working on a feature that checks if external events collide with internal events (in a calendar app). The process looks like this:
- My application creates an array of possible events (called
$internalEvents
) - I source external events from calendars such as Google Calendar, iCloud, etc. (called
$externalEvents
). These are existing events with the typebusy
. - Now I have to check if there is any kind of conflict regarding internal and external events. I tried something as you can see below but this is by far not correct or bulletproof.
I tried to reduce it to the minimum as much as possible. This is the data input:
$internalEvents = array(
array(
"start" => "03/29/2016 12:00:00",
"end" => "03/29/2016 13:00:00"
),
array(
"start" => "03/29/2016 12:30:00",
"end" => "03/29/2016 13:30:00"
),
array(
"start" => "03/29/2016 13:00:00",
"end" => "03/29/2016 14:00:00"
),
array(
"start" => "03/29/2016 13:30:00",
"end" => "03/29/2016 14:50:00"
),
array(
"start" => "03/29/2016 14:00:00",
"end" => "03/29/2016 15:00:00"
),
array(
"start" => "03/29/2016 14:30:00",
"end" => "03/29/2016 15:30:00"
),
array(
"start" => "03/29/2016 15:00:00",
"end" => "03/29/2016 16:00:00"
),
array(
"start" => "03/29/2016 15:30:00",
"end" => "03/29/2016 16:30:00"
),
array(
"start" => "03/29/2016 16:00:00",
"end" => "03/29/2016 17:00:00"
)
);
$externalEvents = array(
array(
"start" => "03/29/2016 08:00:00",
"end" => "03/29/2016 12:00:00",
"type" => "busy"
),
array(
"start" => "03/29/2016 15:30:00",
"end" => "03/29/2016 16:00:00",
"type" => "busy"
),
array(
"start" => "03/29/2016 13:30:00",
"end" => "03/29/2016 14:15:00",
"type" => "busy"
)
);
Now I try to find any kind of conflict by comparing the internal event to all external events:
foreach($internalEvents as $internalEvent) {
$internalEventStart = new DateTime($internalEvent['start']);
$internalEventEnd = new DateTime($internalEvent['end']);
$result = true;
echo "
verifying " . $internalEventStart->format('Y-m-d H:i') . " - " . $internalEventEnd->format('Y-m-d H:i') . "
";
foreach($externalEvents as $externalEvent) {
$externalEventStart = new DateTime($externalEvent['start']);
$externalEventEnd = new DateTime($externalEvent['end']);
// check if there are conflicts between internal and external events
if ($internalEventStart >= $externalEventStart && $internalEventStart <= $externalEventEnd) {
$result = false;
echo " problem 1: event is between busy time: " . "
";
}
if ($internalEventStart >= $externalEventStart && $internalEventStart <= $externalEventEnd && $externalEventEnd <= $internalEventEnd) {
$result = false;
echo " problem 2: event starts during busy time: " . "
";
}
if ($internalEventStart <= $externalEventStart && $externalEventStart <= $internalEventEnd && $internalEventEnd <= $externalEventEnd) {
$result = false;
echo " problem 3: event stops during busy time: " . "
";
}
if (($internalEventStart <= $externalEventStart) && ($externalEventStart <= $externalEventEnd) && ($externalEventEnd <= $internalEventEnd)) {
$result = false;
echo " problem 4: event during busy time: " . "
";
}
if (($internalEventStart <= $internalEventEnd) && ($internalEventEnd <= $externalEventStart) && ($externalEventStart <= $externalEventEnd)) {
$result = false;
echo " problem 5: event during busy time: " . "
";
}
}
if($result) {
echo " result: OK
";
} else {
echo " result: NOT OK
";
}
}
I am looking for an algorithm that can find any possible event overlap conflict. Any hint is highly appreciated.
The running code can be found here (IDEone.com).