简体   繁体   English

MySQL-如果在午夜之前从昨天获取数据

[英]MySQL - grab data from yesterday if past midnight

I got a database that contains the daily dish , opening time and closing time . 我得到了一个数据库,其中包含每日菜肴开放时间关闭时间
What I'm looking for is a way to check if the time is within these times, for example: 我正在寻找一种检查时间是否在这些时间内的方法,例如:
Opening time: 14:00 营业时间: 14 00
Closing time: 02:00 <-- (Pay attention to this, it's past 00:00) 关闭时间: 02 : 00 <-(请注意,已过00:00)

If the times could just be from 00:00 to 23:59, this problem would be super easy to solve (I would just check if the current time for the current day is within these times). 如果时间可能只是从00:00到23:59,那么这个问题将非常容易解决(我只是检查当天的当前时间是否在这些时间之内)。
But that is not the case, because the day has changed, so the times from yesterday wont be checked, meaning the restaurant that closed at 02:00 will not be returned. 但是事实并非如此,因为日期已经改变,所以不会检查昨天的时间,这意味着在02:00关闭的餐厅将不予退还。

Here is how I declare the names of the days opening/closing time in PHP, for the query: 这是我在查询中用PHP声明打开/关闭时间的天数的名称:

$day_open = array('d1_o', 'd2_o', 'd3_o', 'd4_o', 'd5_o', 'd6_o', 'd7_o');
$day_close = array('d1_c', 'd2_c', 'd3_c', 'd4_c', 'd5_c', 'd6_c', 'd7_c');

// This is the name of the week days dishes
$dish_dd_array = array('d1_t', 'd2_t', 'd3_t', 'd4_t', 'd5_t', 'd6_t', 'd7_t');

$weekday = date('N'); // This returns 1 for monday, 5 for friday etc..
$current_time = date("H:i", time()); 

$query = mysql_query("SELECT {$dish_dd_array[$weekday-1]} FROM restaurants WHERE (({$day_open[$weekday-1]} < {$day_close[$weekday-1]} AND '{$current_time}' BETWEEN {$day_open[$weekday-1]} AND {$day_close[$weekday-1]}) OR ".
    "({$day_open[$weekday-1]} > {$day_close[$weekday-1]} AND ('{$current_time}' > {$day_open[$weekday-1]} OR '{$current_time}' < {$day_close[$weekday-1]})))");

The query is kind of long so I will explain it here aswell: 该查询有点长,所以我也会在这里解释一下:
It just checks if the current time is within the current days opening/closing time in the database. 它只是检查当前时间是否在数据库中当前日期的打开/关闭时间之内。 If it is, it will then return the dish. 如果是这样,它将返回盘子。

Just to clarify, this code works flawlessly if the time range is within 00:00 and 23:59 for the same day, but I want to support even times efter 24:00 so night restaurants can be visible too! 需要澄清的是,如果时间范围在同一天的00:00和23:59之间,则此代码可以完美地工作,但是我想在24:00之后支持甚至更晚的时间,这样夜店也可以看到!

Thanks in advance, 提前致谢,
Tompa 通帕

What I did here is add a condition if the closing time is 我在这里所做的是,如果关闭时间为

AM or PM 上午下午

If it is AM the closing is that time the next day . 如果是上午,则隔天是第二天的时间。 When PM consider it as the same day. 当下午考虑为同一天。 It is very RARE to have a closing time in AM at the same day. 在同一天的上午关闭时间非常罕见

Or ELSE you should add a FLAG ( a column on your database with true or false value or on your settings file ) to determine if the closing time is on the same or next day. 否则,您应该添加一个FLAG (数据库中具有true或false值或设置文件中的列)以确定关闭时间是在同一天还是第二天。

Keep in mind that midnight is an arbitrary distinction for the benefit of us mere humans. 请记住,午夜是对我们仅仅是人类的利益的任意区分。 There is no such thing as midnight in a time interval. 在时间间隔中没有午夜之类的东西。 So if the time we are looking at is at or after the opening time on Monday and before the opening time on Tuesday, we can safely assume it was during the Monday business hours. 因此,如果我们正在寻找的时间是在星期一的开放时间或之后,并且在星期二的开放时间之前,我们可以放心地假设它是在星期一的工作时间内。 You don't even need the closing time. 您甚至不需要关闭时间。

WHERE   '{$current_time}' >= {$day_open[$weekday-1]} 
    AND '{$current_time}' < {$day_open[$weekday]})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM