3v4l.org

run code in 300+ PHP versions simultaneously
<?php $toParse = [ [new DateTime('2021-10-3')], [new DateTime('2022-01-25'),new DateTime('2022-01-26'),new DateTime('2022-01-27')], [new DateTime('2021-09-19'),new DateTime('2021-09-22')], [new DateTime('2022-02-28'),new DateTime('2022-03-01'),new DateTime('2022-03-02')], [new DateTime('2022-04-02'),new DateTime('2022-04-03'),new DateTime('2022-04-04'),new DateTime('2022-06-10'),new DateTime('2022-06-11'),new DateTime('2022-06-12'),new DateTime('2022-06-13')], [new DateTime('2021-12-01'),new DateTime('2021-12-02'),new DateTime('2021-12-03'),new DateTime('2021-12-08')] ]; function produceDateString(array $dates): string { // sort the dates sort($dates); // create an array of arrays that contain ranges of consecutive days $ranges = []; $currentRange = []; foreach ($dates as $date) { if(empty($currentRange) || consecutive(end($currentRange), $date)) { $currentRange[] = $date; } else { $ranges[] = $currentRange; $currentRange = [$date]; } } $ranges[] = $currentRange; // create the output string $output = ''; $previous = null; foreach ($ranges as $range) { // add a comma between each range if (!empty($output)) { $output .= ', '; } // the long format should be used on the first occurrence of the loop // or when the month of first date in the range doesn't match // the month of the last date in the previous range $format = $previous === null || end($previous)->format('m') !== reset($range)->format('m') ? 'M. j' : 'j'; // the output differes when there are 1 or multiple dates in a range if (count($range) > 1) { // the output differs when the end and start are in the sane month $output .= sameMonth(reset($range), end($range)) ? reset($range)->format($format).'-'.end($range)->format('j') : reset($range)->format('M. j').'-'.end($range)->format('M. j'); } else { $output .= reset($range)->format($format); } $previous = $range; } return $output; } function consecutive(DateTime $t1, DateTime $t2): bool { $t1->setTime(0, 0, 0, 0); $t2->setTime(0, 0, 0, 0); return(abs($t2->getTimestamp() - $t1->getTimestamp()) < 87000); } function sameMonth(DateTime $t1, DateTime $t2): bool { return $t1->format('Y-m') === $t2->format('Y-m'); } foreach ($toParse as $dates) { echo produceDateString($dates)."\r\n"; }
Output for 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.4, 8.3.6 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0 - 8.5.1
Oct. 3 Jan. 25-27 Sep. 19, 22 Feb. 28-Mar. 2 Apr. 2-4, Jun. 10-13 Dec. 1-3, 8
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.
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 Oct. 3 Jan. 25-27 Sep. 19, 22 Feb. 28-Mar. 2 Apr. 2-4, Jun. 10-13 Dec. 1-3, 8

preferences:
202.62 ms | 409 KiB | 5 Q