2

I am saving the variables in the format of HH:MM:SS. I want to sum up several variables such as:

TotalTime += var1+var2

It gives me the result of 0, whats the right format for getting the sum as HH:MM:SS?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
user2828251
  • 235
  • 2
  • 7
  • 21
  • Convert them to `DateInterval` and uses its methods for adding intervals. – Barmar May 11 '15 at 10:47
  • Please show your current code which you have – Rizier123 May 11 '15 at 10:47
  • It's unclear what do you exactly need but this could help [http://stackoverflow.com/questions/29695797/sum-time-from-datetime/29765989#29765989](http://stackoverflow.com/questions/29695797/sum-time-from-datetime/29765989#29765989). Maybe this question is also duplicate. – kba May 11 '15 at 11:04
  • So where are we with this question here? – Rizier123 May 11 '15 at 11:23

1 Answers1

2

This should work for you:

Here I just converted the first date into a DateTime object and the second date I converted into a DateInterval object, which I then can add() to the first date.

<?php

    $var1 = "12:23:01";
    $var2 = "05:22:45";

    $date = new DateTime($var1);
    list($hours, $minutes, $seconds) = explode(":", $var2);
    $interval = new DateInterval("PT" . $hours . "H" . $minutes . "M" . $seconds . "S");

    $date->add($interval);
    echo $date->format("H:i:s");

?>

output:

17:45:46
Rizier123
  • 58,877
  • 16
  • 101
  • 156