如何维护各国时间戳

发布于 2025-01-01 06:20:21 字数 500 浏览 0 评论 0原文

我的时间戳有问题。假设一个用户在凌晨 4.30 上传了一张在美国某处的图片,并且另一个用户在日本看到了这张图片。它将显示上午 4.30,因为数据库中保存的日期将是上午 4.30,但对于日本人来说应该是下午 4.30,因为他们比美国早 12 小时。

所以我想使用 PHP 获取当前服务器时间,假设它是 a,并使用 JavaScript 函数获取用户的当前时间,假设它是 b,然后查找ba 之间的差异以及该结果应添加到数据库中存储的日期中。

所以服务器可能显示上午 4.30,js 函数给出下午 4.30,这将产生 12 小时的差异,因此这 12 小时应该添加到数据库中保存的日期中。这是我想做的,但我不确定是否有任何内置插件来完成任务,因为每个站点都会使用这种类型的功能,他们可能会使用某人的开源代码。

另外可能还有其他更好的解决方案吗?我的算法对吗?我认为获取差异需要一点时间,并且将这些差异添加到所有帖子中也不会花费太多时间。

I have a problem with timestamp. Suppose a user uploads a picture somewhere in USA at 4.30 am and if another user sees this picture in Japan. It would show 4.30 am as the date saved in database would be 4.30 am but it should be 4.30 pm to the Japanese people, as they are 12 hours ahead of America.

So I was thinking to get the current server time using PHP, let's say it is a and also get the current time of user using JavaScript functions say it is b and then find the difference between b and a and that result should be added to the date stored in the database.

So server may show 4.30 am and js function gives 4.30 pm it would give 12 hrs difference so this 12 hrs should be added to the date saved in the database.This is what I thought of doing but I'm not sure if there are any inbuilt plugins in doing the task because every site will be using this type of feature may be they might be using someone's open source code.

Also there might be any other better solutions? And is my algorithm right? I was thinking it is little bit time consuming getting the difference and adding those to all the posts would be little time consuming.

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

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

发布评论

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

评论(3

强辩 2025-01-08 06:20:21

将所有时间戳存储为 UTC。然后在用户界面中针对用户的特定时区进行校正。如有必要,您还可以考虑夏令时

Store all time stamps as UTC. Then for the user interface do the correction for the particular time zone for the user. You can also take into account day light savings if necessary

无声情话 2025-01-08 06:20:21

你可以在上传时查一下GMT+0时间,然后在下载时转换为当地时间吗?这解释了时区: http://hutch120.blogspot.com.au /2008/03/php-timezones-explained.html

Can you look up the time at GMT+0 when you upload, then when it is downloaded, convert to the local time. This explains timezones: http://hutch120.blogspot.com.au/2008/03/php-timezones-explained.html

时光与爱终年不遇 2025-01-08 06:20:21

我更喜欢将所有本地时间转换为服务器时间,并将日期保留在数据库中的一个时区。这有助于进行计算。我还保留有关原始条目的时区信息,您可以使用客户端的首选时区进行用户设置,并向他们显示他们想要的任何时区的时间。

I prefer to transform all local times to the server time and keep dates in the db just in one timezone. This helps to do calculations. I also keep the timezone information about the original entry and you can have a user setting with the preferred timezone of the client and show them the time in whatever timezone they want.

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