3v4l.org

run code in 300+ PHP versions simultaneously
<?php $ss = 'first day of this month midnight'; $es = 'first day of next month midnight - 1 second'; $s = new DateTime($ss); $e = new DateTime($es); $d= $e->diff($s); var_dump($d->days); // 0 ... but should be 30 // This should be 0 because: // * "first day of last month midnight" is 2016-12-01T00:00:00" // * "first day of this month midnight - 1 second" is 2016-12-01T23:59:59+11:00 // because right to left. First the expression "midnight -1 second" "first day of this month" $s = (new DateTime(null))->setTimestamp(strtotime($ss)); // verbose setup method $e = (new DateTime(null))->setTimestamp(strtotime($es)); // verbose setup method $d = $e->diff($s); var_dump($d->days); // 30 ... and should be 30 /* Next we will try mix/match the code to see what happens, surprisingly it seems that the end date ($e) is the important one, if it uses the verbose method it returns the correct values. */ $s = (new DateTime(null))->setTimestamp(strtotime($ss)); // verbose setup method $e = new DateTime($es); $d= $e->diff($s); var_dump($d->days); // 0 ... but should be 30 $s = new DateTime($ss); $e = (new DateTime(null))->setTimestamp(strtotime($es)); // verbose setup method $d= $e->diff($s); var_dump($d->days); // 30 ... and should be 30 /* This test just proves that the $e date is important BUT NOT because it's the one we call the diff() method on, that's just coincidental that seems to imply that the "- 1 second" in the date string is the problem. */ $s = new DateTime($ss); $e = (new DateTime(null))->setTimestamp(strtotime($es)); // verbose setup method $d= $s->diff($e); var_dump($d->days); // 30 ... and should be 30 /* [Workaround] This final test seems to prove that the input string is important and that the "- 1 secord" has a negative knock-on effect on the results of the diff. By modifying the datetime with ->modify everything works as expected ... it just means you have to be careful of how we work with DateTimes . */ $s = new DateTime($ss); $e = new DateTime('first day of this month midnight'); $e->modify('- 1 second'); var_dump($e->diff($s)->days); // 30 ... and should be 30 echo "\n\n Same stuff with DateTimeImmutable\n"; $s = new DateTimeImmutable($ss); $e = new DateTimeImmutable($es); $d= $e->diff($s); var_dump($d->days); // 0 ... but should be 30 $s = (new DateTimeImmutable(null))->setTimestamp(strtotime($ss)); // verbose setup method $e = (new DateTimeImmutable(null))->setTimestamp(strtotime($es)); // verbose setup method $d = $e->diff($s); var_dump($d->days); // 30 ... and should be 30 /* Next we will try mix/match the code to see what happens, surprisingly it seems that the end date ($e) is the important one, if it uses the verbose method it returns the correct values. */ $s = (new DateTimeImmutable(null))->setTimestamp(strtotime($ss)); // verbose setup method $e = new DateTimeImmutable($es); $d= $e->diff($s); var_dump($d->days); // 0 ... but should be 30 $s = new DateTimeImmutable($ss); $e = (new DateTimeImmutable(null))->setTimestamp(strtotime($es)); // verbose setup method $d= $e->diff($s); var_dump($d->days); // 30 ... and should be 30 /* This test just proves that the $e date is important BUT NOT because it's the one we call the diff() method on, that's just coincidental that seems to imply that the "- 1 second" in the date string is the problem. */ $s = new DateTimeImmutable($ss); $e = (new DateTimeImmutable(null))->setTimestamp(strtotime($es)); // verbose setup method $d= $s->diff($e); var_dump($d->days); // 30 ... and should be 30 /* [Workaround] This final test seems to prove that the input string is important and that the "- 1 secord" has a negative knock-on effect on the results of the diff. By modifying the datetime with ->modify everything works as expected ... it just means you have to be careful of how we work with DateTimes . */ $s = new DateTimeImmutable($ss); $e = new DateTimeImmutable('first day of this month midnight'); $e->modify('- 1 second'); var_dump($e->diff($s)->days); // 30 ... and should be 30

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.3.40.0120.00318.98
8.3.30.0120.00318.92
8.3.20.0000.00819.13
8.3.10.0040.00421.91
8.3.00.0080.00019.50
8.2.170.0160.00622.96
8.2.160.0090.00621.23
8.2.150.0050.00324.18
8.2.140.0030.00524.66
8.2.130.0040.00426.16
8.2.120.0040.00419.88
8.2.110.0060.00320.83
8.2.100.0090.00318.16
8.2.90.0030.00618.41
8.2.80.0060.00320.58
8.2.70.0000.00818.00
8.2.60.0000.00818.00
8.2.50.0000.00818.18
8.2.40.0130.00018.30
8.2.30.0000.00819.52
8.2.20.0040.00418.26
8.2.10.0060.00318.27
8.2.00.0040.00418.25
8.1.270.0000.00823.98
8.1.260.0040.00426.35
8.1.250.0090.00028.09
8.1.240.0030.00622.34
8.1.230.0070.00422.22
8.1.220.0040.00418.01
8.1.210.0040.00418.95
8.1.200.0030.00617.60
8.1.190.0000.00917.61
8.1.180.0040.00418.10
8.1.170.0090.00017.62
8.1.160.0000.00719.00
8.1.150.0000.00820.41
8.1.140.0080.00019.79
8.1.130.0040.00419.23
8.1.120.0040.00417.60
8.1.110.0080.00017.69
8.1.100.0000.00817.58
8.1.90.0000.00817.62
8.1.80.0000.00717.79
8.1.70.0030.00317.81
8.1.60.0050.00317.68
8.1.50.0060.00317.68
8.1.40.0030.00517.63
8.1.30.0000.00917.77
8.1.20.0060.00317.75
8.1.10.0030.00617.81
8.1.00.0050.00317.78
8.0.300.0050.00320.14
8.0.290.0090.00317.13
8.0.280.0030.00318.66
8.0.270.0000.00717.38
8.0.260.0000.00717.47
8.0.250.0030.00317.21
8.0.240.0030.00317.14
8.0.230.0000.00717.10
8.0.220.0000.00717.18
8.0.210.0070.00017.19
8.0.200.0050.00317.21
8.0.190.0000.00817.24
8.0.180.0000.00717.12
8.0.170.0040.00417.21
8.0.160.0040.00417.18
8.0.150.0080.00017.07
8.0.140.0000.00717.14
8.0.130.0000.00513.61
8.0.120.0040.00417.14
8.0.110.0070.00017.21
8.0.100.0040.00417.06
8.0.90.0030.00517.14
8.0.80.0090.00917.09
8.0.70.0000.00917.20
8.0.60.0050.00317.06
8.0.50.0040.00417.02
8.0.30.0080.00817.44
8.0.20.0140.00817.52
8.0.10.0070.00017.25
8.0.00.0110.00716.84
7.4.330.0000.00615.55
7.4.320.0030.00316.88
7.4.300.0000.00716.86
7.4.290.0000.00816.86
7.4.280.0050.00316.86
7.4.270.0030.00516.70
7.4.260.0000.00816.76
7.4.250.0040.00416.63
7.4.240.0000.00716.80
7.4.230.0030.00516.85
7.4.220.0120.00916.96
7.4.210.0070.00716.82
7.4.200.0050.00216.90
7.4.160.0090.01116.69
7.4.150.0070.01117.40
7.4.140.0130.00917.86
7.4.130.0090.00816.75
7.4.120.0110.01116.80
7.4.110.0070.01016.64
7.4.100.0160.00916.87
7.4.90.0080.01117.01
7.4.80.0090.00919.39
7.4.70.0100.00616.75
7.4.60.0060.00916.86
7.4.50.0130.00616.59
7.4.40.0030.01416.98
7.4.30.0060.01016.96
7.4.00.0030.01415.17
7.3.330.0000.00513.32
7.3.320.0000.01113.14
7.3.310.0000.00816.24
7.3.300.0030.00316.38
7.3.290.0090.00716.41
7.3.280.0090.00916.48
7.3.270.0130.00317.40
7.3.260.0150.00916.64
7.3.250.0080.00916.64
7.3.240.0130.00516.54
7.3.230.0070.01116.65
7.3.210.0060.01916.72
7.3.200.0030.01819.39
7.3.190.0060.01216.71
7.3.180.0080.00916.69
7.3.170.0250.00016.69
7.3.160.0150.00616.76
7.3.10.0130.01316.13
7.3.00.0150.01216.48
7.2.330.0070.01617.04
7.2.320.0090.01516.79
7.2.310.0000.01916.83
7.2.300.0030.01516.98
7.2.290.0030.01417.07
7.2.130.0140.00816.38
7.2.120.0240.01217.19
7.2.110.0120.00916.56
7.2.100.0240.00316.88
7.2.90.0110.00816.88
7.2.80.0140.01416.89
7.2.70.0150.00916.68
7.2.60.0140.00116.89
7.2.50.0200.00616.46
7.2.40.0080.01716.18
7.2.30.0180.00616.42
7.2.20.0120.00616.77
7.2.10.0250.00316.52
7.2.00.0110.00817.04
7.1.250.0200.01416.11
7.1.200.0060.00916.13
7.1.100.0030.01718.25
7.1.70.0220.00017.31
7.1.60.0030.01017.52
7.1.50.0080.01717.14
7.1.00.2930.07320.77
7.0.200.0000.01517.08
7.0.140.1670.07020.27
7.0.130.1570.05020.51
7.0.120.1830.06320.39
7.0.110.1830.07020.30
7.0.100.1670.07720.28
7.0.90.1900.05320.27
7.0.80.2030.07720.24
7.0.70.1700.06320.39
7.0.60.1800.07020.43
7.0.50.1370.07020.32
7.0.40.1470.05720.29
7.0.30.1300.07320.43
7.0.20.1830.07320.19
7.0.10.1200.05720.25
7.0.00.1100.06020.44
5.6.290.0230.05320.95
5.6.280.0070.06321.17
5.6.270.0200.05320.95
5.6.260.0200.05720.82
5.6.250.0270.06320.96
5.6.240.0200.05320.86
5.6.230.0130.06721.15
5.6.220.0230.07321.15
5.6.210.0070.06321.01
5.6.200.0130.06321.02
5.6.190.0230.07720.93
5.6.180.0200.08021.07
5.6.170.0230.05020.93
5.6.160.0200.05021.09
5.6.150.0070.08020.81
5.6.140.0100.08721.12
5.6.130.0170.07721.06
5.6.120.0170.08321.09
5.6.110.0230.07020.88
5.6.100.0270.06021.06
5.6.90.0130.08320.91
5.6.80.0070.06320.42
5.6.70.0200.06720.49
5.6.60.0170.08320.47
5.6.50.0100.06020.50
5.6.40.0230.05720.28
5.6.30.0100.08020.26
5.6.20.0200.06720.28
5.6.10.0130.06020.27
5.6.00.0200.05320.32

preferences:
55.61 ms | 400 KiB | 5 Q