开发者

Dealing with Time Zones

I need some guidance with one of my project requirements, I am developing an application which has to deal with various time zones.

Scenario:开发者_运维百科 User 1 is from India, so his time zone would be GMT+05:30 User 2 is from UK, so his time zone would be GMT+01:00

If the User 1 sends a message to User 2, I want to show the Message Sent/Received Time as per the user’s time zone. For example User 1 sends a message at 6:30 Indian time, when User 2 would view the message it would show as 2:00 UK time.

Here goes my question, whenever I save the message should I convert it to GMT+00, so all my base times stamps are the same and then later when I display the message, I convert it back to User specific time zone. Would this be complex? Is this the best way of doing this?

I like to get views for both saving and displaying, also when I should do the time conversion from optimization point of view. I would need do deal with any/all timezones.

I am developing this application with PHP and MySQL and I am aware of timezone conversion method come with both PHP and MySQL.

I am just trying to figure out the best way of doing this. Look forward to have all valuable suggestions.

Note : As of now I am not much worried with day/light savings.

Thanks Ravi


When storing the messages, convert, use and store them in a DST neutral timezone - UTC or GMT, for example.

With them store the original timezone offset and the DST offset - this will help with business logic.

When displaying the message, use this data and convert to the local time.

See this question and answers for best practices regarding working with different time zones.


Here goes my question, whenever I save the message should I convert it to GMT+00, so all my base times stamps are the same and then later when I display the message, I convert it back to User specific time zone. Would this be complex? Is this the best way of doing this?

It is. The only caveat is to allow each user to specify their desired timezone to view times in (either as a preference or from their client system's settings or from user database).

I like to get views for both saving and displaying, also when I should do the time conversion from optimization point of view. I would need do deal with any/all timezones.

The best time to convert from client to GMT is in your "business" logic - basically, whatever logic you have which processes web form data. NOT on the database side.

The best time to convert from GMT to client for viewing is right before/in the presentation layer, e.g. when you're printing your HTML.

The reason is that this way, as little of the code as possible needs to worry about timezones.

NOTE This timezone related logic becomes VERY VERY complicated if/when you need to do any date-specific logic (e.g. aggregate based on date as opposed to merely print timestamp to the user).


When saving data to your database, use something like:

INSERT INTO TableName SET TheDateFile=UTC_TIMESTAMP()

And then, presuming that you allow each user to specify their own timezone, you will need to convert the date in your business logic (as mentioned by DVK)

Something like:

DATE_FORMAT(DATE_ADD(TheDateField, INTERVAL 2 HOUR)  // or whatever value

I have not done this myself, but I guess it would be simple enough to store the hour interval as per the users timezone.


In any case if you deal with users which are on different timezones, you must separate the timezone from the date. I heard long time ago that wordpress did it the same way:

  • First step, determine timezone of the date
  • Second step, set the timezone of the date to GMT (+0)
  • Third step, store the timezone and the GMT date in two separate columns.

Anyway, if you need more informations, there's a post here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜