douduiyun6125 2010-06-18 14:09
浏览 32
已采纳

使用数据库,php,js确定餐厅现在是否已打开(如yelp)

i was wondering if anyone knows how yelp determines what restaurants are "open now"? i'm developing a similar application using html/javascript/php. i was going to have a column in my database for each day, with comma separated hours written in "2243" format (10:43 pm). so for example if a restaurant is open for lunch and dinner it might be "1100,1400,1700,2200". then i'd check (using js) if the current time falls in one of the ranges for the current day. i'd also like to be able to determine if a restaurant is "open tonight", "open late", etc. for those i guess i'd check whether the open range overlaps with certain ranges.

is there a better way to do this? particularly, how to store the hours in the database and then determine if they overlap with a given set of hours.

thanks.

  • 写回答

3条回答 默认 最新

  • dongyu9894 2010-06-18 14:25
    关注

    You definitely want to redesign the database. Putting multiple values into a single column like that is a huge violation of the rules of normalization and will cause lots of headaches down the road. A single column should always hold a single piece of information.

    You should also be using proper data types. Don't put times in a string, because you could end up with "foo" as a time and then what do you do?

    Instead, what you probably want is:

    CREATE TABLE Restaurants
    (
        restaurant_id    INT            NOT NULL,
        restaurant_name  VARCHAR(40)    NOT NULL,
        CONSTRAINT PK_Restaurants PRIMARY KEY CLUSTERED (restaurant_id)
    )
    CREATE TABLE Restaurant_Hours
    (
        restaurant_id    INT         NOT NULL,
        hours_id         INT         NOT NULL,
        day_of_week      SMALLINT    NOT NULL,
        start_time       TIME        NOT NULL,  -- Depends on your RDBMS and which date/time datatypes it supports
        end_time         TIME        NOT NULL,
        CONSTRAINT PK_Restaurant_Hours PRIMARY KEY CLUSTERED (restaurant_id, hours_id)
    )
    

    You can then easily check for restaurants open at a given time:

    SELECT
        R.restaurant_id,
        R.restaurant_name
    FROM
        Restaurants R
    WHERE
        EXISTS
        (
            SELECT *
            FROM
                Restaurant_Hours RH
            WHERE
                RH.restaurant_id = R.restaurant_id AND
                RH.start_time <= @time AND
                RH.end_time >= @time AND
                RH.day_of_week = @day_of_week
        )
    

    If you have a time slot that spans midnight you would need to have two rows - one for the first day, and one for midnight - "x" for the next day. Also, remember to keep time zones in mind when using from a GUI.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序