我们如何在PHP中添加两个日期间隔 [英] How we can add two date intervals in PHP
问题描述
我想添加两个日期间隔来计算总时长(以小时和分钟为单位),实际上我想执行加法,如下所示:
i want to add two date intervals to calculate the total duration in hours and minutes in fact i want to perform addittion as shown below:
$a = new DateTime('14:25');
$b = new DateTime('17:30');
$interval1 = $a->diff($b);
echo "interval 1 : " . $interval1->format("%H:%I");
echo "<br />";
$c = new DateTime('08:00');
$d = new DateTime('13:00');
$interval2 = $c->diff($d);
echo "interval 2 : " . $interval2->format("%H:%I");
echo "<br />";
echo "Total interval : " . $interval1 + $interval2;
任何想法如何执行这种类型的间隔添加,以获得总时间的两个间隔的总和PHP中的分钟格式
Any idea how to perform this type of interval addition to get the sum of the two intervals in total hours and minutes format in PHP
推荐答案
PHP没有运算符重载*所以 +
对象使PHP尝试将其转换为字符串,但 DateInterval
不支持:
PHP has no operator overloading* so +
with objects makes PHP trying it to convert them to string first, but DateInterval
does not support that:
interval 1: 03:05
interval 2: 05:00
Total interval : 08:05
相反,您需要创建一个新的 DateTime
对象,然后使用 add
函数添加间隔,最后显示差异到参考点:
Instead you need to create a new DateTime
object, then use the add
function to add the intervals and finally display the difference to the reference point:
$e = new DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
echo "Total interval : ", $f->diff($e)->format("%H:%I"), "\n";
Full Exmaple /( Demo ):
Full Exmaple/(Demo):
$a = new DateTime('14:25');
$b = new DateTime('17:30');
$interval1 = $a->diff($b);
echo "interval 1: ", $interval1->format("%H:%I"), "\n";
$c = new DateTime('08:00');
$d = new DateTime('13:00');
$interval2 = $c->diff($d);
echo "interval 2: ", $interval2->format("%H:%I"), "\n";
$e = new DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
echo "Total interval : ", $f->diff($e)->format("%H:%I"), "\n";
您可能还想考虑如何 DateInterval
存储其值,然后从其中扩展以进行自己的计算。以下示例(演示)很粗糙,它没有考虑到反转的东西,它 not(re)set $ days
to false
我没有检查/测试创建时间说明符的ISO规范,但我认为这是足够的显示想法:
You might also want to consider looking how DateInterval
stores its' values and then extend from it to do the calculation your own. The following example (Demo) is rough, it does not take into account the inverted thingy, it does not (re)set $days
to false
and I have not checked/tested the ISO specification of the period specifier on creation but I think it is enough to show the idea:
class MyDateInterval extends DateInterval
{
/**
* @return MyDateInterval
*/
public static function fromDateInterval(DateInterval $from)
{
return new MyDateInterval($from->format('P%yY%dDT%hH%iM%sS'));
}
public function add(DateInterval $interval)
{
foreach (str_split('ymdhis') as $prop)
{
$this->$prop += $interval->$prop;
}
}
}
$a = new DateTime('14:25');
$b = new DateTime('17:30');
$interval1 = $a->diff($b);
echo "interval 1: ", $interval1->format("%H:%I"), "\n";
$c = new DateTime('08:00');
$d = new DateTime('13:00');
$interval2 = $c->diff($d);
echo "interval 2: ", $interval2->format("%H:%I"), "\n";
$e = MyDateInterval::fromDateInterval($interval1);
$e->add($interval2);
echo "Total interval: ", $e->format("%H:%I"), "\n";
* 如果你写一个PHP扩展,它实际上是可能的(至少是sort-of)。
* If you write a PHP extension, it actually is possible (at least sort-of).
这篇关于我们如何在PHP中添加两个日期间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!