formatter->asDate() Year

I was trying to format a date and was using


but as of the 28th of December, it started displaying the wrong year. Instead of 2020, it was displaying 2021.

I’ve fixed it by switching to

\Yii::$app->formatter->asDateTime($model->StartDt, ‘php:m/d/Y’)

but I’m trying to understand the why behind the issue and learn something.

after consulting the documentation

I see that Y -> year of “Week of Year” whereas y or yyyy -> year, but do not understand the difference. I haven’t been able to test using y and assume it would fix the issue, but would like to understand what ‘year of “Week of Year”’ is exactly. Can anyone enlighten me on this?

Thank you.

Working with dates is a really complex thing, so for ISO 8601 they use a concept of date calculation based on weeks. Ubuntu Man Pages put it this way:

ISO 8601 week dates
%G, %g, and %V yield values calculated from the week-based year defined by the ISO 8601
standard. In this system, weeks start on a Monday, and are numbered from 01, for the
first week, up to 52 or 53, for the last week. Week 1 is the first week where four or
more days fall within the new year (or, synonymously, week 01 is: the first week of the
year that contains a Thursday; or, the week that has 4 January in it). When three of
fewer days of the first calendar week of the new year fall within that year, then the ISO
8601 week-based system counts those days as part of week 53 of the preceding year. For
example, 1 January 2010 is a Friday, meaning that just three days of that calendar week
fall in 2010. Thus, the ISO 8601 week-based system considers these days to be part of
week 53 (%V) of the year 2009 (%G); week 01 of ISO 8601 year 2010 starts on Monday, 4
January 2010.

A clear spot on the problem you pointed can be read on Wikipedia:

The first ISO week of a year may have up to three days that are actually in the Gregorian calendar year that is ending; if three, they are Monday, Tuesday and Wednesday.

So, 2020-12-28 was a Monday, belonging to the week year of 2021.

Another good read on the subject: What year it it? (A tale of ISO week dates)


Oh, what a fantastic definition the week-based year has!! I never knew that.
Thank you @bpanatta.

1 Like

Waw, interesting, I did not not that how to calculate 1st week.
So less than 4 days in a week breaking the year = previous year, more than 3 days = next year.

But it somehow does not fit for me here:

  • 28.12.2020 was monday … till 31.12.2020 thursday = 4 days = should belong to 2020 - right?