3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function getWeeksInMonth($weekStartDay = 0) { $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();
based on UVseG
Output for 5.5.0 - 7.3.0beta1
5