PHP Adding Hours, Minutes, Seconds

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

For example, if the total time is 02:30:00, the total time should be in seconds.

Thank!

+3
source share
4 answers
$timestr = '00:30:00';

$parts = explode(':', $timestr);

$seconds = ($parts[0] * 60 * 60) + ($parts[1] * 60) + $parts[2];
+8
source

OK

base 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

+2
source

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;

?> 
+1
source

Try the following:

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

All Articles