1

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?

4 Answers 4

3
$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.

5
  • The js library i am using will return an error if a string is passed instead of an int.. Commented Feb 23, 2012 at 10:15
  • As I commented on another answer here. Look in the source of the library. Find the function where it uses the timestamp and do a workaround like this var timestamp = parseInt(json.timestamp) * 1000; Commented Feb 23, 2012 at 10:16
  • For reference, I passing the json to the highcharts charting lib. I would need to modify the library to make it work.. not ideal. Commented Feb 23, 2012 at 10:16
  • Weird, I solved the exact problem I had with timestamps a while back. It was also highscharts. I simply parsed the numbers as ints in the option-function where I set all the settings. Commented Feb 23, 2012 at 10:20
  • Went in and fixed the highcharts script. It worked. thanks :) Commented Feb 23, 2012 at 10:30
2

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

2
  • Unfortunately, the js library i am using trips on the value if it is a string instead of an int Commented Feb 23, 2012 at 10:12
  • 2
    put parseInt on the variable before it's used? Commented Feb 23, 2012 at 10:14
1

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

1
  • var timestamp = parseInt(json.timestamp) * 1000; Commented Feb 23, 2012 at 10:14
0

use it

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

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.