CakePHP 3 - 比较和修改日期

时间:2017-03-30 07:07:30

标签: date cakephp time cakephp-3.0

CakePHP 3 Cookbook on Date/Time中,您可以使用IsWithinNext / WasWithinNext将时间间隔与未来/过去的天数/周数进行比较。您还可以通过执行 - >修改('额外时间')来修改日期/时间 - 例如。 if $ date = 2016-01-01,$ date-> modify('+ 1 week')表示$ date = 2016-01-08。

这些功能需要使用Cake \ i18n \ Time。但是,当我尝试使用这些功能时,我收到了一个Cake错误:

  

在字符串上调用成员函数isWithinNext()。

这是我使用的代码:

$date_start = \Cake\Database\Type::build('date')->marshal($data['session']['date_start'])->i18nFormat(); //before hand my dates were in the form of an array comprised of Year, Month and Day. This changes them into date format.
if($date_start->isWithinNext('1 week')){
    $deposit_due = $booking->date_confirm;
    $deposit_due->modify('+48 hours');
} elseif ($date_start->isWithinNext('2 weeks')){
    $deposit_due = $booking->date_confirm;
    $deposit_due->modify('+1 week');
} elseif ($date_start->isWithinNext('3 weeks')){
    $deposit_due = $booking->date_confirm;
    $deposit_due->modify('+1 week');
} else {
    $deposit_due = $booking->date_confirm;
    $deposit_due->modify('+2 weeks');
}

1 个答案:

答案 0 :(得分:1)

调用i18nFormat()会返回格式化字符串,因为您可以在API中查找:https://api.cakephp.org/3.4/class-Cake.I18n.DateFormatTrait.html#_i18nFormat

例如,这应该有效:

$date_start = new \Cake\I18n\Time($data['session']['date_start']);
debug($date_start->isWithinNext('2 weeks'));