I'm building a PHP application to manage business operating hours, including cases where the closing time extends past midnight. I'm considering various ways to store these hours in MySQL and need advice on the best approach/best practices.
Challenges:
Some businesses operate past midnight, complicating data storage and retrieval. Efficiently querying current time against stored business hours is critical. Here is my current table that does not account for past midnight operation:
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| account_id | int | NO | MUL | NULL | |
| weekday | int | NO | | NULL | |
| open | time | YES | | NULL | |
| close | time | YES | | NULL | |
+--------------+------------+------+-----+---------+----------------+
Proposed Solutions:
Start Time and Duration: Store opening time and duration of operation.
Is_overnight Flag: Use an is_overnight boolean field to indicate operations extending into the next day.
Additional Entry for Overnight Hours: Implement PHP logic to check if the closing time is past midnight and, if so, insert an additional entry for the next day. This method splits the operating hours across two days where necessary.
Are there any better approaches or modifications to the above methods that can enhance scalability and manageability? Which method is preferred? This seems like it would be a common problem for many businesses such as Google Business, Yelp, etc. what is their solution?