19

In my shops database I need to have the opening hours. Do you have an idea how i can implement this in my dB?

The opening hours are from Monday to Sunday, each day can have 2 opening windows (ex 09:00-12:00, 16:00-19:00)

2 Answers 2

38

build another table, call it schedules, add a foreign key to the shops table primary key, a Day of week field, time_open, time_closed. The data should look something like this:

shop_id     day_of_week      time_open        time_closed
1           1                09:00            12:00
1           1                16:00            19:00
1           2                09:00            12:00
1           2                16:00            19:00
1           3                09:00            12:00
1           3                16:00            19:00
1           6                10:00            14:00
2           1                09:00            12:00
2           1                13:00            18:00

This will give you the opportunity to build any kind of schedules, with as many windows as you want, with how many exceptions you need. It's universal, limited only to the fact that it expects all weeks to be identical. No holidays considered, nor odd/even-week schedules that someone might use.

Edit:
With Julien's question, about working hours of a night business, it has come to my attention that the previous solution is not the best bu far. You can't have a bar open at 20:00, close at 06:00, and compare if current time (02:45) is inside this interval, because it won't be. That's why, it would be most convenient to register not the closing time, but the total working time, in the convenient unit of measure (minutes for example).

shop_id     day_of_week      time_open        working_time
1           1                09:00            180
1           1                16:00            180
1           2                09:00            180
1           2                16:00            180
1           3                09:00            180
1           3                16:00            180
1           6                10:00            240
2           1                09:00            180
2           1                13:00            300
6
  • 1
    this will allow you to add a third schedule for a given day, if that's a violation of your rules, you'll need to tightly control the addition of new schedules to check for that... or you could add a "sched_num" column with a check constraint that it be either a 0 or 1 and a unique constraint on day_of_week and sched_num. Apr 27, 2010 at 19:07
  • 1
    which is the better way for handling opening hours for night biz, like a bar, open from 10:00pm to 04:00am. Add two entry (day1 : 22:00->23:59 and day2: 00:00->04:00) ? or one entry (day1: 22:00->04:00) ?
    – Julien
    Feb 27, 2012 at 0:18
  • 1
    Interesting question. In this case it would be most reasonable to re-make this situation. See edited response.
    – Alex
    Mar 2, 2012 at 2:46
  • 1
    I think in this situation, I would just record the time in decimal hours and allow > 24 hours in a day. So, on Saturday, the bar might be open from 22.00 to 28.00 (4am the next day). For example. But it really depends on the use cases I'd be supporting with the database.
    – mkoistinen
    Nov 2, 2014 at 22:06
  • I was thinking...for the second open/close window why not add two more columns...so for open/close in total 4 columns...open_a,close_a,open_b,close_b....just a thought Feb 20, 2021 at 19:40
5

What type of database are you using? If it's mysql, put in a field of type "time". Then you can pass it the times and do basic sql time manipulations on them. This whole post assumes mysql.

If there is only one store and you know it will never grow, just make a table called times where you keep the times the store is open. Then, if you want to see if the store is open just check the current time against one of the ranges in the database.

If there is more than one store, make a table called "times" and a table called "stores". In the times table, columns can be "open" and "close" with an id called "store_id". The stores table needs an id too. Then, you can just say:

"SELECT open, close FROM times WHERE store_id=x"

This will give you ALL the time ranges associated with the particular store.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.