Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

php - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...