我正在构建一个 api,其中列出了午夜后营业的酒吧列表。我无法确定它们当前是打开还是关闭。
例如,这些酒吧之一的营业时间如下:
Monday
open time = 20:00
closed time = 05:00
Tuesday
open time = 20:00
closed time = 04:00
如果我想知道一个酒吧当前是否开放并且当前时间 04:00(星期一到星期二),这将导致当前酒吧关闭,因为它的星期二和关闭时间 = 04:00
但实际结果应该是酒吧营业到05:00(周一到周二)
我有 20 多个酒吧的列表,我需要知道它们当前是打开还是关闭。这可以在mysql或php中完成吗?我应该如何设置我的数据库表?
我有这个作为我当前的设置
表名称:开放时间字段:
id int(11)
bar_id int(11)
open_time time
close_time time
day tinyint(1)
表记录:
id: 1
bar_id: 1
open_time: 20:00:00
closed_time: 05:00:00
day: 0
id: 2
bar_id: 1
open_time: 20:00:00
closed_time: 04:00:00
day: 1
此查询适用于不超过午夜的开放日
SELECT `open_time`, `closed_time`, IF(CURTIME() BETWEEN `open_time`
AND `closed_time`,'open','closed') AS `status` FROM `openhours`
WHERE `day` = DATE_FORMAT(NOW(), '%w')
但是我该如何处理过去的午夜问题?
它尝试过这样的事情,但不太正确
SELECT open_time, close_time, day, (CASE WHEN ((open_time <= close_time
AND open_time <= CURTIME() AND close_time >= CURTIME()) OR (open_time >= close_time
AND (CURTIME() <= close_time OR CURTIME() >= open_time))) THEN 'open' ELSE 'closed'
END) AS status FROM openhours WHERE bar_id = 2 and CASE WHEN (day = WEEKDAY(NOW())
AND (CURTIME() < open_time)) THEN CASE WHEN (day = (WEEKDAY(NOW()) - 1)
AND (CURTIME() < close_time)) THEN day = (WEEKDAY(NOW()) - 1)
ELSE day = WEEKDAY(NOW()) END ELSE day = WEEKDAY(NOW()) END
提前致谢!