将php中的时间与mysql时间进行比较并没有给出正确的结果
Mysql小时表(字段类型将php中的时间与mysql时间进行比较并没有给出正确的结果,php,mysql,Php,Mysql,Mysql小时表(字段类型time): 在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_t
time
):
在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'])
if($cur_time>$biz_hours['opened']&&$cur_time<$biz_hours['closed']))
注意:$biz_hours
是从mysql表中获取数据的数组
打印$cur\u time
时刚刚显示09:12:00。但是如果caluse返回false。以下是实时站点url:。请帮我找出问题所在 if(time()>strotime($biz_小时['opened'])和&time()if (time() > strtotime($biz_hours['opened']) && time() < strtotime($biz_hours['closed']))
您使用的比较是一个简单的字符串比较。最好先将数据转换为时间戳。大概是这样的:
$curTime = time();
$openTime = strtotime($biz_hours['opened']);
$closeTime = strtotime($biz_hours['closed']);
if($curTime > $openTime && $curTime < $closeTime)
{ ....
$curTime=time();
$openTime=strottime($biz_hours['opened']);
$closeTime=strottime($biz_hours['closed']);
如果($curTime>$openTime&&$curTime<$closeTime)
{ ....
我使用了
time
获取当前服务器时间,而不是您使用的方法。我还使用strotime
将mysql时间字段转换为时间戳。我建议在sql中执行日期/时间检查
<?php
$biz_o = new DateTime(date('H:i:s',strtotime($biz_hours['opened'])));
$biz_c = new DateTime(date('H:i:s',strtotime($biz_hours['closed'])));
$now = new DateTime(date('H:i:s'),strtotime($_SERVER['REQUEST_TIME']));
if($now > $biz_o && $now < $biz_c){
//You got me
}
<?php
$biz_o = new DateTime(date('H:i:s',strtotime($biz_hours['opened'])));
$biz_c = new DateTime(date('H:i:s',strtotime($biz_hours['closed'])));
$now = new DateTime(date('H:i:s'),strtotime($_SERVER['REQUEST_TIME']));
if($now > $biz_o && $now < $biz_c){
//You got me
}