Number of the last week of the month


Please tell me, php gurus, why the number of the last, it would seem, week of the month, and the last month of the year = 01? More precisely, I guess why, but how to deal with it?

Here is the code:

$currentYear = 2012;
$weekMax = date("W", mktime(0,0,0,12,31,$currentYear));

Answer 1, authority 100%

You can simply check the day of the week first. If it is greater than 3, then your formula, and if not, then simply subtract the number of the day of the week from 31 and calculate

$currentYear = 2012;
$w = date("w", mktime(0,0,0,12,$d,$currentYear));
if ($w<4) $d=$d-$w;
$weekMax = date("W", mktime(0,0,0,12,$d,$currentYear));
$date=date("Y-m-d---w", mktime(0,0,0,12,$d,$currentYear));
echo "{$weekMax}==={$date}"; //   ,     

Answer 2

It’s strange, it’s actually the other way around: the weeks roll into the next year.

$weekMax = date("W", strtotime($currentYear.'-12-31'));

In any case, you can put a crutch: not the 31st, but the 24th if the number of weeks is 1. Just kidding. This is how it works