How can the code snippet be improved to handle multiple opening times for a single day more efficiently?
The code snippet can be improved by storing the opening times for each day in an array, where each day can have multiple opening times. This can be achieved by using a multidimensional array where the key is the day of the week and the value is an array of opening times. This allows for more flexibility in handling multiple opening times for a single day efficiently.
// Define opening times for each day
$openingTimes = [
'Monday' => ['09:00-12:00', '13:00-17:00'],
'Tuesday' => ['09:00-12:00', '13:00-17:00'],
'Wednesday' => ['09:00-12:00', '13:00-17:00'],
'Thursday' => ['09:00-12:00', '13:00-17:00'],
'Friday' => ['09:00-12:00', '13:00-17:00'],
'Saturday' => ['09:00-12:00'],
'Sunday' => ['Closed']
];
// Get current day of the week
$currentDay = date('l');
// Check if the current day is in the opening times array
if (array_key_exists($currentDay, $openingTimes)) {
// Output the opening times for the current day
echo "Today's opening times are: " . implode(', ', $openingTimes[$currentDay]);
} else {
// Output that the store is closed
echo "Sorry, we are closed today.";
}
Keywords
Related Questions
- Are there any best practices for ensuring secure data transmission in PHP when the user does not have access to the web server?
- What are the recommended PHP functions for handling file operations and data manipulation?
- In what scenarios can using absolute paths instead of relative paths improve the reliability of file operations in PHP scripts?