How to convert /Date(1533524400000-0300)/ to PHP

Asked

Viewed 162 times

3

I receive as return from a JSON the date in this format:

/Date(1533524400000-0300)/

How to Connect to a DateTime in PHP?

  • But is that a date? What kind of date?

  • @Diegosouza I don’t know if there’s an official name, but this format is called Microsoft JSON date (perhaps because MS has chosen to use this format in its JSON Apis). In this case, 1533524400000 is Unix timestamp in milliseconds, and -0300 is offset (in this case, 3 hours before UTC). In my opinion, one of the worst date formats ever created...

1 answer

0

Use this function I found on Soen:

echo parseJsonDate('/Date(1533524400000-0300)/', 'date');

function parseJsonDate($date, $type = 'date') {
    preg_match( '/\/Date\((\d+)([+-]\d{4})\)/', $date, $matches); // Match the time stamp (microtime) and the timezone offset (may be + or -)
    $date = date( 'm-d-Y', $matches[1]/1000 ); // convert to seconds from microseconds
    switch($type){    
        case 'date':
            return $date; // returns '05-04-2012'
            break;

        case 'array':
            return explode('-', $date); // return array('05', '04', '2012')
            break;

        case 'string':
            return $matches[1] . $matches[2]; // returns 1336197600000-0600
            break;
    }
}

08-06-2018

Browser other questions tagged

You are not signed in. Login or sign up in order to post.