PHP Add Up Hours, Minutes, Seconds

后端 未结 4 1060
清歌不尽
清歌不尽 2020-12-11 22:19

Given a series of hours, minutes, and seconds (ex: 01:30:00 or 00:30:00), I would like to add up each and convert the sum to seconds.

For example, if the total time

相关标签:
4条回答
  • 2020-12-11 22:53

    Try this:

    $string = "1:30:20";
    $components = explode(":", $string);
    $seconds = $components[0]*60*60 + $components[1]*60 + $components[2]
    
    0 讨论(0)
  • 2020-12-11 23:04

    Try

    <?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;
    
    ?> 
    
    0 讨论(0)
  • 2020-12-11 23:12

    OK.

    basic multiplication:

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

    Demo: http://codepad.org/PaXcgdNc

    0 讨论(0)
  • 2020-12-11 23:13
    $timestr = '00:30:00';
    
    $parts = explode(':', $timestr);
    
    $seconds = ($parts[0] * 60 * 60) + ($parts[1] * 60) + $parts[2];
    
    0 讨论(0)
提交回复
热议问题