Mysql 小时表(字段类型时间
):
opened closed
12:00:00 23:59:00
在 php 中,我需要检查当前时间是否在这两个时间之间。
首先我将 php 当前服务器时间转换为 mysql 时间格式
$cur_time = date('H:m A',$_SERVER['REQUEST_TIME']);
$cur_time = DateTime::createFromFormat( 'H:i A',$cur_time);
$cur_time = $cur_time->format('H:i:s');
然后我比较了时间 -
if($cur_time > $biz_hours['opened'] && $cur_time < $biz_hours['closed'])
注意:$biz_hours
是从mysql表中获取数据的数组。
打印$cur_time
刚刚显示的是09:12:00。但如果 caluse 返回 false。这是实时站点网址:http://dsbangladesh.com/takeout-banani/b/10 。请帮我找出问题所在。
最佳答案
您使用的比较是简单的字符串比较。最好的选择是首先将数据转换为时间戳。像这样:
$curTime = time();
$openTime = strtotime($biz_hours['opened']);
$closeTime = strtotime($biz_hours['closed']);
if($curTime > $openTime && $curTime < $closeTime)
{ ....
我使用time
来获取当前服务器时间,而不是您使用的方法。我还使用 strtotime
将 mysql 时间字段转换为时间戳。
关于php - 比较 php 中的时间与 mysql 时间没有给出正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34338422/