0

我正在构建一个 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

提前致谢!

4

3 回答 3

0

你想要这样的东西

case closed < open
 not between open and closed
else 
 between open and closed 

当任一返回 true 时,栏打开

于 2013-11-01T20:17:00.917 回答
0

您可以将 UNIX_TIMESTAMP() 用于此类问题:

SELECT `open_time`, `closed_time`, IF(UNIX_TIMESTAMP(CURTIME()) BETWEEN UNIX_TIMESTAMP(`open_time`) AND UNIX_TIMESTAMP(`closed_time`),'open','closed') AS `status` FROM `openhours` WHERE `day` = DATE_FORMAT(NOW(), '%w')

我希望我明白你的意思:)

于 2013-10-30T08:01:15.883 回答
0

一种选择是存储一周开始之后的秒数,这样您就完全不用担心午夜了。

将这些添加到您的表中:

open_time_i   int(11)
closed_time_i int(11)

并使您的数据看起来像这样:

id: 1
bar_id: 1
open_time: 20:00:00
open_time_i: 72000
closed_time: 05:00:00
closed_time_i: 104400
day: 0

id: 2
bar_id: 1
open_time: 20:00:00
open_time_i: 158400
closed_time: 04:00:00
closed_time_i: 187200
day: 1

然后您的查找可能如下所示:

$seconds = time() - strtotime('this week last sunday', date("Y-m-d", time()))

SELECT `open_time`, `closed_time`, IF(
  $seconds BETWEEN `open_time_i` AND `closed_time_i`
    OR
  (("closed_time_i" >= 604800 AND $seconds BETWEEN 0 AND ("closed_time_i" - 604800)))
,'open','closed') AS `status` FROM `openhours`

第二个 between 语句检查酒吧是否在该周变化之后开放。

于 2014-03-29T15:18:09.063 回答