Take the 2-minute tour ×

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?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted


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




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.

share|improve this answer
Thanks. So just to confirm... time() in PHP is timezone-independent? –  Nathan Osman Aug 14 '10 at 18:50
Yes. time() always returns UTC timestamps. –  Amber Aug 14 '10 at 19:01
add comment

You can use time()

For the Javascript question UTC

share|improve this answer
Why not just $utc = (int) gmdate('U');? Er, or just time()... –  eyelidlessness Aug 14 '10 at 18:21
actually gmdate returns the Greenwich Mean Time, I was wrong, I'll edit it. –  dierre Aug 14 '10 at 18:33
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 Aug 14 '10 at 18:48
I'll keep that in mind. –  dierre Aug 14 '10 at 20:28
add comment

Your Answer


