I’m trying to store a week schedule (e.g. Monday, 9am-5pm, etc.). I do not have the need to store the dates; I just need to save the following: day, from time, to time.
So, say I have the following time values:
- 1:20pm
- 1320
- 8:00 AM
- etc
Assuming that the values are actual valid times, how do I convert these strings into MySQL Time type? And how do I do the reverse? (I’m using PHP.)
Also, how do I query for something like this: find every store that is open on Mondays between 2pm and 3pm? Do I just do something like: WHERE day = 1 AND from_time >= 2pm AND to_time <= 3pm (changing ‘2pm’ and ‘3pm’ to whatever their converted values are, of course)? Or is there some MySQL function better suited for such queries?
MySQL has a TIME data type – it will store values in the hh:mi:ss format, and you can use the TIME_FORMAT function to change the presentation to however you’d like.
Try not to rely on functions applied to the column for comparison – IE:
…because it will render an index, if one exists on the column, useless — ensuring a table scan (worst performing option).