Nathan Osman
Nathan Osman

Reputation: 73175

Getting timezone for JavaScript and PHP all mixed up

I'm trying to synchronize the timezone between a PHP script and some JavaScript code.

I want a PHP function that returns a timestamp in UTC. Does gmmktime() do that?

On the JavaScript side, I have:

var real_date = new Date();
real_date    -= real_date.getTimezoneOffset() * 60000;
real_date    /= 1000;

Does this convert the timestamp to UTC?

Upvotes: 1

Views: 814

Answers (2)

dierre
dierre

Reputation: 7210

You can use time()

For the Javascript question UTC

Upvotes: 0

Amber
Amber

Reputation: 526573

PHP

Just time() will do what you want. If you want an arbitrary timestamp, instead of the current time, then gmmktime will do that, yes.

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

http://www.php.net/manual/en/function.time.php

Javascript

You can use the .UTC() method of a Date object to get # of milliseconds in UTC. However, your current solution should also work, if you're starting with a timestamp.

Upvotes: 5

Related Questions