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:
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);
}
Depending of what you consider a first day of the week, you can do it like this:
I will consider that you consider Monday as a first day of the week.
// Create a date (carbon)
$carbon = Carbon::create()->setISODate(2022, 9);
// Find the first Monday in the month for given date. For Tuesday use 2, Wednesday 3 ...
$firstMondayInTheMonth = $carbon->firstOfMonth(1); // This is also a start of that week
// Find the end of the week.
$endOfTheWeek = $firstMondayInTheMonth->endOfWeek();
In the image below you can see how it works in practice:
With that, you have a first Monday in the month - which means first start of the week, and using endOfWeek
you can get Sunday of that week (end of the first week). Using betweenIncluded
you can figure out if one date is between that Monday and Sunday of first week in that month.