Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
600 views
in Technique[技术] by (71.8m points)

cakephp - Why does formatting a date return the wrong year?

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(CakeI18nTime) {

'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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...