Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...