【问题标题】:What does absolute parameter do in DateTime::diff(DateTime [, bool absolute = false])DateTime::diff(DateTime [, bool absolute = false]) 中的绝对参数有什么作用
【发布时间】:2011-09-29 11:09:12
【问题描述】:

这里是示例代码

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

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

【问题讨论】:

    标签: php datetime datediff


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我现在明白了。用echo $diff->format('%R%a'), $diff2->format('%R%a'); 对其进行了测试,得到-60+60 //darn 接受答案的时间限制:(
    【解决方案2】:

    来自 php.net

    Absolute parameter determines whether to return absolute difference.
    

    LOI datetime.diff

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      相关资源
      最近更新 更多