applechief
applechief

Reputation: 6895

PHP Passing a Javascript timestamp using json_encode

I am preparing data to pass to my Javascript in PHP. Encoding that data using json_encode and passing it.

One of the elements I am trying to pass is a javascript timestamp. PHP timestamps are in seconds, Javascript timestamps in milliseconds, so i need to multiply the php timestamp by 1000.

The problem is that the resulting int is too large and gets sent at 1.3239E+12 instead of 1323907200000 losing around 1 day of precision on the way.

One way to do it would be to pass a Date.UTC() function in the json but there's no easy way to do it using json_encode.

Any solution?

Upvotes: 1

Views: 1521

Answers (4)

Maicol Romero
Maicol Romero

Reputation: 9

use it

$time = date(strtotime($r->date)) *1000;

Upvotes: 0

OptimusCrime
OptimusCrime

Reputation: 14863

$jstimestamp = (string) time() + "000";

Try that. I solved a similar problem the same way. However, the easiest is to multiply in the Javascript or divide the js-timestamp with 1000.

Upvotes: 3

Jon
Jon

Reputation: 437376

You can always multiply after receiving (in Javascript), instead of before sending.

Upvotes: 1

Waynn Lue
Waynn Lue

Reputation: 11375

I've solved this problem in the past by encoding it in as a string, instead of as an integer.

Upvotes: 2

Related Questions