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"; }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.10.0060.00217.07
8.5.00.0110.01021.94
8.4.150.0030.00014.05
8.4.140.0130.00817.51
8.4.130.0070.01218.99
8.4.120.0100.00619.66
8.4.110.0120.00818.90
8.4.100.0120.01118.88
8.4.90.0050.00420.51
8.4.80.0110.01018.88
8.4.70.0130.00818.89
8.4.60.0040.00718.99
8.4.50.0100.01018.81
8.4.40.0140.00717.85
8.4.30.0040.01520.52
8.4.20.0130.00719.70
8.4.10.0060.00319.01
8.3.280.0120.01418.81
8.3.270.0130.01116.73
8.3.260.0110.00916.82
8.3.250.0110.00818.33
8.3.240.0110.00716.86
8.3.230.0120.00816.65
8.3.220.0100.00919.02
8.3.210.0150.00616.91
8.3.200.0060.00416.97
8.3.190.0070.00817.44
8.3.180.0040.00419.01
8.3.170.0190.00018.87
8.3.160.0150.00317.45
8.3.150.0040.01517.43
8.3.140.0080.00020.71
8.3.130.0050.00518.65
8.3.120.0060.00318.92
8.3.110.0040.00420.94
8.3.100.0110.00016.61
8.3.90.0110.00426.77
8.3.80.0030.00617.00
8.3.70.0070.00716.88
8.3.60.0140.00418.39
8.3.50.0100.01018.18
8.3.40.0120.00319.29
8.3.30.0070.00718.88
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0000.00826.16
8.2.290.0120.00720.50
8.2.280.0050.00318.30
8.2.270.0110.00716.68
8.2.260.0000.00816.75
8.2.250.0080.00017.00
8.2.240.0100.01017.24
8.2.230.0100.00622.58
8.2.220.0000.00824.06
8.2.210.0050.00326.77
8.2.200.0090.00016.75
8.2.190.0130.00616.88
8.2.180.0200.00725.92
8.2.170.0150.00419.04
8.2.160.0120.00322.96
8.2.150.0000.00825.66
8.2.140.0050.00224.66
8.2.130.0040.00426.16
8.2.120.0000.00826.16
8.2.110.0040.00422.11
8.2.100.0080.00418.28
8.2.90.0030.00519.34
8.2.80.0000.00817.97
8.2.70.0080.00517.75
8.2.60.0030.00517.93
8.2.50.0040.00418.07
8.2.40.0040.00420.07
8.2.30.0000.00818.32
8.2.20.0030.00519.56
8.2.10.0040.00419.18
8.2.00.0040.00419.48
8.1.330.0100.00922.30
8.1.320.0100.01116.32
8.1.310.0090.00016.35
8.1.300.0060.00320.36
8.1.290.0060.00330.84
8.1.280.0150.00025.92
8.1.270.0040.00423.99
8.1.260.0090.00026.35
8.1.250.0030.00528.09
8.1.240.0030.00619.57
8.1.230.0070.00320.49
8.1.220.0030.00518.01
8.1.210.0000.00818.77
8.1.200.0070.00317.60
8.1.190.0060.00317.72
8.1.180.0040.00418.10
8.1.170.0030.00618.84
8.1.160.0040.00418.93
8.1.150.0050.00318.91
8.1.140.0040.00417.62
8.1.130.0000.00817.71
8.1.120.0050.00217.71
8.1.110.0000.00717.63
8.1.100.0070.00017.64
8.1.90.0040.00417.62
8.1.80.0030.00517.60
8.1.70.0030.00317.64
8.1.60.0050.00317.82
8.1.50.0030.00617.62
8.1.40.0000.00717.72
8.1.30.0060.00317.71
8.1.20.0030.00617.84
8.1.10.0030.00517.73
8.1.00.0030.00617.75
8.0.300.0030.00618.77
8.0.290.0040.00417.13
8.0.280.0060.00318.59
8.0.270.0040.00418.26
8.0.260.0030.00317.14
8.0.250.0050.00217.26
8.0.240.0040.00417.17
8.0.230.0050.00217.25
8.0.220.0030.00317.13
8.0.210.0040.00417.10
8.0.200.0070.00017.32
8.0.190.0040.00417.17
8.0.180.0040.00417.14
8.0.170.0030.00617.14
8.0.160.0040.00417.18
8.0.150.0030.00517.07
8.0.140.0080.00317.10
8.0.130.0070.00013.59
8.0.120.0050.00317.07
8.0.110.0040.00917.23
8.0.100.0080.00617.16
8.0.90.0090.00517.23
8.0.80.0080.00617.09
8.0.70.0120.00317.12
8.0.60.0070.00817.25
8.0.50.0090.00517.20
8.0.30.0120.00217.12
8.0.20.0140.00017.21
8.0.10.0020.01217.29
8.0.00.0100.00317.25
7.4.330.0030.00315.55
7.4.320.0070.00016.62
7.4.300.0000.00816.68
7.4.290.0040.00416.63
7.4.280.0060.00316.70
7.4.270.0080.00016.62
7.4.260.0030.00313.42
7.4.250.0040.00416.77
7.4.240.0050.00716.78
7.4.230.0060.00616.66
7.4.220.0000.01016.78
7.4.210.0000.01016.60
7.4.200.0000.01016.67
7.4.190.0050.00516.75
7.4.180.0110.00016.84
7.4.160.0030.00816.68
7.4.150.0040.00816.72
7.4.140.0060.00616.64
7.4.130.0070.00516.71
7.4.120.0040.00716.57
7.4.110.0030.00716.61
7.4.100.0100.00016.68
7.4.90.0040.00816.66
7.4.80.0000.01016.56
7.4.70.0090.00216.64
7.4.60.0070.00416.70
7.4.50.0070.00416.54
7.4.40.0060.00616.61
7.4.30.0080.00316.70
7.4.20.0030.00816.68
7.4.10.0070.00416.48
7.4.00.0080.00316.76
7.3.330.0020.00516.41
7.3.320.0060.00013.40
7.3.310.0080.00316.41
7.3.300.0050.00516.50
7.3.290.0110.00016.37
7.3.280.0040.00716.55
7.3.270.0050.00516.47
7.3.260.0100.00016.38
7.3.250.0080.00516.48
7.3.240.0050.00516.46
7.3.230.0070.00316.36
7.3.220.0030.00716.52
7.3.210.0070.00316.46
7.3.200.0000.01016.55
7.3.190.0070.00316.50
7.3.180.0000.01016.34
7.3.170.0070.00316.61
7.3.160.0060.00616.51
7.3.150.0000.01116.54
7.3.140.0080.00316.52
7.3.130.0060.00616.41
7.3.120.0030.00916.57
7.3.110.0030.00816.49
7.3.100.0080.00316.49
7.3.90.0050.00616.71
7.3.80.0070.00316.50
7.3.70.0000.01016.59
7.3.60.0050.00516.66
7.3.50.0030.00716.64
7.3.40.0020.01016.47
7.3.30.0080.00316.50
7.3.20.0090.00316.66
7.3.10.0040.00716.59
7.3.00.0120.00516.66

preferences:
35.92 ms | 403 KiB | 5 Q