3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface DateFormatParser { /** * @param $string * * @return DateTime */ public function parse($string); } abstract class PregDateParser implements DateFormatParser { protected $pattern, $format, $mask; public function parse($string) { $string = (string)$string; $pattern = $this->pattern; $format = $this->format; $mask = $this->mask; $r = preg_match($pattern, $string, $matches); if (!$r) { throw new UnexpectedValueException('Preg Regex Pattern failed.'); } $buffer = vsprintf($mask, $matches); $result = DateTime::createFromFormat($format, $buffer); if (!$result) { throw new UnexpectedValueException(sprintf('Failed To Create from Format "%s" for "%s".', $format, $buffer)); } return $result; } } class JsonTimestampWithOffsetParser extends PregDateParser { protected $pattern = '/^\/Date\((\d{10})(\d{3})([+-]\d{4})\)\/$/'; protected $format = 'U.u.O'; protected $mask = '%2$s.%3$s.%4$s'; } $date = '/Date(1440960660000-0200)/'; $parser = new JsonTimestampWithOffsetParser; $dt = $parser->parse($date); //$dt->modify(); $timezoneShift = $dt->format('Z'); $timezoneShift = ($timezoneShift > 0) ? '+' . $timezoneShift : '-' . $timezoneShift; echo $dt->modify($timezoneShift)->format('Y-m-d H:i:s');
Output for 8.3.0 - 8.3.4
Fatal error: Uncaught DateMalformedStringException: DateTime::modify(): Failed to parse time string (--7200) at position 0 (-): Unexpected character in /in/9QJVq:52 Stack trace: #0 /in/9QJVq(52): DateTime->modify('--7200') #1 {main} thrown in /in/9QJVq on line 52
Process exited with code 255.
Output for 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17
Warning: DateTime::modify(): Failed to parse time string (--7200) at position 0 (-): Unexpected character in /in/9QJVq on line 52 Fatal error: Uncaught Error: Call to a member function format() on bool in /in/9QJVq:52 Stack trace: #0 {main} thrown in /in/9QJVq on line 52
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33
Warning: DateTime::modify(): Failed to parse time string (--7200) at position 0 (-): Unexpected character in /in/9QJVq on line 52 Fatal error: Uncaught Error: Call to a member function format() on boolean in /in/9QJVq:52 Stack trace: #0 {main} thrown in /in/9QJVq on line 52
Process exited with code 255.
Output for 5.6.8 - 5.6.28
Warning: DateTime::modify(): Failed to parse time string (--7200) at position 0 (-): Unexpected character in /in/9QJVq on line 52 Fatal error: Call to a member function format() on boolean in /in/9QJVq on line 52
Process exited with code 255.
Output for 5.3.3 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35
Warning: DateTime::modify(): Failed to parse time string (--7200) at position 0 (-): Unexpected character in /in/9QJVq on line 52 Fatal error: Call to a member function format() on a non-object in /in/9QJVq on line 52
Process exited with code 255.
Output for 5.3.1 - 5.3.2
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'Failed To Create from Format "U.u.O" for "1440960660.000.-0200".' in /in/9QJVq:32 Stack trace: #0 /in/9QJVq(47): PregDateParser->parse('/Date(144096066...') #1 {main} thrown in /in/9QJVq on line 32
Process exited with code 255.
Output for 5.3.0
2015-08-30 18:51:00
Output for 5.2.10 - 5.2.17
Fatal error: Call to undefined method DateTime::createFromFormat() in /in/9QJVq on line 30
Process exited with code 255.
Output for 5.2.0 - 5.2.9
Fatal error: Call to undefined method DateTime::createfromformat() in /in/9QJVq on line 30
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/9QJVq on line 30
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING in /in/9QJVq on line 3
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
Parse error: parse error, unexpected T_STRING in /in/9QJVq on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/9QJVq on line 3
Process exited with code 255.

preferences:
213.45 ms | 401 KiB | 307 Q