Idan Neeman
Idan Neeman

Reputation: 119

How to decode JSON date using php?

I get from JSON output the date in JSON encrypt, I want to decode it when I insert it to mysql.

I insert the output:

"date": "/Date(1446739002960)/"

to $dateparse variable

I was write the solution using javascript:

var dateString = "\/Date(753343200000)\/".substr(6);
var currentTime = new Date(parseInt(dateString ));
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var date = day + "/" + month + "/" + year;
alert(date);

How can I decode the variable content using php? thank you

Upvotes: 4

Views: 15602

Answers (5)

zsocakave
zsocakave

Reputation: 66

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(1446739002960)/'); // return with \DateTime object

Upvotes: 2

Justin Levene
Justin Levene

Reputation: 1677

First problem is the JSON date can also be in the format '/Date(1511431604000+0000)/', so we need to ignore the +0000 if it exists, otherwise if we use preg_replace( '/[^0-9]/', '', $json_date), 0000 is added to the timestamp:

$json_date = '/Date(1511431604000+0000)/'; // or $json_date = '/Date(1511431604000)/';   

// Parse date to get the timestamp and the timezone if set
preg_match('/\/Date\(([0-9]+)(\+[0-9]+)?/', $json_date, $time_array);

// Remove milliseconds from JSON timestamp to make it a Unix timestamp
$unix_timestamp = intval($time_array[1] / 1000);

Upvotes: 0

dgpro
dgpro

Reputation: 366

Don't forget about timezone as JavaScript date string may include it and here is how you can parse it for both cases

// Let's assume you did JSON parsing and got your date string
$date = '/Date(1511431604000+0000)/';

// Parse the date to get timestamp and timezone if applicable
preg_match('/\/Date\(([0-9]+)(\+[0-9]+)?/', $date, $time);

// remove milliseconds from timestamp
$ts = $time[1] / 1000;
// Define Time Zone if exists
$tz = isset($time[2]) ? new DateTimeZone($time[2]) : null;

// Create a new date object from your timestamp
// note @ before timestamp
// and don't specify timezone here as it will be ignored anyway
$dt = new DateTime('@'.$ts);

// If you'd like to apply timezone for whatever reason
if ($tz) {
  $dt->setTimezone($tz);
}

// Print your date
print $dt->format('Y-m-d H:i:s');

Upvotes: 5

Bene
Bene

Reputation: 1281

1) Fix your JSON

$dateJSON ='{"date": "/Date(1446739002960)/"}'

2) Decode your JSON

$timestamp = json_decode($dateJSON, true);

3) Remove all non numeric characters

$timestamp = preg_replace( '/[^0-9]/', '', $timestamp['date'])

4) Transform timestamp (divided by 1000 cause of JS date) to a human-readable format

$date = date("Y-m-d H:i:s", $timestamp / 1000);

Upvotes: 4

Hasse Björk
Hasse Björk

Reputation: 1601

First of all, your json code is not complete, you are missing the curly brackets needs to be filtered and passed through the json_decode and time functions:

// Curly brackets where missing
$obj = json_decode( '{"date": "/Date(753343200000)/"}' );

// Remove non numerical characters
$obj->date = preg_replace( '/[^0-9]/', '', $obj->date );

// Divide javascript date (in ms) with 1000 to get UNIX date (seconds)
// Convert the date as DD-MM-YYYY 
$dateparse = date( 'd/m/Y', ( $obj->date / 1000 ) );
echo $dateparse;

EDIT Updated the date and corrected for JavaScript date

If you want time too, use date( 'd/m/Y H:i:s', ( $obj->date / 1000 ) )

Upvotes: 3

Related Questions