1

I have two DateTime object.

$time1 = new DateTime('01:04:00');
$time2 = new DateTime('00:13:22');

Addition of this two will be : 01:17:22. How can I do it?

Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44

3 Answers3

4

A "time of day" is not the same thing as a "duration of time". It doesn't makes sense to add together two time of day values - regardless of platform or language. Think about it - what does "11:00 PM" + "4:00 AM" equal? It's a nonsensical question.

You should be thinking about PHP's DateInterval class, not the DateTime class.

It should be noted that if you follow the examples on the dup posts of using strtotime it will work only when each individual input, and the final result, are all under 24 hours. Why? Because that's the maximum amount of time allowed in a standard day. That's the consequence of mixing "time of day" with "duration of time".

This should work for you:

function time_to_interval($time) {
    $parts = explode(':',$time);
    return new DateInterval('PT'.$parts[0].'H'.$parts[1].'M'.$parts[2].'S');
}

function add_intervals($a,$b) {
    $zerodate = new DateTime('0000-01-01 00:00:00');
    $dt = clone $zerodate;
    $dt->add($a);
    $dt->add($b);
    return $zerodate->diff($dt);
}

function format_interval_hhmmss($interval){
    $totalhours = $interval->h + ($interval->d * 24);
    return $totalhours.$interval->format(':%I:%S');
}

$interval1 = time_to_interval('01:04:00');
$interval2 = time_to_interval('00:13:22');
$interval3 = add_intervals($interval1,$interval2);

echo format_interval_hhmmss($interval3);

Note that the choice of value for $zerodate isn't really all that important. It's just that some reference point is required, since PHP doesn't provide operations directly on DateInterval.

Also note that the the DateInterval::format function doesn't have a formatter to get you total number of hours inclusive of days, so if there's any chance the total could be 24 hours or more, then you have to format that part yourself, like I showed in the format_interval_hhmmss function.

Also note that my PHP skills are not all that great, so there may be a more efficient way to write these functions.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
2
function addtime($time1,$time2)
{
    $x = new DateTime($time1);
    $y = new DateTime($time2);

    $interval1 = $x->diff(new DateTime('00:00:00')) ;
    $interval2 = $y->diff(new DateTime('00:00:00')) ;

    $e = new DateTime('00:00');
    $f = clone $e;
    $e->add($interval1);
    $e->add($interval2);
    $total = $f->diff($e)->format("%H:%I:%S");
    return $total;
}
Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44
1

The only built-in DateTime add/substract methods require using a DateInterval. e.g

$t1 = new DateTime('01:04:33');
$new = $t1->add(new DateInterval('PT13M22S'));
                                    ^^^^^^---13 minutes, 22 seconds

however, note that since DateTime works on DATES as well as times, you can't just slam together two times like this and expect to get reliable results. Consider what happens if you're doing the addition on an interval that happens to span a daylight savings border, or crosses over a day boundary, etc...

Marc B
  • 356,200
  • 43
  • 426
  • 500