json date object to php date

前端 未结 5 1275
不知归路
不知归路 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:34

    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;
        }    
    }   
    

提交回复
热议问题