【问题标题】:PHP Add Up Hours, Minutes, SecondsPHP 将小时、分钟、秒加起来
【发布时间】:2011-05-16 15:19:44
【问题描述】:

给定一系列小时、分钟和秒(例如:01:30:00 或 00:30:00),我想将它们相加并将总和转换为秒。

例如,如果总时间是 02:30:00,总时间应该以秒为单位。

谢谢!

【问题讨论】:

    标签: php


    【解决方案1】:
    $timestr = '00:30:00';
    
    $parts = explode(':', $timestr);
    
    $seconds = ($parts[0] * 60 * 60) + ($parts[1] * 60) + $parts[2];
    

    【讨论】:

    • +1,微优化($parts[0] * 3600) - 乘法很昂贵:)
    【解决方案2】:

    好的。

    基本乘法

    <?php
    
        $time = "01:30:00";
    
        list ($hr, $min, $sec) = explode(':',$time);
    
        $time = 0;
    
        $time = (((int)$hr) * 60 * 60) + (((int)$min) * 60) + ((int)$sec);
    
        echo $time;
    
    ?>
    

    演示:http://codepad.org/PaXcgdNc

    【讨论】:

      【解决方案3】:

      试试这个:

      $string = "1:30:20";
      $components = explode(":", $string);
      $seconds = $components[0]*60*60 + $components[1]*60 + $components[2]
      

      【讨论】:

        【解决方案4】:

        试试

        <?php 
        
        $hour_one = "01:20:20";
        $hour_two = "05:50:20";
        $h =  strtotime($hour_one);
        $h2 = strtotime($hour_two);
        
        $minute = date("i", $h2);
        $second = date("s", $h2);
        $hour = date("H", $h2);
        
        echo "<br>";
        
        $convert = strtotime("+$minute minutes", $h);
        $convert = strtotime("+$second seconds", $convert);
        $convert = strtotime("+$hour hours", $convert);
        $new_time = date('H:i:s', $convert);
        
        echo $new_time;
        
        ?> 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-18
          • 2011-06-13
          • 2021-10-29
          相关资源
          最近更新 更多