绝对参数在DateTime :: diff(DateTime [,bool absolute = false])中做了什么

时间:2011-09-29 11:09:12

标签: php datetime datediff

以下是示例代码

$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)
}

如我所见,只有“反转”属性发生变化。这是什么意思?

2 个答案:

答案 0 :(得分:7)

绝对属性将返回两个DateTime对象之间的绝对差值。当返回负差值时,这会将结果更改为正。

答案 1 :(得分:0)

来自php.net

Absolute parameter determines whether to return absolute difference.

LOI datetime.diff