storing time in database for users in different time zones

This question already has an answer here:

  • Daylight saving time and time zone best practices [closed] 30 answers

  • You've landed in one of the more complicated areas of programming.

    If the time you store into you database is derived from 'now()' in MySQL, then you will store the event at a correct moment in time, and you use the UTC scale to measure it. Which, for international apps is probably the right thing to do.

    If one person just says '4:00' this number will not change, unless you provide a form on your page to enter the time, and which can then be used for calculations. In that case, you can ask each user for its time zone (eg. once, on registration), and calculate UTC to save '4:00' in, say '11:00 UTC'. If the other user logs in, you can calculate his local time from the UTC stored.

    By the way, if you use Linux, you can run your system on UTC instead of localtime (it will still show local time, but the internal clock runs on UTC). DO NOT do this if your system is Windows or dual-boot. Windows does not like systems running on different timezones.

    From the MySQL manual:

    CONVERT_TZ() converts a datetime value dt from time zone given by from_tz to the time zone given by to_tz and returns the resulting value

    Ie something like:

    SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
            -> '2004-01-01 13:00:00'
    

    In case you stored times/dates as GMT, and your friend registered and selected 'MET'

    链接地址: http://www.djcxy.com/p/29338.html

    上一篇: 在应用程序中处理多个时区

    下一篇: 为不同时区的用户在数据库中存储时间