我正在使用 PHP 处理时间记录实验中的一些数字。如何将两个 DateInterval 对象添加在一起?
其中一个 DateIntervals 是这样找到的(两个 DateTime 对象之间的差异):
$start_stamp = new \DateTime( '2017-09-01T07:10' ); // DateTime-object
$end_stamp = new \DateTime( '2017-09-01T07:10' ); // DateTime-object
$duration = $start_stamp->diff( $end_stamp ); // DateInterval-object
另一个 DateInterval 简单地定义如下:
$abc = new \DateInterval( 'PT1H' ); // DateInterval with duration of 1 hour
我读过一些 places ,应该将我的 DateIntervals 转换为 DateTime 对象,然后使用 $dateTimeObject->add(...)
函数。但这对我来说似乎很愚蠢。这是一个持续时间,-那么为什么我要把它转换成它不是的东西,以便正确处理它。
我写了下面的函数来解决这个问题,但它看起来很笨拙。可以写得更好吗?或者有更好的方法来总结两个 DateInterval 对象吗?
function sumDateIntervals($dateinterval_one, $dateinterval_two)
{
$d1_years = $dateinterval_one->y;
$d1_months = $dateinterval_one->m;
$d1_days = $dateinterval_one->d;
$d1_hours = $dateinterval_one->h;
$d1_minutes = $dateinterval_one->i;
$d1_seconds = $dateinterval_one->s;
$d1_float = $dateinterval_one->f;
$d2_years = $dateinterval_two->y;
$d2_months = $dateinterval_two->m;
$d2_days = $dateinterval_two->d;
$d2_hours = $dateinterval_two->h;
$d2_minutes = $dateinterval_two->i;
$d2_seconds = $dateinterval_two->s;
$d2_float = $dateinterval_two->f;
$return_dateinterval = new DateInterval(
'P' . ( string ) ( $d1_years + $d2_years ) . 'Y'
. ( string ) ( $d1_months + $d2_months ) . 'M'
. ( string ) ( $d1_days + $d2_days ) . 'DT'
. ( string ) ($d1_hours + $d2_hours ) . 'H'
. ( string ) ( $d1_minutes + $d2_minutes ) . 'M'
. ( string ) ($d1_seconds + $d2_seconds ) . 'S' );
return $return_dateinterval;
}
最佳答案
2021 年更新
差不多四年了,我认为最好的方法是让 PHP 计算总和并规范化过程中的字段:
function sumDateIntervals(DateInterval $a, DateInterval $b)
{
$base = new DateTimeImmutable();
return $base->add($a)->add($b)->diff($base);
}
检查一下online .
最初接受的答案
To me, your solution looks like the best way to implement this functionality.
There is no need to explicitly convert the sums to strings (PHP does it anyway). If you want to make it more readable you can use
sprintf()
to produce the string and shorter names for the arguments:function sumDateIntervals(DateInterval $a, DateInterval $b) { return new DateInterval(sprintf('P%dY%dM%dDT%dH%dM%dS', $a->y + $b->y, $a->m + $b->m, $a->d + $b->d, $a->h + $b->h, $a->i + $b->i, $a->s + $b->s )); }
关于PHP 将两个 DateInterval 对象添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46757367/