Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php中的时间与mysql时间进行比较并没有给出正确的结果_Php_Mysql - Fatal编程技术网

将php中的时间与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

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_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
   }