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
This timestamp is in milliseconds, which is why it's so large.
You can use the PHP date() call to format this timestamp as you wish. Just divide by 1,000 first. In standard US format, it would be
$mydate = date('m d Y', $timestamp);
(where $timestamp is 1365004652303)
To format it in the format you requested (Y-m-d H:i:s) you would use 'Y-m-d H:i:s' as the format string (first parameter). Chop off text starting with "-".
$stamps = preg_split("/-/", $time);
$stamps[0] = $stamps[0]/1000;
$mydate = date('Y-m-d H:i:s', $stamps[0]);
This yields 2013-04-03 11:57:32
Others have suggested the 0500 is an offset; if so, you'd want to adjust $stamps[0] accordingly.
This is what I'm using to parse timestamps from a Xero API. It accounts for:
Code:
if (preg_match('#^(/Date\()([-]?[0-9]+)([0-9]{3})([+-][0-9]{4})?(\)/)$#', $data, $matches)) {
// Handle Xero API DateTime formats. Examples:
// "/Date(1436961673000)/" - unix timestamp with milliseconds
// "/Date(1436961673000+0100)/" - with an additional timezone correction
// "/Date(-1436961673000-0530)/" - before the epoch, 1924 here
//
// RE matches for "/Date(1436961673090+0100)/":
// [1] = (/Date\() "/Date("
// [2] = ([-]?[0-9]+) "1436961673" epoch seconds
// [3] = ([0-9]{3}) "090" milliseconds
// [4] = ([+-][0-9]{4})? "+0100" or "" optional timezone
// [5] = (\)/) ")"
$result = \DateTime::createFromFormat('U u', $matches[2] . ' ' . $matches[3] . '000')
->setTimezone(new \DateTimeZone($matches[4] ?: '+0000'));
}
I had some slightly different experience which led me to make a couple of slight changes to Baba's excellent answer.
Using Newtonsoft's JSON library to encode messages in .NET, which I then send to our website (the PHP part), I get a space before the timezone offset, and no +/- character.
I have also seen negative numbers from pre-epoch dates which means I needed to cater for a - sign before the millisecond value.
I altered the regex to this and it works perfectly for me:
^/Date(([-]?\d{10})(\d{3})\s?([+-]?\d{4}))/$
The two differences are
[-]?
before the 10-digit millisecond value, and
\s?
before the timezone offset.
I would put this as a comment on Baba's answer, but my lack of reputation doesn't permit me. I hope this is appropriate for me to post here as I thought it might be useful.
The following example uses the preg_match()
and the DateTime
class:
$date = '/Date(1365004652303-0500)/';
// get the timestamp
$pattern = '~/Date\(([0-9]*)~';
preg_match($pattern, $date, $matches);
$timestamp = round(((int) $matches[1]) / 1000);
$dt = new DateTime();
$dt->setTimestamp($timestamp);
echo $dt->format('Y-m-d H:i:s');
Try this out:
var_dump(date('Y-m-d H:i:s', '1365004652303'/1000));
$str = '/Date(1365004652303-0500)/';
$match = preg_match('/\/Date\((\d+)([-+])(\d+)\)\//', $str, $date);
$timestamp = $date[1]/1000;
$operator = $date[2];
$hours = $date[3]*36; // Get the seconds
$datetime = new DateTime();
$datetime->setTimestamp($timestamp);
$datetime->modify($operator . $hours . ' seconds');
var_dump($datetime->format('Y-m-d H:i:s'));
Returns:
string(19) "2013-04-03 17:57:32"
string(19) "2013-04-03 12:57:32"
First you need to understand the format you have
/Date(1365004652303-0500)/
Then you have
Build a Format
$date = '/Date(1365004652303-0500)/';
preg_match('/(\d{10})(\d{3})([\+\-]\d{4})/', $date, $matches);
$dt = DateTime::createFromFormat("U.u.O",vsprintf('%2$s.%3$s.%4$s', $matches));
echo $dt->format('r');
Output
Wed, 03 Apr 2013 15:57:32 -0500
^
|= Can you see the GMT ?
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(1365004652303-0500)/';
$parser = new JsonTimestampWithOffsetParser;
$dt = $parser->parse($date);
echo $dt->format('r');