Why does formatting a date return the wrong year?

后端 未结 2 2038
长发绾君心
长发绾君心 2020-12-21 16:12

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 a

相关标签:
2条回答
  • 2020-12-21 16:18

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

    debug($this->Time->format($date, 'yyyy'))
    
    0 讨论(0)
  • 2020-12-21 16:28

    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.

    0 讨论(0)
提交回复
热议问题