查找是否营业:MySQL 小时数计算

发布于 2024-09-24 08:36:06 字数 384 浏览 1 评论 0原文

我有一个存储在 locations 表中的商家列表,该表中存储的是商家开门和关门的时间:

location
`mon_1_open`
`mon_1_closed`
`tue_1_open`
`tue_1_closed`
`wed_1_open`
`wed_1_closed`

等等...

我以完整的小时和分钟存储时间,所以说商家周一营业时间为上午 9:00 至下午 5:30。mon_1_open = '900' AND mon_1_close = '1730'。

如果企业根据一天中的时间开放或关闭,我似乎无法找到一种方法来查找星期几和输出。

有什么建议吗?

I have a list of business stored in a locations table, and stored in that table are hours the business opens and closes:

location
`mon_1_open`
`mon_1_closed`
`tue_1_open`
`tue_1_closed`
`wed_1_open`
`wed_1_closed`

ect...

I store the times in full hours and minutes, so say a business is open from 9:00AM to 5:30PM on monday.. mon_1_open = '900' AND mon_1_closed = '1730'.

I can't seem to figure out a way to find the day of week and output if the business is else open or closed based on the time of day.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-10-01 08:36:06

这不一定能回答您的问题,但从长远来看可能会。

您的数据库方案似乎有缺陷。这绝对没有标准化。我会在它成为一个大问题之前解决这个问题,因为您已经注意到它使得很难找到某些营业时间。这是一个可能更适合的计划草案。

TABLE: locations
    id INT AUTO_INCREMENT PRIMARY KEY
    name VARCHAR(50) 

TABLE: location_hours
    id INT AUTO_INCREMENT PRIMARY KEY
    location_id INT  -  Foreign Key references locations table
    day CHAR(3) - (examples: mon, tue, wed, thu, fri, sat, sun)
    hours VARCHAR(4) - (could also be int)

然后要获取今天的日期,可以在 MySQL 中使用 DATE_FORMAT %a,示例查询:

SELECT locations.name, location_hours.hours 
FROM locations 
    JOIN location_hours ON locations.id = location_hours.location_id 
WHERE location_hours.day = DATE_FORMAT(NOW(), '%a') 
    AND location.name = 'Someway Business'
ORDER BY location_hours.hour

鉴于 ORDER BY,您不应该需要打开/关闭 知道0900 < 1430 因为它是 VARCHAR(尽管 INT 也应该知道如何对其进行排序),但是添加商家时的代码需要更新此记录或者您将需要另一个字段 active 来表示该行是否应在查询中使用。请记住使用 24 小时时间。同样,这是一个模型,我只是当场创建它,因此它可能需要一些改进,但这比像您必须使用当前代码那样进行黑客攻击更好。

更新

解决有关查找是否打开或关闭的评论:

只需使用 PHP date 函数并调用 date('Hi') 这将取出24小时制的当前时间,然后你只需执行一个简单的if语句来查看是否在这之间,如果是,则打开。

IE:

$sql = "SELECT locations.name, location_hours.hours 
FROM locations 
    JOIN location_hours ON locations.id = location_hours.location_id 
WHERE location_hours.day = DATE_FORMAT(NOW(), '%a') 
    AND location.name = 'Someway Business'
ORDER BY location_hours.hour";

$result = mysql_query($sql) or trigger_error("SQL Failed with Error: " . mysql_error());

$times = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($times['open'])) {
        $times['open'] = $row['hours'];
    }else {
        $times['closed'] = $row['hours'];
    }
}

$currentTime = date('Hi');

if ($times['open'] <= $currentTime 
        && $times['closed'] > $currentTime) {
    echo "Open";
}else {
    echo "Closed";
}

鉴于我的逻辑是正确的。再次强调,这只是伪代码的一个使用示例。鉴于我只是当场写下来。上述假设您一次仅查询一家企业。

This does not necessarily answer your question, but it may in the long run.

Your database scheme seems flawed. It definitely is not normalized. I would address that before it becomes a big issue, as you have noticed that it makes it hard to locate certain businesses hours. Here is a draft scheme that might be better suiting.

TABLE: locations
    id INT AUTO_INCREMENT PRIMARY KEY
    name VARCHAR(50) 

TABLE: location_hours
    id INT AUTO_INCREMENT PRIMARY KEY
    location_id INT  -  Foreign Key references locations table
    day CHAR(3) - (examples: mon, tue, wed, thu, fri, sat, sun)
    hours VARCHAR(4) - (could also be int)

Then to get todays date, this can be done in MySQL with DATE_FORMAT %a, an example query:

SELECT locations.name, location_hours.hours 
FROM locations 
    JOIN location_hours ON locations.id = location_hours.location_id 
WHERE location_hours.day = DATE_FORMAT(NOW(), '%a') 
    AND location.name = 'Someway Business'
ORDER BY location_hours.hour

You should not need an open / close given that the the ORDER BY knows that 0900 < 1430 since it is a VARCHAR (although INT should know how to sort it as well), but your code when adding businesses will either need to update this record or you will need another field active to signify if that row should be used in the query. Just remember to use 24 hour time. Again this is a mock up, I just created it on the spot so it probably could use some improvements, but that would be better then doing a hack like you would have to with your current code.

UPDATE

Addressing the comment about finding if it is open or close:

Just use the PHP date function and call date('Hi') this will pull out the current time in 24-hour time, then you just do a simple if statement to see if it is between that, if it is, it is opened.

IE:

$sql = "SELECT locations.name, location_hours.hours 
FROM locations 
    JOIN location_hours ON locations.id = location_hours.location_id 
WHERE location_hours.day = DATE_FORMAT(NOW(), '%a') 
    AND location.name = 'Someway Business'
ORDER BY location_hours.hour";

$result = mysql_query($sql) or trigger_error("SQL Failed with Error: " . mysql_error());

$times = array();
while ($row = mysql_fetch_assoc($result)) {
    if (empty($times['open'])) {
        $times['open'] = $row['hours'];
    }else {
        $times['closed'] = $row['hours'];
    }
}

$currentTime = date('Hi');

if ($times['open'] <= $currentTime 
        && $times['closed'] > $currentTime) {
    echo "Open";
}else {
    echo "Closed";
}

Given that my logic is correct. Again, this is just pseudo code an example of usage. Given I just wrote it up on the spot. The above assumes you are only querying one business at a time.

苏辞 2024-10-01 08:36:06
$dayOfWeek = strtolower(date('D'));

$query = '
    SELECT
        location,
        '.$dayOfWeek.'_1_open <= '.date('Gi').' AND
        '.$dayOfWeek.'_1_closed >= '.date('Gi').' as is_open';

那应该有效。

但是,您确实应该为打开/关闭列使用适当的时间数据类型。

$dayOfWeek = strtolower(date('D'));

$query = '
    SELECT
        location,
        '.$dayOfWeek.'_1_open <= '.date('Gi').' AND
        '.$dayOfWeek.'_1_closed >= '.date('Gi').' as is_open';

That should work.

However, you really should use a proper time datatype for the open/closed columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文