【问题标题】:PHP: Work out duration between two timesPHP:计算两次之间的持续时间
【发布时间】:2009-02-10 21:50:09
【问题描述】:

好的,一个非常简单的问题,但我太厚了,无法弄清楚。我想得到两次之间的差异。例如,“1:07”(1 分 7 秒)和“3:01”(3 分 1 秒)。它只会是几分钟和几秒钟。我一直在尝试利用这个:

function timeDiff($firstTime,$lastTime)
{

// convert to unix timestamps
$firstTime=strtotime($firstTime);
$lastTime=strtotime($lastTime);

// perform subtraction to get the difference (in seconds) between times
$timeDiff=$lastTime-$firstTime;

// return the difference
return $timeDiff;
}

但我觉得我跑错了方向?

感谢您的帮助。

编辑

我试过这个:echo timeDiff('1:07','2:30');

我得到了这个输出“4980”

上面是什么?是秒吗?我不知道如何将其设为“1:23”,这就是区别。

编辑 2

谢谢大家,我从这个线程中学到了很多东西,尤其是。保罗的。效果很好,我喜欢防御性!

【问题讨论】:

    标签: php


    【解决方案1】:

    您不能使用 strtotime,因为它会将 MM:SS 解释为 HH:MM - 这就是您获得比预期更高的值的原因。

    您可以简单地在您的 MM:SS 值前面加上“00:”,使它们看起来像 HH:MM:SS。

    但是请注意,strtotime,如果只给出 HH:MM:SS,将给出 今天 的时间戳,这对于一次性代码来说很好。不要将这种技术用于任何重要的事情,考虑一下如果您对 strtotime 的两次调用跨越午夜会发生什么!

    或者,这样的事情会将 MM:SS 值转换为时间戳,您可以对其进行算术运算

    function MinSecToSeconds($minsec)
    {
        if (preg_match('/^(\d+):(\d+)$/', $minsec, $matches))
        {
            return $matches[1]*60 + $matches[2];
        }
        else
        {
            trigger_error("MinSecToSeconds: Bad time format $minsec", E_USER_ERROR);
            return 0;
        }
    }
    

    它比使用爆炸更具防御性,但显示了另一种方法!

    【讨论】:

    • 谢谢。完美运行。如果将来有人需要它,也许您应该为 if 函数添加一个额外的右括号。 :)
    【解决方案2】:

    这应该以秒为单位为您提供两次之间的差异。

    $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;
    

    【讨论】:

      【解决方案3】:

      使用找到的代码here,自己计算秒数怎么样:

      <?php
      
      function time_to_sec($time) {
          $hours = substr($time, 0, -6);
          $minutes = substr($time, -5, 2);
          $seconds = substr($time, -2);
      
          return $hours * 3600 + $minutes * 60 + $seconds;
      }
      
      function sec_to_time($seconds) {
          $hours = floor($seconds / 3600);
          $minutes = floor($seconds % 3600 / 60);
          $seconds = $seconds % 60;
      
          return sprintf("%d:%02d:%02d", $hours, $minutes, $seconds);
      } 
      
      
      function timeDiff($firstTime,$lastTime)
      {
      
      // convert to seconds
      $firstTime=time_to_sec($firstTime);
      $lastTime=time_to_sec($lastTime);
      
      // perform subtraction to get the difference (in seconds) between times
      $timeDiff=$lastTime-$firstTime;
      
      // return the difference
      return sec_to_time($timeDiff);
      }
      
      echo timeDiff("1:07", "3:01");
      
      
      ?>
      

      返回 0:01:54

      【讨论】:

        【解决方案4】:

        strtotime 函数用于将字符串格式的日期(精确到秒) 转换为 Unix Timestamp 样式格式。它不适用于将时间格式的字符串转换为任何内容。看看这两个陈述是否有助于您理解任何内容

        //recent php5 for DATE_RFC822
        echo date(DATE_RFC822,strtotime('1:07'));
        echo date(DATE_RFC822,strtotime('3:01'));   
        

        听起来你想要这样的东西

        function timeDiff($firstTime,$lastTime)
        {
            //split minutes and seconds
            list($min_one, $sec_one) = preg_split('{:}',$firstTime,2, PREG_SPLIT_NO_EMPTY);
            list($min_two, $sec_two) = preg_split('{:}',$lastTime ,2, PREG_SPLIT_NO_EMPTY);
        
            //convert times into seconds
            $time_one = $min_one * 60 + $sec_one;
            $time_two = $min_two * 60 + $sec_two;
        
            //return times
            return $time_two - $time_one;
        }
        
        echo timeDiff('1:07','3:10');
        

        【讨论】:

          【解决方案5】:
          $time1 = date("H:i",strtotime($strtime));
          $time2 = date("H:i");
          list($hours, $minutes) = explode(':', $time1);
          $startTimestamp = mktime($hours, $minutes);
          
          list($hours, $minutes) = explode(':', $time2);
          $endTimestamp = mktime($hours, $minutes);
          
          $seconds = $endTimestamp - $startTimestamp;
          $minutes = ($seconds / 60) % 60;
          $hours = floor($seconds / (60 * 60));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-28
            • 2020-07-17
            • 1970-01-01
            • 2017-11-15
            相关资源
            最近更新 更多