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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度