I have a database table storing opening hours of a shop as a time range as a VARCHAR like ‘9am-5pm’ and ‘9am-3pm, 5pm-11pm’. I want to compare these opening hours with the current time, so if the current time is 2pm, I will use PHP to echo a text stating that the shop is now open, and if the current time is 11.30pm, the shop will be shown as closed.
- How do I compare the current time with the opening hours time range in the tables
- Is my method of storing time range suitable?
I’m using Codeigniter, so if theres useful functions do let me know too!
You should use 2 columns (one for open time, one for close time) both using the
TIMEdatatype: http://dev.mysql.com/doc/refman/5.1/en/time.htmlBy doing that, you can then use
BETWEENandNOW():SELECT * FROM shop WHERE CAST(NOW() AS TIME) BETWEEN open_time AND close_time;Edit to include table definition: