3v4l.org

run code in 300+ PHP versions simultaneously
<?php date_default_timezone_set('UTC'); $begin = new \DateTime('2012-06-25'); $end = new \DateTime('2012-07-27'); $interval = $begin->diff($end); $c = $interval; $seconds = (($end->getTimestamp()) - ($begin->getTimestamp())); // for DateTimeInterface::add() $base = new \DateTimeImmutable('2012-07-02'); $end = new \DateTime('2012-08-03'); $interval = $base->diff($end); $target = $base->add($interval); $comparison = ((new \DateTime())->setTimestamp((($base->getTimestamp()) + $seconds))); var_dump($target->format('Y-m-d')); var_dump($comparison->format('Y-m-d')); // for DateTimeInterval::sub() $base = new \DateTimeImmutable('2012-08-03'); $target = $base->sub($interval); $comparison = (new \DateTime())->setTimestamp($base->getTimestamp() - $seconds); var_dump($target->format('Y-m-d')); var_dump($comparison->format('Y-m-d')); var_dump($seconds, $interval, $c);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.26, 8.0.0 - 8.0.13, 8.1rc1 - rc3
string(10) "2012-08-03" string(10) "2012-08-03" string(10) "2012-07-02" string(10) "2012-07-02" int(2764800) object(DateInterval)#2 (16) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(1) ["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(32) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } object(DateInterval)#3 (16) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(2) ["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(32) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }
Output for 5.5.8 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33
string(10) "2012-08-03" string(10) "2012-08-03" string(10) "2012-07-02" string(10) "2012-07-02" int(2764800) object(DateInterval)#2 (15) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(1) ["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(32) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) } object(DateInterval)#3 (15) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(2) ["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(32) ["special_type"]=> int(0) ["special_amount"]=> int(0) ["have_weekday_relative"]=> int(0) ["have_special_relative"]=> int(0) }
Output for 5.5.0 - 5.5.7
Fatal error: DateTime::diff() must be derived from DateTimeImmutable::diff in Unknown on line 0
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Fatal error: Class 'DateTimeImmutable' not found in /in/MKeKE on line 11
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/MKeKE on line 15
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 5 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 11 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 12 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 15 Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/MKeKE on line 15
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, 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 5 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 11 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 12 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 15 Parse error: parse error, unexpected T_OBJECT_OPERATOR in /in/MKeKE on line 15
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 4 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 5 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 11 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 12 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/MKeKE on line 15 Parse error: parse error in /in/MKeKE on line 15
Process exited with code 255.

preferences:
63.77 ms | 545 KiB | 14 Q