这是示例代码
$c = new DateTime();
$o = clone $c;
$o->modify('-60 days');
$diff = $c->diff($o);
$diff2 = $c->diff($o, TRUE);
var_dump($diff, $diff2);
哪个输出
object(DateInterval)#3 (8) {
["y"]=> int(0), ["m"]=> int(1), ["d"]=> int(29), ["h"]=> int(0), ["i"]=> int(0),
["s"]=> int(0), ["invert"]=> int(1), ["days"]=> int(60)
}
object(DateInterval)#4 (8) {
["y"]=> int(0), ["m"]=> int(1), ["d"]=> int(29), ["h"]=> int(0), ["i"]=> int(0),
["s"]=> int(0), ["invert"]=> int(0), ["days"]=> int(60)
}
正如我所看到的,只有“反转”属性发生了变化.这是什么意思?
解决方法:
绝对属性将返回两个DateTime对象之间的绝对差异.当返回负差值时,这会将结果更改为正数.