Get week numbers and accurate days in a given month PHP function

I had created a function that will get the ISO-8601 week numbers in a given month. It would also return the accurate total number of days on that month including leap year. The input parameter is the month and the year of what you are looking for.

function get_week_numbers($month, $year){
// get all days of the month first
$total_days = cal_days_in_month(CAL_GREGORIAN, $month, $year);

// initialize the array()
$return = array();

//loop the days
for($i = 1; $i <= $total_days; $i++){
// get week number of days, if it is a new week number,
// then push to array key
$week_number = date("W", strtotime("$year-$month-$i"));

// push day into array with week number as key
$return[$week_number][] = date("Y-m-d", strtotime("$year-$month-$i"));

}// end for loop
return $return;

}

If I would input “11” for the month, and “2019” for the year, the output will be a multidimensional array with the week numbers as the keys and the days that belong inside of that week in an array form
as its value.


array(5) {
[44]=>
array(3) {
[0]=>
string(10) "2019-11-01"
[1]=>
string(10) "2019-11-02"
[2]=>
string(10) "2019-11-03"
}
[45]=>
array(7) {
[0]=>
string(10) "2019-11-04"
[1]=>
string(10) "2019-11-05"
[2]=>
string(10) "2019-11-06"
[3]=>
string(10) "2019-11-07"
[4]=>
string(10) "2019-11-08"
[5]=>
string(10) "2019-11-09"
[6]=>
string(10) "2019-11-10"
}
[46]=>
array(7) {
[0]=>
string(10) "2019-11-11"
[1]=>
string(10) "2019-11-12"
[2]=>
string(10) "2019-11-13"
[3]=>
string(10) "2019-11-14"
[4]=>
string(10) "2019-11-15"
[5]=>
string(10) "2019-11-16"
[6]=>
string(10) "2019-11-17"
}
[47]=>
array(7) {
[0]=>
string(10) "2019-11-18"
[1]=>
string(10) "2019-11-19"
[2]=>
string(10) "2019-11-20"
[3]=>
string(10) "2019-11-21"
[4]=>
string(10) "2019-11-22"
[5]=>
string(10) "2019-11-23"
[6]=>
string(10) "2019-11-24"
}
[48]=>
array(6) {
[0]=>
string(10) "2019-11-25"
[1]=>
string(10) "2019-11-26"
[2]=>
string(10) "2019-11-27"
[3]=>
string(10) "2019-11-28"
[4]=>
string(10) "2019-11-29"
[5]=>
string(10) "2019-11-30"
}
}