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

Question:

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

/Date(1533524400000-0300)/

How to convert to a DateTime in PHP?

Answer:

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

Scroll to Top