3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyDateTime extends \DateTime { public function add($interval) { $oldDay = (int)$this->format('d'); $this->setDate((int)$this->format('Y'), (int)$this->format('m'), 1); parent::add($interval); $maxDay = (int)$this->format('t'); if($oldDay > $maxDay){ $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $maxDay); } else { $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $oldDay); } return $this; } public function sub($interval) { $oldDay = (int)$this->format('d'); $this->setDate((int)$this->format('Y'), (int)$this->format('m'), 1); parent::sub($interval); $maxDay = (int)$this->format('t'); if($oldDay > $maxDay){ $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $maxDay); } else { $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $oldDay); } return $this; } public function diff($dateTime2, $absolute = false) { if((int)$this->format('t') > (int)$dateTime2->format('t')){ $this->setDate((int)$this->format('Y'), (int)$this->format('m'), (int)$dateTime2->format('t')); } if((int)$this->format('t') < (int)$dateTime2->format('t')){ $dateTime2->setDate((int)$dateTime2->format('Y'), (int)$dateTime2->format('m'), (int)$this->format('t')); } return parent::diff($dateTime2, $absolute); } } $myDateTime1 = new MyDateTime('30th November 2012'); $myDateTime2 = new MyDateTime('28th February 2013'); $interval = $myDateTime2->diff($myDateTime1, true); var_dump($interval); var_dump($myDateTime1->add($interval));
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.30, 7.4.0 - 7.4.24, 8.0.0 - 8.0.11
object(DateInterval)#3 (16) { ["y"]=> int(0) ["m"]=> int(3) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["f"]=> float(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> int(92) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } object(MyDateTime)#1 (3) { ["date"]=> string(26) "2013-02-28 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.4.30 - 5.4.45, 5.5.14 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33
object(DateInterval)#3 (15) { ["y"]=> int(0) ["m"]=> int(3) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> int(92) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } object(MyDateTime)#1 (3) { ["date"]=> string(26) "2013-02-28 00:00:00.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.3.27 - 5.3.29, 5.4.17 - 5.4.29, 5.5.0 - 5.5.13
object(DateInterval)#3 (15) { ["y"]=> int(0) ["m"]=> int(3) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["weekday"]=> int(0) ["weekday_behavior"]=> int(0) ["first_last_day_of"]=> int(0) ["invert"]=> int(0) ["days"]=> int(92) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } object(MyDateTime)#1 (3) { ["date"]=> string(19) "2013-02-28 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.3.0 - 5.3.26, 5.4.0 - 5.4.16
object(DateInterval)#3 (8) { ["y"]=> int(0) ["m"]=> int(3) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(0) ["s"]=> int(0) ["invert"]=> int(0) ["days"]=> int(92) } object(MyDateTime)#1 (3) { ["date"]=> string(19) "2013-02-28 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "Europe/Amsterdam" }
Output for 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vkpO2 on line 2 Fatal error: Call to undefined method DateTime::diff() in /in/vkpO2 on line 40
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vkpO2 on line 2 Fatal error: Class 'DateTime' not found in /in/vkpO2 on line 2
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vkpO2 on line 2 Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vkpO2 on line 4
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vkpO2 on line 2 Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/vkpO2 on line 4
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/vkpO2 on line 2 Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/vkpO2 on line 4
Process exited with code 255.

preferences:
235.7 ms | 402 KiB | 372 Q