Ted Sum
Ted Sum

Reputation: 31

PHP Add Up Hours, Minutes, Seconds

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 is 02:30:00, the total time should be in seconds.

Thanks!

Upvotes: 2

Views: 15330

Answers (4)

Rob
Rob

Reputation: 1255

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;

?> 

Upvotes: 0

Sam Magura
Sam Magura

Reputation: 980

Try this:

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

Upvotes: 0

Marc B
Marc B

Reputation: 360562

$timestr = '00:30:00';

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

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

Upvotes: 10

Naftali
Naftali

Reputation: 146302

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

Upvotes: 2

Related Questions