【问题标题】:Calculations with strings of numbers which are times (HH:MM:SS)使用数字字符串进行计算,即时间 (HH:MM:SS)
【发布时间】:2011-07-28 13:15:21
【问题描述】:

我想知道是否可以在 PHP/Codeigniter 中减去时间格式为

的值

HH:MM:SS

例如:

$time1 = "12:45:03";
$time2 = "14:03:48";

$timelength = $time2- $time1;

有任何建议或代码示例链接吗?

【问题讨论】:

    标签: php string


    【解决方案1】:

    这就像

    //function to convert seconds into hour:minute:second
    function sec2hms ($sec, $padHours = false) 
    {
    
        // start with a blank string
        $hms = "";
    
         // do the hours first: there are 3600 seconds in an hour, so if we divide
         // the total number of seconds by 3600 and throw away the remainder, we're
         // left with the number of hours in those seconds
         $hours = intval(intval($sec) / 3600); 
    
         // add hours to $hms (with a leading 0 if asked for)
         $hms .= ($padHours) 
         ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":"
         : $hours. ":";
    
         // dividing the total seconds by 60 will give us the number of minutes
         // in total, but we're interested in *minutes past the hour* and to get
         // this, we have to divide by 60 again and then use the remainder
         $minutes = intval(($sec / 60) % 60); 
    
         // add minutes to $hms (with a leading 0 if needed)
         $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":";
    
        // seconds past the minute are found by dividing the total number of seconds
        // by 60 and using the remainder
        $seconds = intval($sec % 60); 
    
        // add seconds to $hms (with a leading 0 if needed)
        $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
    
       // done!
       return $hms;
    
     }
    
    $subtracted_time = strtotime($time2) - strtotime($time1); //gives difference in seconds
    echo(sec2hms($subtracted_time));
    

    函数sec2hms源http://www.laughing-buddha.net/php/lib/sec2hms/

    【讨论】:

    • 使用此代码,使用问题中提供的值,$subtracted_time 将得到10:12:45 作为结果,这与时差不对应!如果我们只运行第一行,我们将得到正确的值,以秒为单位。
    • m in date 函数用于 MONTH。 i 应该使用几分钟。无论如何,在这种情况下不应使用date。它会考虑您的计算机时间区域设置 - 结果会因系统而异。
    • 我很不小心发布了答案。现在这是一个经过测试的代码。
    【解决方案2】:

    我会使用strtotime() 函数将时间转换为 UNIX 时间值。然后,您可以减去这两个值,因为它们将是整数,然后使用 date() 函数以您想要的方式格式化差异。

    【讨论】:

    • date() 函数需要一个时间戳参数,即“以 Unix 纪元(格林威治标准时间 1970 年 1 月 1 日 00:00:00)以来的秒数为单位测量的时间”,不适用于此。它考虑时间区域设置。在我的机器中,echo date('H:i:s', 0); 输出 21:00:00,因为我的语言环境是 GMT - 3。
    • 啊,我没想到。谢谢!
    【解决方案3】:

    这是我的建议(工作 PHP 代码):

    $time1 = '12:45:03';
    $time2 = '14:03:48';
    $timelength = strtotime( $time2 ) - strtotime( $time1 );
    
    $hours = intval( $timelength / 3600 );
    $minutes = intval( ( $timelength % 3600 ) / 60 );
    $seconds = $timelength % 60;
    
    echo str_pad( $hours, 2, '0', STR_PAD_LEFT ) . ':' . str_pad( $minutes, 2, '0', STR_PAD_LEFT ) . ':' . str_pad( $seconds, 2, '0', STR_PAD_LEFT );
    

    输出:

    01:18:45

    【讨论】:

      【解决方案4】:

      使用以下对您有帮助的方法

      function calculate_time_past($start_time, $end_time, $format = "s") { 
              $time_span = strtotime($end_time) - strtotime($start_time); 
              if ($format == "s") 
              { // is default format so dynamically calculate date format 
                  if ($time_span > 60) { $format = "i:s"; } 
                  if ($time_span > 3600) { $format = "H:i:s"; } 
              } 
              return gmdate($format, $time_span); 
      } 
      
      $tdiff=calculate_time_past($time1, $time2, "H:i:s"); // will output 00:02:45 when format is overridden 
      

      【讨论】:

        【解决方案5】:

        作为 BenGC 答案的补充

        $tmp_time1 = strtotime($submit_date);
        $tmp_time2 = strtotime($submit_date);
        $timelength = $tmp_time2 - $tmp_time1;
        

        应该可以,我没有测试过,但我使用的代码非常相似。您将获得两次之间的秒数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-29
          • 1970-01-01
          • 2020-01-12
          • 1970-01-01
          • 1970-01-01
          • 2018-08-23
          相关资源
          最近更新 更多