我想能够为多家餐馆储存营业时间,并确定一家商店现在是否营业,但我一直纠结于如何处理午夜后关门的地方。
以下是餐厅营业时间的一个例子:

Monday: 6:00AM-10:00PM
Tuesday: 6:00AM-10:00PM
Wednesday: 6:00AM-10:00PM
Thursday: 6:00AM-10:00PM
Friday: 6:00AM-11:59PM
Saturday: 0:00AM-3:00AM, 6:00AM-11:59PM
Sunday: 0:00AM-3:00AM, 6:00AM-10:00PM

我不确定我应该使用什么类型的模式来表示这一点,这样我就可以确定某个餐厅现在是否营业。
当我设置模式时,我的目标是能够确定任何特定餐厅的以下内容:
餐厅15分钟后是否营业
餐厅45分钟后是否关门
因此,如果当前时间是餐厅开业前15分钟,餐厅应被视为开业;如果当前时间是餐厅关门前45分钟,餐厅应被视为关门,介于两者之间的任何时间都应被视为开业,并考虑到午夜的展期。
以下是一些预期的投入和结果:
Restaurant 1
Monday: 6:00AM-10:00PM
Tuesday: 6:00AM-10:00PM
Wednesday: 6:00AM-10:00PM
Thursday: 6:00AM-10:00PM
Friday: 6:00AM-11:59PM
Saturday: 0:00AM-3:00AM, 6:00AM-11:59PM
Sunday: 0:00AM-3:00AM, 6:00AM-10:00PM

Current day/time -> Result
Monday 12:00PM -> Open
Monday 2:00AM -> Closed
Monday 5:45AM -> Open
Monday 9:30PM -> Closed
Saturday 4:00AM -> Closed
Saturday 11:55PM -> Open
Sunday 2:00AM -> Open
Sunday 2:25AM -> Closed

我不知道该如何处理展期时间,例如确定餐厅是否会在45分钟后的周六晚上11:55营业。
以下是餐厅营业时间的另一个例子:
Monday: 10:00AM-10:00PM
Tuesday: 10:00AM-10:00PM
Wednesday: 10:00AM-10:00PM
Thursday: 10:00AM-10:00PM
Friday: 10:00AM-11:59PM
Saturday: 0:00AM-11:59PM
Sunday: 0:00AM-9:00PM

星期六商店24小时营业,所以我也需要处理这个案子。
餐厅可以全天候营业:
Monday: 0:00AM-11:59PM
Tuesday: 0:00AM-11:59PM
Wednesday: 0:00AM-11:59PM
Thursday: 0:00AM-11:59PM
Friday: 0:00AM-11:59PM
Saturday: 0:00AM-11:59PM
Sunday: 0:00AM-11:59PM

一家餐厅每天可以有0、1、2或更多的时段:
Monday: CLOSED
Tuesday: CLOSED
Wednesday: 10:00AM-2:00PM, 5:00PM-10:00PM
Thursday: 10:00AM-2:00PM, 5:00PM-10:00PM
Friday: 10:00AM-2:00PM, 5:00PM-11:59PM
Saturday: 0:00AM-3:00AM, 10:00AM-2:00PM, 5:00PM-11:59PM
Sunday: 0:00AM-3:00AM, 10:00AM-2:00PM, 5:00PM-9:00PM

因此,我的目标是,对于给定的餐厅及其营业时间,找出当前时间是否介于:
开业前15分钟,以及
在关门前45分钟,
在那天的任何时候
并找到一种存储信息的好方法,以便能够确定多家餐厅的信息。
因为每天可能有0个或多个时段,所以我认为每行存储1个时段是有意义的,但除此之外,我不确定如何存储。

最佳答案

我想我可能会这样储存:

bit_code opens    closes
69       06:00:00 22:00:00
48       06:00:00 03:00:00

我用数字表示了一周中的几天,如下所示:
Monday    -  1
Tuesday   -  2
Wednesday -  4
Thursday  -  8
Friday    - 16
Saturday  - 32
Sunday    - 64

所以,星期五+星期六=48
编辑:
请考虑以下示例:
DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(weekday_code INT NOT NULL
,opens TIME NOT NULL
,closes TIME NOT NULL
);

INSERT INTO my_table VALUES
(69,'06:00:00','22:00:00'),
(48,'06:00:00','03:00:00');

现在是23:30:00。我想下面会告诉我们餐厅一周中哪几天营业(考虑到15分钟和45分钟的规定)。
SELECT *
  FROM my_table
 WHERE CAST('23:30:00' AS TIME) >= opens - INTERVAL 15 MINUTE
   AND CAST('23:30:00' AS TIME) <= (CASE WHEN closes < opens THEN closes + INTERVAL 24 HOUR ELSE closes END) - INTERVAL 45 MINUTE;

+--------------+----------+----------+
| weekday_code | opens    | closes   |
+--------------+----------+----------+
|           48 | 06:00:00 | 03:00:00 |
+--------------+----------+----------+

我们可以在应用程序代码或sql中引入其他技巧来解密“48”周中的哪一天。同样,我们不必这样存储一周中的几天;它看起来非常紧凑,特别是如果有很多餐厅(营业时间相当一致)。
不管怎样,如果采用这种方法…为了好玩/完整…
SELECT a.x
FROM
   ( SELECT  1 x UNION
     SELECT  2 UNION
     SELECT  4 UNION
     SELECT  8 UNION
     SELECT 16 UNION
     SELECT 32 UNION
     SELECT 64
   ) a
JOIN my_table b
  ON a.x = ( a.x & b.weekday_code)
 AND CAST('23:30:00' AS TIME) >= b.opens - INTERVAL 15 MINUTE
 AND CAST('23:30:00' AS TIME) <= (CASE WHEN b.closes < b.opens THEN b.closes + INTERVAL 24 HOUR ELSE b.closes END) - INTERVAL 45 MINUTE;

 +----+
 | x  |
 +----+
 | 16 |
 | 32 |
 +----+

由此我们可以推断,如果今天是星期五或星期六,那么餐馆是营业的。
请注意,可能有一种更优雅的方式用sql来表达这一点,或者在应用程序代码中处理位解码可能更聪明,

关于php - 一种存储营业时间的方法,以确定一个地方现在是否营业,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52025249/

10-12 16:17