0

I used this code to get the 1st date, of the 1st week, of the year:

echo date('Y-m-d',strtotime('2018W01'));   #JAN 1 is Monday, returned 01/01
echo date('Y-m-d',strtotime('2013W01'));   #JAN 1 is Tuesday, returned 12/31
echo date('Y-m-d',strtotime('2014W01'));   #JAN 1 is Wednesday, returned 12/30
echo date('Y-m-d',strtotime('2015W01'));   #JAN 1 is Thursday, returned 12/29
echo date('Y-m-d',strtotime('2016W01'));   #JAN 1 is Friday, returned 01/04!? (shouldn't it be 12/28)
echo date('Y-m-d',strtotime('2022W01'));   #JAN 1 is Saturday, returned 01/03!? (shouldn't it be 12/27)
echo date('Y-m-d',strtotime('2017W01'));   #JAN 1 is Sunday, returned 01/02!? (shouldn't it be 12/26)

Since PHP, which is greater than 5.2.7, showing me that Monday is the 1st day of the week, then I was hoping that the year 2017, 1st day of week, should be 12/26. Is there a week number configuration somewhere for PHP to display 1st day, of the week, of the year correctly, for every year? TIA

xam
  • 452
  • 2
  • 7
  • 15

1 Answers1

3
echo date('Y-m-d',strtotime('2016W01'));
#JAN 1 is Friday, returned 01/04!? (shouldn't it be 12/28)

No, it shouldn't be 12/28, because first week of the year has this rules:

  • It is the first week with a majority (4 or more) of its days in January.
  • Its first day is the Monday nearest to 1 January.
  • It has 4 January in it. Hence the earliest possible dates are 29 December through 4 January, the latest 4 through 10 January.
  • It has the year's first working day in it, if Saturdays, Sundays and 1 January are not working days.

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, it is part of week 53 of the previous year; if on a Saturday, it is part of week 52 (or 53 if the previous year was a leap year); if on a Sunday, it is part of week 52 of the previous year.

Other examples are also correct. Read more here.

Glavić
  • 42,781
  • 13
  • 77
  • 107
  • This is what I was afraid of... Is there anyway to config PHP not to use ISO or Gregorian calendar week number system? So that the `strtotime` would output 12/28, 12/27, and 12/26? Or the only way is to work around and identify if the JAN 1 year lands on Friday through Sunday, if so week number minus 1? The week doesnt have to start on Monday, I just need php to give week # systematically without special rule of requiring to have 4 days to be considered week #1. I was hoping for just 1 day would be enough to be considered week #1. Thank you for your answer, and TIA – xam Jan 07 '15 at 16:54
  • @xam: I don't know why would you need to make your own logic about week numbering? You are using ISO standard, when you use format `YEAR-W0x`. If you need some kind of custom logic, you will need to make it yourself, they are no native functions; but I would rather use ISO standard than make some custom logic... – Glavić Jan 07 '15 at 18:36
  • Changing a standard is not my intention. I wouldn't have ever guessed that the standard would required 4 days to be considered week 1. Probably that's ISO standard...would there be any other standards that PHP can use also? I was naively thought the 1st week of the year only required 1st day of the year in it. If I can use other standard by changing the format, I would like to give it a shot. Thank you and TIA. – xam Jan 07 '15 at 18:54
  • @xam: ISO8601 is just saying, that first or last week belongs to year in which has the majority of days (what makes perfect sense to me). So if 4+ days are in December, week belongs to year X; if 4+ days are in January week belongs to year X+1. There isn't any standard that I should know about that makes week 1 only those weeks that have all days in January... But now, that you know how this ISO works, why not use it? – Glavić Jan 08 '15 at 07:17
  • I worked around and fixed some bugs caused by this concept, thanks. – xam Jan 08 '15 at 22:18