CakePHP 3.6:无法将类Cake\I18n\FrozenTime的对象转换为int
我试图在CakePHP 3.6:无法将类Cake\I18n\FrozenTime的对象转换为int,php,cakephp,time,Php,Cakephp,Time,我试图在index.ctp中检查日期是否晚于一周前: (((!isset($task->date_end) || is_null($task->date_end))? strotime('now') : $task->date_end) > strtotime('-1 week')) 但我收到了这个错误: 类Cake\I18n\FrozenTime的对象无法转换为int 为了检查数据库中的日期是否有任何错误,我将它们全部更改为:2019-01-02 0
index.ctp
中检查日期是否晚于一周前:
(((!isset($task->date_end) || is_null($task->date_end))?
strotime('now') : $task->date_end) > strtotime('-1 week'))
但我收到了这个错误:
类Cake\I18n\FrozenTime的对象无法转换为int
为了检查数据库中的日期是否有任何错误,我将它们全部更改为:
2019-01-02 05:06:00.000000
当您将非整数与整数进行比较时,PHP的类型转换将尝试将前者转换为整数,而FrozenTime
对象无法转换为整数
您可以通过始终使用日期对象来避免这种脆弱的构造,例如,使用它们提供的比较方法
$result = true;
if ($task->date_end !== null) {
$lastWeek = \Cake\I18n\Time::now()->subWeek(1);
$result = $task->date_end->gt($lastWeek);
}
另见
FrozenTime
对象无法转换为整数
您可以通过始终使用日期对象来避免这种脆弱的构造,例如,使用它们提供的比较方法
$result = true;
if ($task->date_end !== null) {
$lastWeek = \Cake\I18n\Time::now()->subWeek(1);
$result = $task->date_end->gt($lastWeek);
}
另见
Time
和Date
对象而不是strotime
,您可以使用setTestNow
更容易地构建可靠的单元测试。此外,通过使用Time
和Date
对象而不是strotime
,您可以使用setTestNow
更轻松地构建可靠的单元测试。