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 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择