Timezone creation function in a PHP web application

This is almost a similar question for this: -

Working with time zones in PHP

I read this thread and a few more problems, and am not sure how to elegantly build time zone support in a PHP / MySQL application.

I am writing an application in PHP where users can select their time zone.

Here all timestamps are stored in GMT in MySQL. And I will convert the time zone back to the users time zone when presenting the data.

Now these are the problems I am facing: -

1) I am showing the user, a list of time zones filled with MySQL time zone tables is a HUGE list and it is difficult to select.

So, I want to implement something like a list of Windows time zones. Offset with region information.

2) PHP and MySQL may have a different set of lists.

3) Where should I convert time intervals to PHP or MySQL? I have a coding combination. Sometimes it’s easy to select a column converted from MySQL, sometimes it’s easy to convert it to PHP. So here the previous paragraph becomes very important. For example, in the MySQL time_zone tables, Asia / Calcutta and Asia / Calcutta were there, but in PHP 5.2.6, by default, only Asia / Calcutta is present in timezonedb.

+3
timezone php mysql
Apr 27 '09 at 14:41
source share
3 answers

When you ran into this exact problem, I found this link that displays a short list of Windows-style time zone data in a subset of a ridiculously exhaustive list of Unix-style time zones.

Users are provided with a drop-down list of these Windows-style names (for example, (GMT-05: 00) Eastern time (USA and Canada)), and their selection is saved in db in a unix-style format (for example, America / New _York)

The work of applying the user's time zone preference is done in PHP during display using the DateTime class . I think I recommend this, so you can be sure that the dates you manipulate in SQL / PHP are always in UTC until they are displayed.

+5
Apr 28 '09 at 0:55
source share
  • Choosing a time zone from a large list is not so difficult. Sort the list by standard offsets so that users can quickly scroll to the desired offset, and from there look for the exact location.

  • Select one list. I would like to make the list shorter. Submit only these options to the user.

  • Do the math in which the list was selected. If you are presenting a MySQL list, let MySQL do the math by date; if you use a PHP list, PHP has math. This will make it so that you do not need to define some kind of weird mapping for missing zones in one implementation.

A bizarre solution may require querying the user's location and extrapolating their time zone. (However, it is possible to explicitly set it as well)

+4
Apr 27 '09 at 15:09
source share

This is where a custom class might be the best solution. Then you can move time and dates with your time zones. Fortunately, PHP v5 has only such an embedded object. You just need to remember the code so that when you rely on the implicit time zone, this is because you know that you explicitly set it before. It also means that if you don't know what an implicit value is, you ask, effectively making it explicit.

Looking at PHP time zone support and MySQL time zone support, I would do hour work mostly in PHP. MySQL support is largely oriented around the concept of "now."

Oh yes: you do not want to do explicit arithmetic of dates (for example, add 86400 seconds to add "one day"). Let PHP objects or libraries do this. They will fix bugs that you did not think about. (Once I wrote an object for a specialized type of date manipulation and spent weeks struggling with a one-hour intermittent error until I found one place adding 86400 to make a day switch instead of using mktime() . It turned out that the daytime rules the lights were messy, which of course I didn’t check.)

0
Apr 28 '09 at 0:01
source share