What best practices should be followed when checking for overlapping events in PHP?
When checking for overlapping events in PHP, it's important to compare the start and end times of each event to determine if there is any overlap. One approach is to check if the start time of one event is before the end time of another event, and vice versa. Additionally, it's crucial to handle edge cases where events start or end at the same time.
function checkForOverlap($event1, $event2) {
if (($event1['start'] < $event2['end'] && $event1['end'] > $event2['start']) ||
($event2['start'] < $event1['end'] && $event2['end'] > $event1['start'])) {
return true; // Overlapping events
}
return false; // No overlap
}
// Example events
$event1 = ['start' => '2022-01-01 09:00:00', 'end' => '2022-01-01 12:00:00'];
$event2 = ['start' => '2022-01-01 10:00:00', 'end' => '2022-01-01 14:00:00'];
if (checkForOverlap($event1, $event2)) {
echo "Events overlap";
} else {
echo "Events do not overlap";
}