这有效:
$aWeekAfterDate = new DateTime();
$aWeekAfterDate->modify('+1 week');
$aWeekAfterDate->format('d.m.Y');
但是这个:
$aWeekAfterDate = new DateTime();
$aWeekAfterDate->modify('+1 week')->format('d.m.Y');
给我这个错误:
Fatal error: Call to a member function format() on a non-object in ... on line ...
Fatal error (shutdown): Call to a member function format() on a non-object in ... on line ...
我查了一下DateTime::modify文档中的方法及其返回值是这样的:
Returns the DateTime object for method chaining or FALSE on failure.
为什么方法链不起作用?
我使用的是 PHP 版本 5.2.6。
最佳答案
变更日志
Version Description
5.3.0 Changed the return value on success from NULL to DateTime.
因此返回 DateTime 对象进行链接仅在 PHP 5.3.0 中引入
关于php - DateTime方法链接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5497490/