查找是否营业:MySQL 小时数计算
我有一个存储在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不一定能回答您的问题,但从长远来看可能会。
您的数据库方案似乎有缺陷。这绝对没有标准化。我会在它成为一个大问题之前解决这个问题,因为您已经注意到它使得很难找到某些营业时间。这是一个可能更适合的计划草案。
然后要获取今天的日期,可以在 MySQL 中使用 DATE_FORMAT
%a
,示例查询:鉴于
ORDER BY,您不应该需要打开/关闭
知道0900 < 1430
因为它是VARCHAR
(尽管INT
也应该知道如何对其进行排序),但是添加商家时的代码需要更新此记录或者您将需要另一个字段active
来表示该行是否应在查询中使用。请记住使用 24 小时时间。同样,这是一个模型,我只是当场创建它,因此它可能需要一些改进,但这比像您必须使用当前代码那样进行黑客攻击更好。更新
解决有关查找是否打开或关闭的评论:
只需使用 PHP
date
函数并调用date('Hi')
这将取出24小时制的当前时间,然后你只需执行一个简单的if语句来查看是否在这之间,如果是,则打开。IE:
鉴于我的逻辑是正确的。再次强调,这只是伪代码的一个使用示例。鉴于我只是当场写下来。上述假设您一次仅查询一家企业。
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.
Then to get todays date, this can be done in MySQL with DATE_FORMAT
%a
, an example query:You should not need an open / close given that the the
ORDER BY
knows that0900 < 1430
since it is aVARCHAR
(althoughINT
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 fieldactive
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 calldate('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:
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.
那应该有效。
但是,您确实应该为打开/关闭列使用适当的时间数据类型。
That should work.
However, you really should use a proper time datatype for the open/closed columns.