4

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

1
  • Did you tried json_decode($yourDate)? Commented Nov 28, 2015 at 20:31

5 Answers 5

5

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');
Sign up to request clarification or add additional context in comments.

1 Comment

A timestamp represents an absolute reference to a second or microsecond in time. Timezone is not relevant here. You would use timezone when representing the timestamp as a string in a local timezone.
4

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

3 Comments

$times="/Date(1446739002960)/"; $timestamp = json_decode($times); $date = date("Y-m-d H:i:s", $timestamp); echo $date;
better go with Hasse Björk's answer. You need to remove all non numeric characters. And btw the timestamp is probaly wrong. You will get something like 47815-05-03 10:05:00
You need to divide the JS-date with 1000 to get PHP-date... and change json_decode($dateJSON, true); to get an array instead of an object.
3

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

3 Comments

work good, but the dates that I get after decode are invalids... try please 753343200000, it need to be 15/11/1993
please see my javascript code in the question @Hasse
Ok, I changed the date format to your desired format dd/mm/yyyy and fixed a miss when converting JS-date to PHP-date. JavaScript is in milliseconds and PHP in seconds.
2

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.