Json date object to php date

I am calling a web service that returns a json object to me. The json object encodes a date. I am trying to find a way to convert this date to mdY format in php. The Json object is {"DateOfBirth": "/ Date (387518400000-0400) /"} this date is 02-15-1982.

The web service that I am calling is in .NET and it will convert the date to a JSON object. Not sure if this will help.

Thanks in advance

Thanks Tanmay

+3
source share
4 answers

, 02-15-1982, 04-12-1982, . , 60 , .

:

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] );

: 04-12-1982

+1

, / , , @hookedonwinter . , , , , 2012 , , .

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

@Brombomb

, , . 01.01.1970, regEx

, :

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

, . ,

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

and I can decide if I want the difference with the UTC time zone to be added / subtracted 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;
    }
}
+3
source

@Brombomb in my case, I added another parameter to your function 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
source

Source: https://habr.com/ru/post/1756644/


All Articles