如何在PHP中比较两个时间
我在变量如何在PHP中比较两个时间,php,datetime,Php,Datetime,我在变量$resttimefrom和$resttimeto中分别存储了两次类似7:30:00和22:30:00的格式 我想检查当前时间是否在这两个值之间。我正在用代码检查这个 $time = date("G:i:s"); if ($time > $resttimefrom and $time < $resttimeto ){ $stat = "open"; } else { $stat = "close"; } $time=日期(“G:i:s”); 如果($tim
$resttimefrom
和$resttimeto
中分别存储了两次类似7:30:00和22:30:00的格式
我想检查当前时间是否在这两个值之间。我正在用代码检查这个
$time = date("G:i:s");
if ($time > $resttimefrom and $time < $resttimeto ){
$stat = "open";
} else {
$stat = "close";
}
$time=日期(“G:i:s”);
如果($time>$resttimefrom和$time<$resttimeto){
$stat=“打开”;
}否则{
$stat=“关闭”;
}
但我总是把$stat设置得尽可能接近。是什么原因造成的?您正在比较字符串。
使用将时间字符串转换为时间戳。
然后比较。
$today=date(“m-d-y”);
$now=日期(“m-d-y G:i:s”);
if(strotime($today.$resttimefrom)<$now&&$now>strotime($today.$resttimeto)){
$stat='打开';
其他的
$stat='关闭
函数返回一个字符串,因此您进行的比较将是一个字符串比较-因此7:30将大于22:30
最好使用mktime
,它将返回一个Unix时间戳值(整数),以便进行更好的比较
$currentTime = mktime();
$resttimefrom = mktime(hour,min,second);
尝试将它们重新格式化为可以进行类似比较的格式。例如,数字:
$resttimefrom = mktime(7,30,0);
$resttimeto = mktime(22,30,0);
$time = mktime(date('H'),date('i'),date('s'));
只需将日期转换为Unix时间戳,进行比较,就可以得到结果!可能如下所示:
$time =date("G:i:s");
$time1 = strtotime($time);
$resttimefrom1 = strtotime($resttimefrom );
$resttimeto1 = strtotime($resttimeto );
if ($time1 >$resttimefrom and $time1 <$resttimeto)
{
$stat="open";
}
else
{
$stat="close";
}
$time=日期(“G:i:s”);
$time1=strottime($time);
$resttimefrom 1=strtotime($resttimefrom);
$resttimeto1=strtotime($resttimeto);
如果($time1>$resttimefrom和$time1一个简单而聪明的方法是从日期中删除“:”
$resttimefrom = 73000;
$resttimeto = 223000;
$currentTime = (int) date('Gis');
if ($currentTime > $resttimefrom && $currentTime < $resttimeto )
{
$stat="open";
}
else
{
$stat="close";
}
$resttimefrom=73000;
$resttimeto=223000;
$currentTime=(int)日期('Gis');
如果($currentTime>$resttimefrom&&$currentTime<$resttimeto)
{
$stat=“打开”;
}
其他的
{
$stat=“关闭”;
}
在PHP中操作和比较日期和时间的诀窍是将日期/时间值存储在整数变量中,并使用mktime()、date()和strotime()函数。日期/时间的整数表示是自1970年1月1日午夜以来的秒数,称为“历元”。一旦日期/时间为整数形式,您就可以将其与同样为整数形式的其他日期进行有效比较
当然,因为您很可能会从页面请求和数据库选择查询中接收日期/时间值,所以在执行任何比较或算术之前,您需要将日期/时间字符串转换为整数
假设您确信$resttimefrom和$resttimeto变量包含格式正确的时间,则可以使用strtotime()函数将字符串时间转换为整数。strtotime()接受格式为日期的字符串,并将其转换为自历元起的秒数
$time_from = strtotime($resttimefrom);
$time_to = strtotime($resttimeto);
旁注:strotime()总是以整数形式返回完整日期。如果字符串没有日期,则只返回时间,strotime()将返回今天的日期以及您在字符串中给出的时间。不过,这对您并不重要,因为strotime()返回的两个日期将具有相同的日期,比较这两个变量将产生预期的效果,即比较两次,因为日期相互抵消
在比较这两个整数时,请记住日期/时间越早,其整数值越小。因此,如果要查看$time\u from是否早于$time\u to,可以使用以下命令:
if ($time_from < $time_to)
{
// $time_from is ealier than $time_to
}
if($time\u from<$time\u to)
{
//$time\u from早于$time\u to
}
现在,要将日期/时间与当前系统日期/时间进行比较,只需使用不带参数的mktime()来表示当前日期/时间:
if ($time_from < mktime())
{
// $time_from is in the past
}
if($time\u from
正如榴弹上校所说,我将所有时间转换为秒,然后将其与当前时间的总秒数进行比较您可以尝试使用strotime
$st_time = strtotime($resttimefrom);
$end_time = strtotime($resttimeto);
$cur_time = strtotime(now);
然后检查
if($st_time < $cur_time && $end_time > $cur_time)
{
echo "WE ARE CLOSE NOW !!";
}
else{
echo "WE ARE OPEN NOW !!";
}
if($st_time<$cur_time&$end_time>$cur_time)
{
回声“我们现在很接近!!”;
}
否则{
回声“我们现在开门!!”;
}
我希望这会对您有所帮助。如上面的答案所述:如果时间是以字符串形式提供给您的,那么在没有参数的情况下调用strotime,mktime()会更容易使用
抛出一个E_STRICT
通知。此外,当DST是一个因素时,您的代码可能会导致错误结果。mktime
有一个标志指示DST,但从PHP5.3开始,如果使用is_DST
参数,它也会抛出一个E_DEPRECATED
通知。不考虑DST。请注意不要使用mktime()没有参数。虽然这仍然有效,但它现在已被弃用。使用time()获取自epoch以来的秒数。它可能会重复,因为$resttimefrom和$resttimeto没有定义好主意。我尝试在Twig中不使用strtotime
来执行此操作,效果很好。+1有助于避免错误注意:使用未定义的常量now-假定为'now',将关键字封装到引号中
$Time1 = date_parse($time);
$seconds1 = $Time1['hour'] * 3600 + $Time1['minute'] * 60 + $Time1['second'];
$Time2 = date_parse($current_time);
$seconds2 = Time2['hour'] * 3600 + Time2['minute'] * 60 + Time2['second'];
$actula_time = $seconds1 - $seconds2;
echo floor($actula_time / 3600) .":". floor(($actula_time / 60)%60) .":". $actula_time%60;
if($st_time < $cur_time && $end_time > $cur_time)
{
echo "WE ARE CLOSE NOW !!";
}
else{
echo "WE ARE OPEN NOW !!";
}
$Time1 = date_parse($time);
$seconds1 = $Time1['hour'] * 3600 + $Time1['minute'] * 60 + $Time1['second'];
$Time2 = date_parse($current_time);
$seconds2 = Time2['hour'] * 3600 + Time2['minute'] * 60 + Time2['second'];
$actula_time = $seconds1 - $seconds2;
echo floor($actula_time / 3600) .":". floor(($actula_time / 60)%60) .":". $actula_time%60;