Skip to content

Instantly share code, notes, and snippets.

@cviebrock
Created September 17, 2013 18:00
Show Gist options
  • Select an option

  • Save cviebrock/6598105 to your computer and use it in GitHub Desktop.

Select an option

Save cviebrock/6598105 to your computer and use it in GitHub Desktop.
Convert `"/Date()/"` dates from JSON to DateTime objects.
function convertJsonDate( $string ) {
if ( preg_match('#^/Date\((-?\d+)([+-]\d+)?\)/$#', $string, $matches) ) {
$ds = intval( $matches[1] ) / 1000;
$dm = intval( $matches[1] ) % 1000;
$tz = '+000';
if ( isset($matches[2]) ) {
$tz = $matches[2];
}
if ( $dt = \DateTime::createFromFormat('U u O', $ds.' '.$dm.' '.$tz) ) {
$string = $dt;
}
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment