【问题标题】:How to compare two time in PHP如何在PHP中比较两次时间
【发布时间】:2010-08-04 08:35:48
【问题描述】:

我有两次以 7:30:00 和 22:30:00 的格式分别存储在变量 $resttimefrom$resttimeto 中。

我想检查当前时间是否在这两个值之间。我正在用代码检查这个

$time = date("G:i:s");
if ($time > $resttimefrom and $time < $resttimeto ){
    $stat = "open";
} else {
    $stat = "close";
} 

但我总是将 $stat 设为 Close。是什么原因造成的?

【问题讨论】:

标签: php datetime


【解决方案1】:

你可以试试strtotime

$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 !!";
}

我希望这可以帮助你..

【讨论】:

  • +1 用于帮助但避免错误“注意:现在使用未定义的常量 - 假定为“现在””,将关键字封装在引号中
【解决方案2】:
$today = date("m-d-y ");
$now = date("m-d-y G:i:s");

if (strtotime($today . $resttimefrom) < $now && $now > strtotime($today . $resttimeto)) {
    $stat = 'open';
else
    $stat = 'close

【讨论】:

    【解决方案3】:

    在 PHP 中操作和比较日期和时间的技巧是将日期/时间值存储在一个整数变量中,并使用 mktime()、date() 和 strtotime() 函数。日期/时间的整数表示是自 1970 年 1 月 1 日午夜以来的秒数,称为“纪元”。一旦您的日期/时间采用整数形式,您就可以有效地将其与其他同样采用整数形式的日期进行比较。

    当然,由于您很可能会从页面请求和数据库选择查询中接收日期/时间值,因此您需要先将日期/时间字符串转换为整数,然后才能进行任何比较或算术运算。

    假设您确定 $resttimefrom 和 $resttimeto 变量包含格式正确的时间,您可以使用 strtotime() 函数将字符串时间转换为整数。 strtotime() 接受一个格式化为日期的字符串,并将其转换为自纪元以来的秒数。

    $time_from = strtotime($resttimefrom);
    $time_to = strtotime($resttimeto);
    

    旁注:strtotime() 总是以整数形式返回完整日期。如果您的字符串没有日期,只有时间,strtotime() 将返回今天的日期以及您在字符串中提供的时间。不过,这对您来说并不重要,因为 strtotime() 返回的两个日期将具有相同的日期,并且比较两个变量将具有比较两个时间的预期效果,因为日期相互抵消。

    当您比较两个整数时,请记住日期/时间越早,其整数值就越小。因此,如果您想查看 $time_from 是否早于 $time_to,您可以这样:

    if ($time_from < $time_to)
    {
        // $time_from is ealier than $time_to
    }
    

    现在要将日期/时间与当前系统日期/时间进行比较,只需使用不带参数的 mktime() 来表示当前日期/时间:

    if ($time_from < mktime())
    {
        // $time_from is in the past
    }
    

    【讨论】:

    • 附注不要使用没有参数的 mktime()。虽然这仍然有效,但现在已弃用。使用 time() 获取自纪元以来的秒数。
    【解决方案4】:

    只需将您的日期转换为 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";
    } 
    

    【讨论】:

      【解决方案5】:

      执行此操作的一种简单而聪明的方法是从日期中删除“:”。

      $resttimefrom = 73000;
      $resttimeto = 223000;
      
      $currentTime = (int) date('Gis');
      
      if ($currentTime > $resttimefrom && $currentTime < $resttimeto )
      {
          $stat="open";
      }
      else
      {
          $stat="close";
      } 
      

      【讨论】:

      • 好主意。我正在尝试在没有strtotime 的情况下在 Twig 中执行此操作,并且效果很好。
      【解决方案6】:

      正如 Shrapnel 上校所说,我正在将所有时间转换为秒,然后将其与当前时间的总秒数进行比较

      【讨论】:

        【解决方案7】:

        尝试将它们重新格式化为您可以比较的东西。比如数字:

        $resttimefrom = mktime(7,30,0);
        $resttimeto = mktime(22,30,0);
        
        $time = mktime(date('H'),date('i'),date('s'));
        

        【讨论】:

        • 不考虑夏令时。
        【解决方案8】:

        您正在比较字符串。
        使用 strtotime() 将时间字符串转换为时间戳。
        然后与time()比较。

        【讨论】:

          【解决方案9】:

          date 函数返回一个字符串,因此您进行的比较将是一个字符串比较 - 所以 7:30 将超过 22:30

          最好使用mktime,它会返回一个 Unix 时间戳值(整数),以便更好地进行比较

          $currentTime = mktime();
          $resttimefrom = mktime(hour,min,second);
          

          http://php.net/mktime

          【讨论】:

          • 如上面的答案中所述:如果将时间作为字符串提供给您,那么使用 strtotime 会更容易
          • 当不带参数调用时,mktime() 会抛出 E_STRICT 通知。此外,当 DST 是一个因素时,您的代码可能会导致错误的结果。 mktime 有一个标志来指示 DST,但是从 PHP5.3 开始,如果使用了 is_dst 参数,它也会抛出一个 E_DEPRECATED 通知。
          【解决方案10】:
          $firstTime = '1:07';
          $secondTime = '3:01';
          
          list($firstMinutes, $firstSeconds) = explode(':', $firstTime);
          list($secondMinutes, $secondSeconds) = explode(':', $secondTime);
          
          $firstSeconds += ($firstMinutes * 60);
          $secondSeconds += ($secondMinutes * 60);
          $difference = $secondSeconds - $firstSeconds;
          

          【讨论】:

            【解决方案11】:
            $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;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-11-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-02-27
              • 2018-01-10
              • 1970-01-01
              相关资源
              最近更新 更多