Reputation: 98
In PHP you can change the timezone with a simple line of code
date_default_timezone_set('Europe/London');
My problem I encounter is making a timezone changer that is user based. I want to have the user use a drop box with timezone options and using MySQL keep it saved on the user's data variables. My issue is related to how the time zones are handled by PHP, specifically with the names that are given to each variable. It is confusing enough having time zones with names like the following.
That isn't the only problem, there are so many the numbers of different time zone names reach the hundreds. I am not sure of those hundreds which one to use because I obviously cannot use them all as that would be a bit redundant and inefficient. I would use time zones like GMT+5 or GMT-1
but the PHP manual says I shouldn't use them, and from what I read they do not support DST. I am wondering what I should do in order to get a good list of time zones that would be good for my purposes. Or if someone is kind enough they could forward me to a list that I could use.
I am looking for any list that can cover most time zones.
TL;DR I need help in figuring out what time zones to use for a time zone drop box.
Upvotes: 0
Views: 191
Reputation: 241738
You should understand that PHP time zones are IANA standard time zones. They aren't proprietary to PHP, and users may already be used to seeing them (possibly).
If you want to limit the selection, consider using two dropdowns, the first to select a country, and the second to pick a time zone within that country. PHP has functions for this, and the data comes from zone.tab in the original dataset.
An alternative would be to use a map-based time zone picker control, such as this one for javascript.
Upvotes: 1
Reputation: 108796
If you store a timezone string (for example 'Europe/London') for each user, you can convert incoming times to UTC, then reconvert them upon display. That's pretty easy once you figure it out:
$previoustz = date_default_timezone_get();
@date_default_timezone_set( $users_chosen_timezone );
/* do your time processing here */
@date_default_timezone_set( $previoustz );
You do have to be disciplined about always doing this, or your system will display some wrong-time bugs.
As for the question of what time zones to offer, I strongly suggest you offer the whole list provided by PHP. You only have to gather a timezone setting from a user in a user-profile screen, so you're not constantly hassling users with a long dropdown list.
You need them all for for a few reasons. One is the general bizarreness of timezones in the USA, where some local jurisdictions do strange things with daylight savings time. Check out America/Indiana. This from the state in which the legislature once decreed the value of π to be 3. Do you want to figure all this out? I didn't think so.
A second reason is this: some jurisdictions switch back and forth from daylight savings on hard-to-automate dates. For example, the state of Israel switches based on the Hebrew calendar. You want your users simply to choose their timezone.
Another is this: people using global systems like WordPress have come to expect the standard set of choices, which are based on a venerable Unix / Linux system called timezoneinfo. If you just go with the standard, you can let the timezoneinfo people take care of what choices you should offer. This is an international electropolitical issue, and timezoneinfo deals with it for you far better than you could hope to.
Take a look at http://www.php.net/manual/en/datetimezone.listidentifiers.php for a way to obtain the list.
Upvotes: 2