I am calling an API from where I am getting date /Date(1365004652303-0500)/
, I don\'t understand what format this is. How is this date format called? I was not
Baba's answer is very helpful, yet it does not cover 3 cases I need:
This is what I use for getting the timestamp:
$date_string = '/Date(-594262800300+0100)/';
preg_match('/([+-]?\d+)([+-]\d{4})?/', $date_string, $matches);
$timestamp = $matches[1] / 1000;
$hours = $matches[2] * 36; // Get the seconds
return $timestamp + $hours;
Again, as Ghigo noted this is not correct if the offset contains minutes, but it works for my case.
You can use this package to parse the JSON dates:
https://github.com/webapix/dot-net-json-date-formatter
use \Webapix\DotNetJsonDate\Date;
$dateTime = Date::toDateTime('/Date(1365004652303-0500)/');
// return with \DateTime object, you can format it: $dateTime->format('Y-m-d H:i:s')
If your date is like /Date(-62135578800000)/
, a positive or negative integer without timezone:
$date = substr('/Date(-62135578800000)/', 6, -5);
$date = date('m/d/Y H:i:s', $date + date('Z', $date) * -1);
// 01/01/0001 05:00:00
Let's break /Date(1365004652303-0500)/
down to:
First string makes itself pretty clear.
The next large number is the epoch value
The -0500 represents the timezone in which the dates were originally stored. It is relative to UTC and thus, it is referring to Eastern Standard Time.
The epoch is with a milisecond precision. Try this code:
<?php
$str = "/Date(1365004652303-0500)/";
preg_match( "#/Date\((\d{10})\d{3}(.*?)\)/#", $str, $match );
echo date( "r", $match[1] );
?>
You can also use the timezone for setting the date relative to your own. http://codepad.viper-7.com/RrSkMy