rastacoding
rastacoding

Reputation: 25

javascript client side time to php servertime

Im having the hardest time trying to convert client side time to server time. The reason is I have a cron job executing once every hour. I manage to get the timezone offset in java script, but I'am clueless on how to apply the timezone offset. For instance, my timezone offset is 7. Ok!? than what? How should I apply this offset?

To get the offset I use

var offset = new Date().getTimezoneOffset();

Server side is handled by php.

Upvotes: 1

Views: 510

Answers (1)

jeff
jeff

Reputation: 8348

As others have said in the comments, the best approach is using a UNIX timestamp. To get this in JavaScript, use the following code:

var date = Math.round(new Date().getTime() / 1000);

getTime returns a value in milliseconds, but we want that value in seconds, so we divide it by 1000.

You can then either use AJAX to send that value to the server or put the value in a hidden form field and have it sent to the server when the user submits the form.

In PHP, you can get the date like this:

$date = new DateTime();
// 1341773609 is the UNIX timestamp, which I got from running the above
// JavaScript and alerting the date
$date->setTimestamp(1341773609);
echo $date->format('Y-m-d H:i:s');

Upvotes: 1

Related Questions