Leandro Garcia
Leandro Garcia

Reputation: 3228

What kind of date format is this?

I get this from a certain API:

/Date(-27734400000+0000)/

What format is it called? How can I convert it to a timestamp?

Upvotes: 4

Views: 182

Answers (1)

apokryfos
apokryfos

Reputation: 40653

For whatever reason C# seems to serialize DateTime objects like that. Normally its of the format /Date(Seconds since 01/01/1970)/ but the fact that this is negative (and so large) makes me suspect that there was an overflow somewhere. At any rate doing something like:

<?php
$str = "/Date(-27734400000+0000)/";
$strTime = substr($str,6,-2);
$date = new \DateTime();
$date->setTimestamp(intval($strTime));
//You can also do some more sophisticated things to set the timezone based on the +xxxx part
print_r($date);

This one prints (at http://sandbox.onlinephpfunctions.com/code/8e65641fa2aae0a3f49ce9c3f27457d7c6842b7b)

DateTime Object (
    [date] => 1091-02-17 16:00:00.000000
    [timezone_type] => 3
    [timezone] => US/Pacific
)

This makes no sense to me unless you're expecting some date in 1091. I think this is most likely an API bug you should inform the developers about.

Upvotes: 3

Related Questions