<?php
function get_weeks($month, $year) {
$weeks = [];
$ym = $year . '-' . $month;
$final = date('t', strtotime($ym));
$firstSat = date('d', strtotime("first Saturday of $ym"));
$d = 1;
$weekNo = 0;
do {
$weekEnd = $d === 1 ? $firstSat : min($final, $d + 6);
$weeks[] = sprintf(
"Week %d: %s-%02d - %s-%02d",
++$weekNo,
$ym,
$d,
$ym,
$weekEnd
);
$d = $weekEnd + 1;
} while ($weekEnd < $final);
return $weeks;
}
foreach (['01','02','03','04','05','06', '07', '08', '09', '10', '11', '12'] as $month) {
echo var_export(get_weeks($month, '2020'), true) . "\n";
}
preferences:
24.41 ms | 408 KiB | 5 Q