We need to determine "first week of the month". As far as I've read there's no ISO standard for this but I've encountered two definitions:
- First week of the month is the week that contains 1st day of the month.
- First week of the month is the first full week of that month.
In our application, we are frequently using the ISO week format which is like '2022-W09' that means (obviously) 9th week of 2022.
So, we can easily find first week of the year '2022-W01' and the dates it includes: from 2022-01-03 to 2022-01-09 (according to HTML5 input type week).
And this shows me that (even though I liked and implemented the first definition in the first place) we should accept the second definition because HTML follows that.
As a conclusion, I need an algorithm to find "first week of the month" which I accept to be "the first full week of that month" (2nd definition).
Hereby I put the code I use to find "the week that contains 1st day of the month" which is "first week of the month" in the 1st definition above. You may modify it to suggest a solution:
public function isFirstWeekOfMonth()
{
$carbon = Carbon::create()->setISODate(2022, 9);
$startOfWeekCarbon = $carbon->startOfWeek();
$endOfWeekCarbon = $carbon->endOfWeek();
$startOfMonthCarbon = $carbon->endOfWeek()->startOfMonth();
return $startOfMonthCarbon->betweenIncluded($startOfWeekCarbon, $endOfWeekCarbon);
}