3v4l.org

run code in 300+ PHP versions simultaneously
<?php $start_date = '2019-01-01'; $end_date = '2019-12-31'; function makeBroadcastCalendar($start_date, $end_date) { $end = new DateTime($end_date); $start = new DateTime($start_date); $start->modify('monday this week'); $week = 1; $weeks = array(); while($start < $end) { $weeks[$week++] = $start->format('Y-m-d'); $start->modify('+1 week'); } return $weeks; } function getBroadcastMonth($date) { $end_of_week = new DateTime($date); $end_of_week->modify('sunday this week'); return (int)$end_of_week->format('n'); } function getBroadcastWeek($date) { $start = new DateTime($date); $start->modify('January 1'); $start->modify('monday this week'); $start_of_week = new DateTime($date); $start_of_week->modify('monday this week'); $week = 1; while ($start < $start_of_week) { $start->modify('+1 week'); $week++; } return $week; } function getFirstAndLast($year, $month) { $start = new DateTime("$year-$month-01"); $end = clone $start; $start->modify('monday this week'); $output = array('first_day' => $start->format('Y-m-d')); $end->modify('+1 month'); $end->modify('monday this week'); while ($start < $end) { $start->modify('+1 week'); } $output['end'] = $start->modify('-1 day')->format('Y-m-d'); return $output; } echo getBroadcastMonth('2019-10-12') . PHP_EOL; echo getBroadcastMonth('2019-10-29') . PHP_EOL; echo getBroadcastMonth('2019-05-31') . PHP_EOL; echo getBroadcastWeek('2019-10-12') . PHP_EOL; echo getBroadcastWeek('2019-10-29') . PHP_EOL; echo getBroadcastWeek('2019-05-31') . PHP_EOL; for ($i = 1; $i <= 12; $i++) { print_r(getFirstAndLast(2019, $i)); }

preferences:
136.88 ms | 404 KiB | 5 Q