【问题标题】:difference between two pair of date in phpphp中两对日期的区别
【发布时间】:2016-10-26 03:49:24
【问题描述】:

我试图找出两个日期之间的差异。不幸的是,我只得到整数,而不是按要求格式化。我尝试了几种方法,但没有运气。我需要从 $TactInterval 中扣除 $HoldInterval 但系统出错。 下面是我的代码,它不能按要求工作。 我收到 2 而不是 02h30m 的请求。请告知是什么问题。谢谢。

$hd1 = new Datetime('2016-10-18 08:30:00');
$hd2 = new Datetime('2016-10-18 12:00:00');
$HoldInterval = $hd2->diff($hd1);

$d1 = new Datetime('2016-10-18 09:00:00');
$d2 = new Datetime('2016-10-18 10:00:00');
$TactInterval = $d2->diff($d1);
//$interval = $TactInterval - $HoldInterval ); // This is not working.... 

$dt1 = $TactInterval->format('%Hh%Im');
$dt2 = $HoldInterval->format('%Hh%Im');

echo $dt1."<br>";
echo $dt2."<br>";

$dt3 = $dt2 - $dt1; // This is working but only a whole number shows. it show be 02h30m. 

//$dt4 = $dt3->format('%Hh%Im'); // does not work when formated.

echo $dt3."<br>";

【问题讨论】:

    标签: php date difference


    【解决方案1】:
    $s = new DateTime();
    $e = new DateTime();
    $s->add($HoldInterval);
    $e->add($TactInterval);
    $diff = $e->diff($s);
    echo $diff->format('%Hh%Im');
    

    DEMO

    【讨论】:

    • 感谢您的回复。这是一个很大的帮助。请问这怎么可能是使用程序的代码?
    猜你喜欢
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 2010-10-25
    • 2019-09-17
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多