1

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 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. So just to confirm... time() in PHP is timezone-independent?
Yes. time() always returns UTC timestamps.
0

You can use time()

For the Javascript question UTC

2 Comments

actually gmdate returns the Greenwich Mean Time, I was wrong, I'll edit it.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.