2

As working on a large application I am trying to make the datetime stamps reconcile with the current user time. so If activity id done at 3:00PM then the every user see it at 3:00PM

So Here is solution steps to the problem. on this and please correct me or lead me to the right direction if I am not on the right direction.

  1. Store all datetime in MySql as UTC time.

  2. Store time zones in a table with the current offset. for example

    zone_id  zone_name  utc-offset
    1        Central    -6
    
  3. In my users table I have a field for user_time_zone_id and in that field I will have the value "1" in the user setting so it will say that this user is using the system from "Central" location.

  4. In my php application configuration I set the default time zone to UTC like this

    date_default_timezone_set('UTC');
    
  5. Once I load this application I define the user offset and on each datetime out put I do the calculation of the time. for example date('Y-m-d', strtotime($current_offset.' hour')) where $current_offset = -6 as it is define by the user profile upon the page load.

Here are my questions.

Is my approach to this problem correct? Is there a better way of doing this? How to calculate the daylight saving time? Please keep in mind that there are some parts of the country that does not have daylight saving.

4

2 回答 2

1

有一次我也遇到过类似的事情,最终尝试跟踪用户的时区和夏令时是一件很痛苦的事情,尤其是当你的一半客户在没有夏令时的 AZ 时。我不知道这对你来说是否可行,但我最终所做的只是将所有内容存储在 UTC 中,然后使用 JS 将其转换为带有Date对象的用户本地时间。它是通过 ajax 调用完成的,但如果需要,您也可以这样做echodocument.write

于 2013-03-21T15:39:48.350 回答
0

您不需要使用date_default_timezone_set,使用对时区具有本机支持的​​PHPDateTime类。或者,就像@romo 所说,您可以在客户端上使用 JavaScript 进行操作。

于 2013-03-21T15:59:58.393 回答