如何在PHP中添加,累积,求和时差?

时间:2014-01-06 22:00:37

标签: php datediff

我试图添加php日期时间对象的差异,我做错了。我得到了我期望的时差:

$ total = 00:11:33 $ accumulator = 00:05:47

但是如何添加DateIntervals?或者我只是创建一个新的dateTime对象并添加到它?

$total = new DateInterval('P0000-00-00T00:00:00');
$accumulator = new DateInterval('P0000-00-00T00:00:00');
$a = new DateTime('2014-01-05 00:01:38');
$b = new DateTime('2014-01-05 00:13:11');
$c = new DateTime('2014-01-05 00:18:58');
$d = new DateTime('2014-01-05 00:24:45');

$total = $a->diff($b);
$accumulator =  $c->diff($d);

echo '$total = ' . $total->format("%H:%I:%S");
echo '$accumulator = ' . $accumulator->format("%H:%I:%S");

$total += $accumulator;

echo 'Grand Total = ' . $total->format("%H:%I:%S");

1 个答案:

答案 0 :(得分:0)

也许你想使用Carbon扩展。 https://github.com/briannesbitt/Carbon