![]() Using this parameter, the result is correct: We get the 53th calendar week of 2015. Here is an example: $date = strtotime('') Įcho date("W o", $date) // output: 53 2015 Find more info on our main week number page. Please note that there are multiple systems for week numbering, this is the ISO week date standard (ISO-8601), other systems use weeks starting on Sunday (US) or Saturday (Islamic). However, we need 2015, because from the point of view of the calendar week, January, 1 is belonging to the last week of the last year in this case.įortunately, the developers of PHP have thought of this special case and bestowed the parameter "o" (lowercase O) upon us. If we use this parameter instead of the usual parameter Y for the year with the date() function, we will receive the year suitable for the passed date's week number. All weeks are starting on Monday and ending on Sunday. The reason for that wrong result is that "Y" is providing us the year of the passed date - this is 2016. This example shows, what is going wrong: For the date, we get the week number 53 of 2016. In both of this cases, the usage of date("W Y", $date) leads to wrong results: $date = strtotime('') Įcho date("W Y", $date) // output: 53 2016 The problem can occur whenever the first calendar week of the new year is already starting in December or if the last calendar week of a year is going over into January. January 2016 F 1 New Year’s Day S 2 S 3 M 4 1 T 5 W 6 T 7 F 8 S 9 S 10 M 11 2 T 12 W 13 T 14 F 15 S 16 S 17 M 18 Dr. There is one problem that arises almost at each turn of the year. With this, the result of the function is converted from a string to an integer number whose output is naturally single-digit without any leading zeros. If we want to have a single digit number for the week numbers 1 to 9, we can just write (int)date("W", $date) to get it. ![]() Here is an example for this call: $date = time() // current dateĮcho date("W", $date) // output, for example: 03 or 10Īccording to ISO-8601, for PHP, the week begins at Monday and the output is formatted in the form of two digits as string. When passing this character as a first parameter and any date as a second parameter to the date() function, we get the calendar week respectively the week number of the year for our date as a result. This is much easier than you might suspect: We only need the function date() and the formatting character "W" for it. Today, I would like to show you how to retrieve the calender week (week number) of an arbitrary given date with PHP. ![]()
0 Comments
Leave a Reply. |