3v4l.org

run code in 300+ PHP versions simultaneously
<?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"; }
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1
array ( 0 => 'Week 1: 2020-01-01 - 2020-01-04', 1 => 'Week 2: 2020-01-05 - 2020-01-11', 2 => 'Week 3: 2020-01-12 - 2020-01-18', 3 => 'Week 4: 2020-01-19 - 2020-01-25', 4 => 'Week 5: 2020-01-26 - 2020-01-31', ) array ( 0 => 'Week 1: 2020-02-01 - 2020-02-01', 1 => 'Week 2: 2020-02-02 - 2020-02-08', 2 => 'Week 3: 2020-02-09 - 2020-02-15', 3 => 'Week 4: 2020-02-16 - 2020-02-22', 4 => 'Week 5: 2020-02-23 - 2020-02-29', ) array ( 0 => 'Week 1: 2020-03-01 - 2020-03-07', 1 => 'Week 2: 2020-03-08 - 2020-03-14', 2 => 'Week 3: 2020-03-15 - 2020-03-21', 3 => 'Week 4: 2020-03-22 - 2020-03-28', 4 => 'Week 5: 2020-03-29 - 2020-03-31', ) array ( 0 => 'Week 1: 2020-04-01 - 2020-04-04', 1 => 'Week 2: 2020-04-05 - 2020-04-11', 2 => 'Week 3: 2020-04-12 - 2020-04-18', 3 => 'Week 4: 2020-04-19 - 2020-04-25', 4 => 'Week 5: 2020-04-26 - 2020-04-30', ) array ( 0 => 'Week 1: 2020-05-01 - 2020-05-02', 1 => 'Week 2: 2020-05-03 - 2020-05-09', 2 => 'Week 3: 2020-05-10 - 2020-05-16', 3 => 'Week 4: 2020-05-17 - 2020-05-23', 4 => 'Week 5: 2020-05-24 - 2020-05-30', 5 => 'Week 6: 2020-05-31 - 2020-05-31', ) array ( 0 => 'Week 1: 2020-06-01 - 2020-06-06', 1 => 'Week 2: 2020-06-07 - 2020-06-13', 2 => 'Week 3: 2020-06-14 - 2020-06-20', 3 => 'Week 4: 2020-06-21 - 2020-06-27', 4 => 'Week 5: 2020-06-28 - 2020-06-30', ) array ( 0 => 'Week 1: 2020-07-01 - 2020-07-04', 1 => 'Week 2: 2020-07-05 - 2020-07-11', 2 => 'Week 3: 2020-07-12 - 2020-07-18', 3 => 'Week 4: 2020-07-19 - 2020-07-25', 4 => 'Week 5: 2020-07-26 - 2020-07-31', ) array ( 0 => 'Week 1: 2020-08-01 - 2020-08-01', 1 => 'Week 2: 2020-08-02 - 2020-08-08', 2 => 'Week 3: 2020-08-09 - 2020-08-15', 3 => 'Week 4: 2020-08-16 - 2020-08-22', 4 => 'Week 5: 2020-08-23 - 2020-08-29', 5 => 'Week 6: 2020-08-30 - 2020-08-31', ) array ( 0 => 'Week 1: 2020-09-01 - 2020-09-05', 1 => 'Week 2: 2020-09-06 - 2020-09-12', 2 => 'Week 3: 2020-09-13 - 2020-09-19', 3 => 'Week 4: 2020-09-20 - 2020-09-26', 4 => 'Week 5: 2020-09-27 - 2020-09-30', ) array ( 0 => 'Week 1: 2020-10-01 - 2020-10-03', 1 => 'Week 2: 2020-10-04 - 2020-10-10', 2 => 'Week 3: 2020-10-11 - 2020-10-17', 3 => 'Week 4: 2020-10-18 - 2020-10-24', 4 => 'Week 5: 2020-10-25 - 2020-10-31', ) array ( 0 => 'Week 1: 2020-11-01 - 2020-11-07', 1 => 'Week 2: 2020-11-08 - 2020-11-14', 2 => 'Week 3: 2020-11-15 - 2020-11-21', 3 => 'Week 4: 2020-11-22 - 2020-11-28', 4 => 'Week 5: 2020-11-29 - 2020-11-30', ) array ( 0 => 'Week 1: 2020-12-01 - 2020-12-05', 1 => 'Week 2: 2020-12-06 - 2020-12-12', 2 => 'Week 3: 2020-12-13 - 2020-12-19', 3 => 'Week 4: 2020-12-20 - 2020-12-26', 4 => 'Week 5: 2020-12-27 - 2020-12-31', )

preferences:
112.11 ms | 417 KiB | 5 Q