3v4l.org

run code in 300+ PHP versions simultaneously
<?php $result = []; $start = new \DateTime('2016-11-12'); $start->modify('monday this week'); $end = new \DateTime('2017-05-06'); $end->modify('monday next week'); $p = new \DatePeriod( $start, new \DateInterval('P1W'), $end ); foreach ($p as $d) { $y = $d->format("Y"); $m = $d->format("M"); if (!array_key_exists($y, $result['years'])){ $result['years'][$y] = ['y' => $y]; } if (!arrray_key_exists($d->format("m"), $result['years'][$y]['months'])){ $result['years'][$y]['months'][$d->format("m")] = ['m' => $m]; } $result['years'][$y]['months'][$m]['weeks'][] = ['f' => $d, 't' => (clone $d)->modify("sunday this week")]; //$result[$d->format("Y")][$d->format("M")][] = [$d, (clone $d)->modify("sunday this week")]; } print_r($result); exit; foreach ($result as $year => $months){ echo $year . PHP_EOL; foreach ($months as $month => $weeks){ echo "--" . $month . PHP_EOL; foreach ($weeks as $week){ echo "----" . $week[0]->format("jM") . "-" . $week[1]->format("jM") . PHP_EOL; } } } exit; $start = new DateTime('2010-12-02'); $start->modify('first day of this month'); $end = new DateTime('2012-05-06'); $end->modify('first day of next month'); $interval = DateInterval::createFromDateString('1 month'); $period = new DatePeriod($start, $interval, $end); foreach ($period as $dt) { echo $dt->format("Y-m") . PHP_EOL; $interval_1 = DateInterval::createFromDateString('1 week'); $end_1 = clone $dt; $end_1->add($interval); $period_1 = new DatePeriod($dt, $interval_1, $end_1); foreach ($period_1 as $dt_1){ echo "--" . $dt_1->format("Y-m-d") . PHP_EOL; } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: Undefined array key "years" in /in/Up7ip on line 21 Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /in/Up7ip:21 Stack trace: #0 {main} thrown in /in/Up7ip on line 21
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: Undefined array key "years" in /in/Up7ip on line 21 Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /in/Up7ip:21 Stack trace: #0 {main} thrown in /in/Up7ip on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined index: years in /in/Up7ip on line 21 Warning: array_key_exists() expects parameter 2 to be array, null given in /in/Up7ip on line 21 Fatal error: Uncaught Error: Call to undefined function arrray_key_exists() in /in/Up7ip:24 Stack trace: #0 {main} thrown in /in/Up7ip on line 24
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Warning: array_key_exists() expects parameter 2 to be array, null given in /in/Up7ip on line 21 Fatal error: Uncaught Error: Call to undefined function arrray_key_exists() in /in/Up7ip:24 Stack trace: #0 {main} thrown in /in/Up7ip on line 24
Process exited with code 255.
Output for 5.6.0 - 5.6.28
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ']' in /in/Up7ip on line 27
Process exited with code 255.

preferences:
205.28 ms | 401 KiB | 234 Q