3v4l.org

run code in 300+ PHP versions simultaneously
<?php function weeks_in_month($month, $year) { $dates = []; $week = 1; $date = new DateTime("$year-$month-01"); $days = (int)$date->format('t'); // total number of days in the month $oneDay = new DateInterval('P1D'); for ($day = 1; $day <= $days; $day++) { $dates["Week $week"] []= $date->format('d/m/Y'); $dayOfWeek = $date->format('l'); if ($dayOfWeek === 'Saturday') { $week++; } $date->add($oneDay); } return $dates; } print_r(weeks_in_month(1, 2018));
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Array ( [Week 1] => Array ( [0] => 01/01/2018 [1] => 02/01/2018 [2] => 03/01/2018 [3] => 04/01/2018 [4] => 05/01/2018 [5] => 06/01/2018 ) [Week 2] => Array ( [0] => 07/01/2018 [1] => 08/01/2018 [2] => 09/01/2018 [3] => 10/01/2018 [4] => 11/01/2018 [5] => 12/01/2018 [6] => 13/01/2018 ) [Week 3] => Array ( [0] => 14/01/2018 [1] => 15/01/2018 [2] => 16/01/2018 [3] => 17/01/2018 [4] => 18/01/2018 [5] => 19/01/2018 [6] => 20/01/2018 ) [Week 4] => Array ( [0] => 21/01/2018 [1] => 22/01/2018 [2] => 23/01/2018 [3] => 24/01/2018 [4] => 25/01/2018 [5] => 26/01/2018 [6] => 27/01/2018 ) [Week 5] => Array ( [0] => 28/01/2018 [1] => 29/01/2018 [2] => 30/01/2018 [3] => 31/01/2018 ) )

preferences:
165.91 ms | 411 KiB | 5 Q