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(); echo $dt->format('Z'); echo $dt->format('Y-m-d H:i:s');
Output for 5.3.0, 5.3.3 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
72002015-08-30 18:51:00
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/omkj9:32 Stack trace: #0 /in/omkj9(47): PregDateParser->parse('/Date(144096066...') #1 {main} thrown in /in/omkj9 on line 32
Process exited with code 255.
Output for 5.2.10 - 5.2.17
Fatal error: Call to undefined method DateTime::createFromFormat() in /in/omkj9 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/omkj9 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/omkj9 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/omkj9 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/omkj9 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/omkj9 on line 3
Process exited with code 255.

preferences:
245.81 ms | 401 KiB | 383 Q