phpmysqltimestamp

Best way to handle business opening time in a year PHP and MySQL


We have developed a website for pharmacies in which we show the opened pharmacies in a specific city.

I handle this with a field named "timestamps" in which it is stored all timestamps of opening hour every 15 minutes for a period of about 3 months. For example if every days the pharmacy is open from 8:00 to 19:00 there is a range of timestamps from one time to another, with an interval of 15 minutes.

While in the frontend we have a list of opened pharmacies and I could show opened ones by querying the database with for example: "WHERE timestamps LIKE('%1449820800%')" where the timestamp is the current time rounded to the nearest quarter hour.

The question is: considering the time ranges are different from week to week, is there a better way to handle this situation? Also because we have 25.000 users and the website is slow if we have large amount of timestamps.

Thank you in advance!


Solution

  • You could just have a database with each day of openning for every store :

    -----------------------------------------
    | StoreId  | Day | HourOpen | HourClose |
    =========================================
    |        1 |   1 | 8:30     | 21:15     |
    -----------------------------------------
    |        1 |   2 | 9:00     | 17:00     |
    -----------------------------------------
    |        2 |   1 | 10:00    | 12:30     |
    -----------------------------------------
    |        2 |   1 | 14:00    | 19:00     |
    =========================================
    

    In this table, the day represent the day of the week (1 for monday, 2 for tuesday for example) and then you just have to parameter the openniong hours for each store only once.

    You can then query this table to see if a store is open for a day of the week at the very moment.

    If a pharmacy has an exceptionnal closure or openning hours for a day, i suggest an ovveride table like this one

    ----------------------------------------------------------
    | StoreId  | Date        | isOpen | HourOpen | HourClose |
    ==========================================================
    |        1 | 2015-12-20  |  true  | 10:00    | 16:00     |
    ----------------------------------------------------------
    |        2 | 2015-12-20  | false  | null     | null      |
    ==========================================================
    

    This way, you can check first if an pharmacy has any record in this table for the current day (not depending of the time) if it does, you check if there is an opening. If there is not any entry in the override table, you check with the first table.

    You also can ahve a hour model table with opening and closing time, a day table, and an associative table that creates relations between stores, hours and days.