json date object to php date

前端 未结 5 1272
不知归路
不知归路 2021-01-20 03:54

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

5条回答
  •  孤城傲影
    2021-01-20 04:38

    If there's a chance you said 02-15-1982 but really meant 04-12-1982, then I have a solution. If not, then there's a gap in the time of about 60 days, which can be accounted for with a bit more math.

    Here's my solution for now:

    date_default_timezone_set(  'America/Denver' );
    
    $json = json_decode( '{"DateOfBirth":"\/Date(387518400000-0400)\/"}' );
    $date_string = $json -> DateOfBirth;
    
    preg_match( '/([\d]{9})/', $date_string, $matches ); // gets just the first 9 digits in that string
    
    echo date( 'm-d-Y', $matches[0] );
    

    This returns: 04-12-1982

提交回复
热议问题