Reputation: 921
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
Upvotes: 0
Views: 5706
Reputation: 339816
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.
Upvotes: 1
Reputation: 9705
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))?>);
Upvotes: 0
Reputation: 490233
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;
Upvotes: 1