0
votes

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

1

1 Answers

3
votes
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.