php时间戳到javascript时间或日期

i am trying to convert php timestamp to javascript time or date (basically to do a difference between 2 times and find it in seconds)

i am getting a timestamp from php as below:

2011-12-26 17:27:37

i am trying to do a difference of the above timestamp and current time in javascript and trying to print the time diff in seconds..

like:

CurrentTimeInJavaScript - JavaScriptConvertedTime(2011-12-26 17:27:37)

Edit

or how do we get the current/system time in javascript timestamp?

new Date().getTime() is getting the time from 1970 as expected!

I want it in say 20111226180000 for 2011 Dec 26 6 PM

The format of your date seems okay

you should be able to pass in directly to the date constructor

i.e.

var d = new Date('2011-12-26 17:27:37');

After you can compare the timestamp by using getTime method on the date object.

Create two Date objects in JS; one of the current date (new Date) and one of your server time (pass the server time, e.g. new Date('date string').

Then get the difference.

var diff = +date1 - +date2;

Well first of all 2011-12-26 17:27:37 is not a timestamp. You can convert it into a timestamp with strtotime.

The next to be aware of is that javascript timestamps are in milliseconds, so you'll have to multiply it by 1000. To get the timestamp in javascript, you use Date.getTime()

Putting it all together:

new Date().getTime() - new Date(<?php echo json_encode(strtotime(2011-12-26 17:27:37))?>);

Rather than the full string time format, use the output of PHP's date() function, which is just the number of seconds passed since midnight UTC on January 1st 1970.

The Javascript time is in the same format, but multiplied by 1000 because it has millisecond resolution.