Why does formatting a date return the wrong year?

你说的曾经没有我的故事 提交于 2019-12-18 09:12:29

问题


I want to print formatted dates on my page. I have an object date and I use $this->Time->format() for formatting. Everything works fine before December 27th. Unfortunately all dates after the December 26th, formatting change my year number.

This is my debug($date)

object(Cake\I18n\Time) {

'time' => '2015-12-30T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false

}

This is my debug($this->Time->format($date, 'YYYY'))

'2016'

I tried to use i18nFormat but I have the same problem. Here is my debug($date->i18nFormat('YYYY'));

'2016'

Thanks for your help


回答1:


i18n dates are formatted using IntlDateFormatter, which use ISO date format patterns, where YYYY doesn't just mean Full year, but Full week-numbering year, wich will return 2016 because the week of the day 2015-12-30 spans into the next year, or more specifically, because that week includes January the 1st that is still a weekday (Monday to Friday), thus it's being treated as the first week of 2016 according to the ISO week rules.

See also https://en.wikipedia.org/wiki/ISO_8601#Week_dates

You want to use yyyy instead, which will return the regular calendar year.




回答2:


Try specifying the date format as 'yyyy'. The case is important.

debug($this->Time->format($date, 'yyyy'))


来源:https://stackoverflow.com/questions/34691716/why-does-formatting-a-date-return-the-wrong-year

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!