I am calling a webservice which return me back a json object. The json object encodes the date. I am trying to find a way to convert that date to m-d-Y format in php. Json
I know this question/answer is old, but I wanted to add that the @hookedonwinter answer is no longer correct. While his answer may have solved the specific solution, here in 2012 we now have an extra decimal place to take care of.
echo parseJsonDate('/Date(1336197600000-0600)/', 'date');
public 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;
}
}