3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getWeeksInMonth($weekStartDay = 1) { $date = new \Datetime('2015-03-01'); // Total number of days in the given month.; $numberOfDays = $date->format("t"); // Count the number of times it hits $weekStartDay. $numberOfWeeks = 1; for ($i = 1; $i <= $numberOfDays; $i++) { $nextDay = $date->add(new \DateInterval('P1D')); $dayOfWeek = $nextDay->format('w'); if ($dayOfWeek == $weekStartDay) { $numberOfWeeks++; } } return $numberOfWeeks; } echo getWeeksInMonth();
Output for 5.5.0 - 5.5.35, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
6

preferences:
179.39 ms | 404 KiB | 212 Q