views:

102

answers:

2

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?

+2  A: 

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.

Amber
Thanks. So just to confirm... time() in PHP is timezone-independent?
George Edison
Yes. `time()` always returns UTC timestamps.
Amber
A: 

You can use time()

For the Javascript question UTC

dierre
Why not just `$utc = (int) gmdate('U');`? Er, or just `time()`...
eyelidlessness
actually gmdate returns the Greenwich Mean Time, I was wrong, I'll edit it.
dierre
FWIW, unless you're concerned about accuracy down to fractional seconds (and if you are, you probably aren't using PHP), GMT and UTC should be equivalent.
eyelidlessness
I'll keep that in mind.
dierre