What are the best practices for handling overlapping time intervals in PHP scripts to accurately determine a business's open or closed status?
When determining a business's open or closed status based on overlapping time intervals, it is important to accurately handle the logic to avoid any discrepancies. One way to achieve this is by creating a function that checks if the current time falls within any of the defined time intervals for the business's operating hours.
function isBusinessOpen($openHours) {
$currentTime = new DateTime();
$currentDay = $currentTime->format('D');
foreach ($openHours[$currentDay] as $interval) {
$startTime = DateTime::createFromFormat('H:i', $interval['start']);
$endTime = DateTime::createFromFormat('H:i', $interval['end']);
if ($currentTime >= $startTime && $currentTime < $endTime) {
return true;
}
}
return false;
}
// Define the business's operating hours
$openHours = [
'Mon' => [
['start' => '09:00', 'end' => '17:00']
],
'Tue' => [
['start' => '09:00', 'end' => '17:00']
],
// Add the rest of the days and intervals here
];
// Check if the business is open
if (isBusinessOpen($openHours)) {
echo 'The business is open.';
} else {
echo 'The business is closed.';
}