3v4l.org

run code in 300+ PHP versions simultaneously
<?php function rangify(array $dates): string { $lastYm = null; $lastN = null; $result = []; foreach ($dates as $date) { $dt = new DateTime($date); $ym = $dt->format('Y-m'); $j = (int) $dt->format('j'); if ($ym === $lastYm) { if (($j - 1) === $lastN) { $ref = preg_replace('/-\d+$/', '', $ref) . "-$j"; } else { $ref .= ",$j"; } } else { unset($ref); $ref = $dt->format('M') . " $j"; $result[] = &$ref; } $lastYm = $ym; $lastN = $j; } return implode(', ', $result); } $tests = [ ['2015-10-27', '2015-10-28', '2015-10-30', '2015-10-31', '2015-11-01'], ['2015-10-31', '2015-11-01', '2015-11-02'], ['2015-10-26', '2015-10-28', '2015-10-29', '2015-10-31', '2015-11-01'], ]; //Oct 30-31, Nov 01 //Oct 31, Nov 1-2 //Oct 29,31, Nov 1 foreach ($tests as $test) { echo rangify($test) . "\n"; }
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Oct 27-28,30-31, Nov 1 Oct 31, Nov 1-2 Oct 26,28-29,31, Nov 1
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
94.39 ms | 407 KiB | 5 Q