1

I want to output the users system timezone, e.g. "EST". How can I do that, preferably using Javascript, though I could do it on the PHP side as well, if that's significantly easier.

2 Answers 2

4

Javascript has a Date.getTimezoneOffset function which will give you the difference in minutes between the GMT and the local timezone. To get from that to the actual offset, you could say:

var offset = Date.getTimezoneOffset / 60 * -1

The * -1 is needed because the offset will be opposite what you expect (for instance, EST, which is GMT -5, would result in 300, because GMT is 300 minutes ahead).

From there, you will need to convert the offset to the description of the offset. There is no built in way in Javascript to do so. You could consider creating a lookup array, but then there would be the issue that there are multiple names for the same offset (for instance, GMT - 5 could be EST (Eastern Standard Time) or CDT (Central Daylight Time).

See Wikipedia for a list of timezones.

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

1 Comment

The problem with this approach is the offset by itself isn't enough. You need to know if there's a "Daylight savings time" in effect at that location.
2

You will have to use Javascript - since you do not know the client timezone on the server side. You could use the code below and extract the relevant timezone string from localtime.

var now = new Date();
localtime = now.toTimeString();

which will return something like "12:21:44 GMT-0400 (EDT)"

2 Comments

Hmm... that's close. I'm getting "Eastern Daylight Time", rather than the abbreviation. (Actually, I can use php, because this is for an app that will run locally on the user's machine.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.