3v4l.org

run code in 300+ PHP versions simultaneously
<?php $month = '2'; $year = '2017'; $lastDayOfWeek = '7'; //1 (for monday) to 7 (for sunday) function getWeeksInMonth($year, $month, $lastDayOfWeek, $format = 'Y/m/d') { $aWeeksOfMonth = []; $date = new DateTime("{$year}-{$month}-01"); $iDaysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); $aOneWeek = [$date->format($format)]; $weekNumber = 1; for ($i = 1; $i <= $iDaysInMonth; $i++) { if ($lastDayOfWeek == $date->format('N') || $i == $iDaysInMonth) { $aOneWeek[] = $date->format($format); $aWeeksOfMonth[$weekNumber++] = $aOneWeek; $date->add(new DateInterval('P1D')); $aOneWeek = [$date->format($format)]; $i++; } $date->add(new DateInterval('P1D')); } return $aWeeksOfMonth; } $weeks = getWeeksInMonth($year, $month, $lastDayOfWeek); foreach($weeks as $weekNumber => $week){ echo "Week {$weekNumber}: {$week[0]} - {$week[1]}\r\n"; }
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 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
Fatal error: Uncaught Error: Call to undefined function cal_days_in_month() in /in/1NGgR:11 Stack trace: #0 /in/1NGgR(30): getWeeksInMonth('2017', '2', '7') #1 {main} thrown in /in/1NGgR on line 11
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Fatal error: Call to undefined function cal_days_in_month() in /in/1NGgR on line 11
Process exited with code 255.

preferences:
132.05 ms | 407 KiB | 5 Q