Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to store a timezone in a database?

Let me explain: I'm not asking about the proper way to store a specific datetime's timezone in a database. I'm talking about timezones themselves. FOR EXAMPLE:

I have a MySQL table named 'users'. Now, on this table, I wish to have a column that contains the timezone of wherever the user lives (this is provided by the user, it will be chosen from a list). I'm working with PHP, which has a list of timezone strings like these:

List of TimeZones for America

Now, the obvious solution (at least for me) would be to create a VARCHAR column in the 'users' table, then store the timezone string used by PHP in said column.

Now that I think about it, that would assume I'll always use PHP to interact with that database table, which while true now, might not be so in the future. And (correct me if I'm wrong) PHP's timezone strings probably don't work for other programming languages, maybe other languages have their own 'constants' for timezone handling.

How would you approach saving a user's timezone preference in a DB column, then? Any ideas are certainly appreciated.

Note: the useful thing about PHP's timezones is that, even if DST is in effect, they automatically take it into account, which is awesome. So you can see my interest in using them instead of just storing a numeric offset.

like image 229
Emmanuel Figuerola Avatar asked Apr 05 '13 01:04

Emmanuel Figuerola


People also ask

What time zone should I store the data in?

When storing dates in the database, they should always be in UTC. If you are not familiar with what UTC is, it is a primary time standard that all the major timezones are based on. The major timezones are just offsets from UTC.

How does SQL Server store time zones?

timezone Name of the destination time zone. SQL Server relies on time zones that are stored in the Windows Registry. Time zones installed on the computer are stored in the following registry hive: KEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones.

Should I store UTC or local time?

UTC is the universal time, which is equal to Greenwich Mean Time, only it does not follow the changes in daylight saving time. UTC is the best choice for timestamping, as Windows today offers functionality to make UTC available to programs together with local time which is actually calculated as an offset from UTC.

How does DB know timezone?

If you're trying to determine the session timezone you can use this query: SELECT IF(@@session. time_zone = 'SYSTEM', @@system_time_zone, @@session.


1 Answers

And (correct me if I'm wrong) PHP's timezone strings probably don't work for other programming languages

Ok, I will. :)

PHP implements the IANA/Olson/TZDB/ZoneInfo database (lots of names for the same thing). This is the closest thing there is to an industry standard, and it's implemented in just about every programming language and platform.

The only OS that doesn't support it natively is Microsoft Windows, because Microsoft maintains their own time zone database. So for platforms that are Windows-centric, like .Net, there are libraries that you have to use if you want support for that style of timezone.

Read more in the timezone tag wiki, or on Wikipedia.

So yes - you just store the timezone id string as a varchar. That's the best thing to do.

like image 166
Matt Johnson-Pint Avatar answered Oct 13 '22 04:10

Matt Johnson-Pint