在PHP中处理不同时区的存储/显示日期的最佳方式? [英] Best way to handle storing/displaying dates in different timezones in PHP?

查看:118
本文介绍了在PHP中处理不同时区的存储/显示日期的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去几个小时,我一直在阅读这个话题,我想我有一个处理方法,但我想要一些确认。

I've been reading up on this topic for the past few hours, and I think I've got a handle on it, but I'd like some confirmation.

情况

我想要一个用户,比如说,加利福尼亚州能够发布一个存储在MySQL中的评论。然后,我想要一个用户,比如说德克萨斯州,可以在调整到他或她的时区的发布日期后查看评论。

I want a user in, say, California to be able to post a comment that will be stored in MySQL. I then want a user in, say, Texas to be able to view the comment with the post date adjusted to his or her time zone.

建议的解决方案

存储


  1. 运行在应用程序开始之后,所有日期函数都使用UTC时区: date_default_timezone_set('UTC');

  2. $ Date = new DateTime(); 以获取当前日期和时间为UTC的DateTime对象。

  3. 使用 $ Date-> format()将值插入到MySQL的datetime类型列中。

  1. Run the following at the start of the application so that all date functions use UTC timezone: date_default_timezone_set('UTC');
  2. $Date = new DateTime(); to get a DateTime object with the current date and time in UTC.
  3. Use $Date->format() to get the value to insert into the datetime type column in MySQL.

显示


  1. 从JavaScript获取用户的时区信息,并将其存储在cookie中。

  2. 运行MySQL SELECT查询以检索datetime列值。

  3. $ Date = new DateTime($ row ['time']) ; 使用存储的UTC时间实例化DateTime对象。

  4. $ Dat e-> setTimezone(new DateTimeZone($ userTimezone)); 将UTC时间调整到用户的时区。

  5. 使用 $ Date-> format();

  1. Get the user's timezone information from JavaScript and store it in a cookie.
  2. Run a MySQL SELECT query to retrieve the datetime column value.
  3. $Date = new DateTime($row['time']); to instantiate a DateTime object with the stored UTC time.
  4. $Date->setTimezone(new DateTimeZone($userTimezone)); to adjust the UTC time to the user's timezone.
  5. Display using $Date->format();

这是要做什么的要点吗?我错过了更好的解决方案吗?感谢您的帮助!

Is that the gist of what has to be done? Am I missing a better solution? Thanks for your help!

推荐答案

它可以变得更简单。既然您使用JavaScript,那么为什么不使用JavaScript调整客户端的时区呢?

It can be made simpler still. Since you are using JavaScript then why not use JavaScript to adjust the time zone on the client as well?


  1. 将所有时间存储在服务器上UTC

  2. 将其作为UTC服务于客户端

  3. 客户端使用JavaScript调整时间到本地时区

这不仅使事情更简单,而且克服了模型的问题。如果我在纽约注册了我的帐户,但是去澳大利亚,我想按照澳大利亚的时区看到时间。事实上,使用JavaScript可以轻松调整设置,使设计更加动态。第二,您可以避免存储用户时区的开销。

Not only does this make things simpler but it also overcomes a problem with your model. If I registered my account in New York but travelling to Australia, I want to see the times as per the Australian time zone. In fact, using JavaScript you use can easily adjust the settings, making the design even more dynamic. Second, you can avoid the overhead of storing the user's time zone.

那就是说,如果你想让你的设计降级到非JavaScript浏览器,那么你最好采取一个完整的服务器端方法依靠HTTP cookie(而不是依靠JS来获取cookie)。

That said, if you want your design to degrade to non-JavaScript browsers then you are better off taking a full server side approach relying on HTTP cookies (as opposed to relying on JS to fetch cookies).

这篇关于在PHP中处理不同时区的存储/显示日期的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆