3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* In the "verbose setup method" I'm trying setup the DateTime object myself to see if it's the format string which is parsed in correctly or if it's the DateTime object which is breaking stuff. From the testing it appears DateTime is broken somehow. */ $ss = 'first day of last month midnight'; $es = 'first day of this month midnight - 1 second'; $s = new DateTime($ss); //$s->setTimeZone(new DateTimeZone('UTC')); $e = new DateTime($es); //$e->setTimeZone(new DateTimeZone('UTC')); var_dump($s); var_dump($e); $d= $e->diff($s); var_dump($d->days); // 0 ... but should be 30 $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
Output for git.master, git.master_jit, rfc.property-hooks
object(DateTime)#1 (3) { ["date"]=> string(26) "2016-12-01 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } object(DateTime)#2 (3) { ["date"]=> string(26) "2016-12-31 23:59:59.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" } int(30) Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/XqkRH on line 17 Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/XqkRH on line 18 int(30) Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/XqkRH on line 25 int(30) Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/XqkRH on line 30 int(30) Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /in/XqkRH on line 38 int(30) int(30)

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
56.64 ms | 403 KiB | 8 Q