How to get a calendar week sensitive to a year? date ("W") returns 52 on January 1

As the title says, the PHP date function ("W") returns the calendar week (for the current day). Unfortunately, it returns 52 or 53 during the first days of most years. This, by simple thought, is correct, but very annoying, since January 1, 2012 is not calendar week 52, this is NOT the calendar week of the current year. Most calendars define this as week 0 or week 52 of the previous year.

This is very difficult when you group each day of the year by calendar week: January 1, 2012 and December 31, 2012 will both be placed in the same group of calendar weeks.

So my question is: is there a (native) year-sensitive alternative to the PHP date ("W")?

EDIT: I think I wrote the first version of this question very unclear, so this is my edit: I'm looking for a function that returns the correct calendar week for the first days of the year. The PHP date ("W") returns 52 on January 1, 2012, which is the "wrong". It must be 0 or null. According to official sources, the first calendar week of the year begins on the first Monday of the year. So, if the first day of the year is not Monday, this is not week 1! This is week 0. Wikipedia article says

If January 1 is Monday, Tuesday, Wednesday or Thursday, it is on week 01. If January 1 is on Friday, Saturday or Sunday, then it is 52 or 53 years old.

This becomes difficult as the last days of the year are also on week 52/53. date ("W") does not divide into current year and previous year.

+7
source share
3 answers

This decision converts the December excess to week 53 and everything in January to week 1 to 0.

$w=(int)date('W'); $m=(int)date('n'); $w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w); echo "week $w in ".date('Y'); 

2013-12-31 ==> week 53 in 2013

2014-01-01 ==> week 1 in 2014

2015-12-31 ==> week 52 in 2015

2016-01-01 ==> week 0 in 2016

And a small test run, so you can see for yourself :-)

 $id=array(25,26,27,28,29,30,31,1,2,3,4,5,6,7,8); for($iy=2013;$iy<2067;++$iy){foreach($id as $k=>$v){if($k<7){$im=12;}else{$im=1;} if($k==7){++$iy;echo '====<br>';}$tme=strtotime("$im/$v/$iy"); echo date('dm-Y',$tme),' * * '; //THE ACTUAL CODE ================= $w=(int)date('W',$tme); $m=(int)date('n',$tme); $w=$w==1?($m==12?53:1):($w>=51?($m==1?0:$w):$w); //THE ACTUAL CODE ================= echo '<b>WEEK: ',$w,' --- ','YEAR: ',date('Y',$tme),'</b><br>';}--$iy; echo '----------------------------------<br>';} 
+9
source

Is there a (native) year-sensitive alternative to the PHP date ("W")?

No no.

According to official sources, the first calendar week of the year begins on the first Monday of the year.

I am not sure what official sources you have in mind.

PHP date("W") returns the week number in accordance with ISO 8601. As an international standard, ISO 8601 is considered one of perhaps many "official sources". If its definition of week numbers does not match your application, you can use whatever you like.

If you use the non-standard definition of β€œfirst week of the year” or if you use an official source that has not been widely accepted, expect that you will have to write your own function to replace date("W") . (I'm sure you need to write a function.)

Date 2012-01-01 - Sunday. ISO 8601, Wikipedia , and php agree that the ISO week number for 2012-01-01 is 52.

ISO 8601 does not define week 0.

So, if the first day of the year is not Monday, this is not week 1!

Neither ISO nor Wikipedia speak of this. ISO 8601 defines week number 1 as the week in which the first year of the year is. In 2012, the first Thursday was January 5, so number 1 was from January 2 to January 8. 2012-01-01 was in the last week of the previous year in terms of ISO weeks.

If you want something else, you can play with arithmetic, division, etc. (For example, try dividing the date ("z") by 7. For example, you can save this data in a database and spend weeks as you like.

If you are dealing with reporting periods, I would almost certainly have saved this data in a table in the database. It is very easy to generate such data using a spreadsheet.

The text of the data in the table is much easier to check than the text of the php function, regardless of how simple the function is. And the data will certainly be the same for any program that accesses it, regardless of the language it is written in. (So, if your database ever has programs written in 5 different languages, you do not need to write access to it, and support 5 different functions to get the week number.)

+2
source
 $d = new DateTime('first monday january '.date('Y')); echo $d->format("W"); 
+1
source

All Articles