如何在 PHP 中添加、累加、求和时间差异?

问题描述 投票:0回答:1

我正在尝试添加与 php 日期时间对象的差异,但我做错了。我得到了我期望的时差:

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

但是如何添加日期间隔?或者我只是创建一个新的 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");
php time sum datediff
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.