根据不同国家/地区的用户计算机时间在网站上显示日期/时间

发布于 2024-10-31 19:20:10 字数 389 浏览 2 评论 0原文

我正在开发一个网站应用程序的聊天信使。对于我的概念,我避免使用时区。

当用户发布消息时,我使用 php 获取当前服务器日期/时间并将其保存到数据库中。

要向用户显示所有消息的时间,我只需要找出服务器时间和用户系统时间之间的差异。假设相差3小时,我只需要使用mysql从数据库中检索记录的服务器时间,然后将其添加3,例如:$message-time-display = $server-time-of-message-posted + $ different。

对于这个解决方案,虽然用户的计算机时间可能不正确,但不会影响我数据库中保存的时间记录。当用户更正计算机时间后,它会显示正确的时间。这个解决方案可靠吗?如果是这样,为什么没有人这样做?为什么这么多人使用 GMT 或时区之类的东西?也许我错过了一些重要的问题?

I am developing a chatting messenger of website application. For my concept, I avoid to use timezone.

When a user post a message, I use php to get my current server date/time and save it onto database.

To display the time of all messages to the user, I just need find out the different between server time and user's system time. Let's say the different is 3 hours, i just need use mysql to retrieve recorded server time from database then add it with 3, eg : $message-time-display = $server-time-of-message-posted + $different.

For this solution, although the user's computer time may be incorrect, it will not effect the time records that saved on my database. And it will display the correct time when the users have corrected their computer's time. Is this solution is reliable? If so, why there is none of people doing this? Why so many people are using GMT or timezone thingy? Maybe I miss some important issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2024-11-07 19:20:10

一种快速解决方法是不显示确切的时间,而是显示经过的时间。例如:2 分钟前

然后...

由于您知道经过的时间,因此您可以从用户的当前时间中减去该时间。
您可以使用 javascript 获取用户当前时间。

var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();

One quick fix is not to show the exact time, rather the time elapsed. Ex: 2 minutes ago

And then...

Since you know the elapsed time you can substract that from the user's current time.
You can get the user current time with javascript.

var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
故事未完 2024-11-07 19:20:10

如果您正在编写一个仅在一台服务器上的应用程序,那么可以将时间存储为本地服务器时间。如果您的应用程序将部署在全球许多位置,那么将所有时间存储为 UTC< 更有意义/a>.

在我看来,用户提供的时间在存储之前应从用户的时区转换为 UTC(如果是您的个人应用程序,则应转换为服务器的时区),并且在显示时应转换回用户的时区。

所以在你的情况下,它应该是这样的:

  1. 用户操作 - >将 UTC 时间存储在数据库中
  2. 另一个用户操作 ->将当前 UTC 时间与存储的 UTC 时间进行比较并显示差异。

如果您要显示时间戳,则应如下所示:

  1. 用户操作 ->将 UTC 时间存储在数据库
  2. 用户访问中 ->检索存储的 UTC 时间并将其转换为用户的时区。展示。

If you are writing an app that will only ever be on one server then it's ok to store time as your local server time. If your application will be deployed in many locations around the globe, then it makes better sense to store all times as UTC.

To my mind, user-provided times should be translated from user's timezone to UTC (or your server's timezone if it's your personal app) before being stored and should be translated back to the user's timezone when displayed.

So in your case, it should go something like this:

  1. user action -> store UTC time in database
  2. another user action -> compare current UTC time to stored UTC time and display difference.

If you're displaying timestamps, it should go like this:

  1. user action -> store UTC time in database
  2. user access -> retrieve stored UTC time and translate it to user's timezone. Display.
别忘他 2024-11-07 19:20:10

只要用户设置正确的时间,您的解决方案就应该有效。这实际上是唯一的问题。您认为为什么没有人这样做?我想他们中的大多数人都会这样做,只有懒惰的人才会依赖 GMT/EST 时间。

提示:我看到在跟踪代码(例如谷歌的分析 JavaScript)中发送的用户时间,可能是为了计算您正在考虑的差异;)

Your solution should work as long as the user has the time set correctly. That is the only problem actually. Why do you think no one is doing it ? I guess most of them do and only the lazy ones relies on GMT/EST times.

Hint: I saw the user time sent in tracking codes such as google's analytics javascript, probably for computing the difference you are taking about ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文