开发者

json date object to php date

开发者 https://www.devze.com 2023-01-09 12:09 出处:网络
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.

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 object is {"DateOfBirth":"/Date(387518400000-0400)/"} this date is 02-15-开发者_StackOverflow社区1982.

The webservice which I am calling is in .NET, and it converts the date to the JSON object. Not sure if this would help.

Thanks in advance

Thanks, Tanmay


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


@Brombomb

Your Function works fine, but there is one thing you forgot. Timestamps can be negative for Dates before the 01.01.1970, so we need a different regEx

I used this one and it works fine:

preg_match( '/\/Date\((-?\d+)([+-]\d{4})\)/', $date, $matches);

At the end i modified your Function a little bit to become more usefull for me. Now i can decide if i get the Date back as

  • "date" => date only
  • "time" => time only
  • "datetime" => date and time
  • "array" => date and time as an array
  • "string" => the timestamp as a string

and i can decide if i want the difference to the UTC timezone added/substracted when i give the third parameter...

function parseJsonDate($date, $type = 'date', $utc = 0) {
    // Match the time stamp (microtime) and the timezone offset (may be + or -) and also negative Timestamps
    preg_match( '/\/Date\((-?\d+)([+-]\d{4})\)/', $date, $matches);

    $seconds  = $matches[1]/1000;                // microseconds to seconds
    $UTCSec   = $matches[2]/100*60*60;           // utc timezone difference in seconds

    if($utc != 0){
        $seconds  = $seconds + $UTCSec; // add or divide the utc timezone difference
    }

    $date     = date( 'Y-m-d', $seconds );       // only date
    $dateTime = date( 'Y-m-d H:i:s', $seconds ); // date and time
    $time     = date( 'H:i:s', $seconds );       // only time

    switch($type)
    {
        case 'date':
            return $date; // returns 'YYYY-MM-DD'
            break;

        case 'datetime':
            return $dateTime; // returns 'YYYY-MM-DD HH:ii:ss'
            break;

        case 'time':
            return $time; // returns 'HH:ii:ss'
            break;

        case 'array':
            $dateArray = str_replace(" ", "-", $dateTime);
            $dateArray = str_replace(":", "-", $dateArray);
            return explode('-', $dateArray); // return array('YYYY', 'MM', 'DD', 'HH', 'ii', 'SS')
            break;

        case 'string':
            return $matches[1] . $matches[2]; // returns 1336197600000-0600
            break;
    }
}


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


You can use this package to parse the JSON dates:

https://github.com/webapix/dot-net-json-date-formatter

use \Webapix\DotNetJsonDate\Date;

Date::toDateTime('/Date(387518400000-0400)/'); // return with \DateTime object


@Brombomb in my case I added to your function an other parameter to return a Date Object:

public function parseJsonDate( $date, $type = 'date' )
{
    // removes extra millisecond digits in an other reg exp class
    preg_match( '/\/Date\((\d{10})\d+([+-]\d{4})\)/', $date, $matches ); // Match the time stamp (microtime) and the timezone offset (may be + or -)

    $date = date( 'm-d-Y', $matches[1] );

    switch( $type )
    {
        case 'dateTimezone':
            return DateTime::createFromFormat( 'UT', $matches[1] . $matches[2] );

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号