我们如何在 PHP 中添加两个日期间隔 [英] How we can add two date intervals in PHP

查看:15
本文介绍了我们如何在 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"), "
";

完整示例/(演示):

$a = new DateTime('14:25');
$b = new DateTime('17:30');
$interval1 = $a->diff($b);
echo "interval 1: ", $interval1->format("%H:%I"), "
";

$c = new DateTime('08:00');
$d = new DateTime('13:00');
$interval2 = $c->diff($d);
echo "interval 2: ", $interval2->format("%H:%I"), "
";

$e = new DateTime('00:00');
$f = clone $e;
$e->add($interval1);
$e->add($interval2);
echo "Total interval : ", $f->diff($e)->format("%H:%I"), "
";


您可能还想考虑查看 DateInterval 如何存储其值,然后从中扩展以进行您自己的计算.下面的例子(Demo)比较粗糙,没有考虑到倒置的东西,它确实未(重新)设置 $daysfalse 并且我还没有检查/测试 创建周期说明符的 ISO 规范a> 但我认为足以说明这个想法:


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"), "
";

$c = new DateTime('08:00');
$d = new DateTime('13:00');
$interval2 = $c->diff($d);
echo "interval 2: ", $interval2->format("%H:%I"), "
";
                                  
$e = MyDateInterval::fromDateInterval($interval1);
$e->add($interval2);
echo "Total interval: ", $e->format("%H:%I"), "
";


* 如果你写一个 PHP 扩展,它实际上是可能的(至少有点).


* If you write a PHP extension, it actually is possible (at least sort-of).

这篇关于我们如何在 PHP 中添加两个日期间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆