MySQL 和国际日期

发布于 2024-07-24 07:12:08 字数 409 浏览 4 评论 0原文

假设我在多个位置有多个服务器,并且我想对字段日期使用 MySQL 的日期时间类型,并且我总是希望字段日期具有 UTC 时间戳,因此当我将其添加到数据库中。 现在假设我想让 MySQL 为其输出 UNIX 时间戳。

当我在 Server AI 上执行此操作时,会得到字符串“2009-06-17 12:00:00”,在其上执行 UNIX_TIMESTAMP(STRING) 会返回数字 1245240000。 UTC 时间为 2009-06-17 12:00:00。 现在我在服务器B上做同样的事情。 我得到了相同的字符串,因为它是 UTC 字符串,但是当再次执行 UNIX_TIMESTAMP(STRING) 时,我得到了错误的数字 1245232800,这是 UTC +2 时间。 我该如何解决这个问题? 我应该在 PHP 端进行从字符串到时间戳的转换吗?

Say I have multiple servers in multiple locations and I want to use MySQL's datetime type for the field date and I always want to have the field date have the UTC timestamp so I would execute a UTC_TIMESTAMP() when I add it to the database. Now say I want to have MySQL output the UNIX TIMESTAMP for it.

When I do this on Server A I get the string "2009-06-17 12:00:00" doing the UNIX_TIMESTAMP(STRING) on it gives me the number 1245240000 back. Which is 2009-06-17 12:00:00 in UTC time. Now I do the same thing on Server B.
I get the same string back since its the UTC string but when executing UNIX_TIMESTAMP(STRING) again I get back the wrong number back 1245232800 which is the UTC +2 time. How do I get around this? Should I do the convertion from string to timestamp on the PHP side?

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

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

发布评论

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

评论(2

合约呢 2024-07-31 07:12:08

你好,

我想问一个显而易见的问题,你检查了两台机器上的日期和时间吗?

编辑: ...两台机器上的 MySQL 时区是否相同?

更新:好的。 问题在于,传递到 UNIX_TIMESTAMP 的时间戳字符串被解释为当前时区中的值,然后将其转换回 UTC,因此,因为您位于 MEZ,所以减去两个小时才能将其返回到 UTC因此,当将时间戳转换回 Unix 时间字符串时,会从时间戳中减去 7200。

因此,使用 UNIX_TIMESTAMP() 转换时看到的变化将返回到 Unix Epoch 时间字符串。

顺便说一句,您不应该使用 TIMESTAMP 类型来存储 UTC_TIMESTAMP 而不是 DATETIME 类型吗?

更新:将演示时间与存储时间分离绝对是正确的方法。 然后,您可以在世界各地重复使用相同的数据,并且只需在向用户呈现数据时进行本地时间和本地时间之间的转换。

如果你不这样做,那么你将不得不在创建时间戳时存储时区,然后进行各种复杂的排列,必须计算出

  • 本地时区在存储时是否处于夏令时,
  • 存储数据时的时区与显示数据时的时区之间有什么区别。

将其全部存储为 UTC 就可以消除这种情况。

如果大多数用户必须根据返回的 UTC 时间自行计算出本地时间,那么他们不会那么高兴,因此系统通常会转换为用户当前的本地时间。

当然,如果用户想要以当地时间表示的数据(通常是这种情况),则可以这样做。 我能想到的唯一广泛使用的、以 UTC 格式存储和显示数据的系统是空中交通管制和飞行计划管理系统,它们始终保存在 UTC 格式(或者更准确地说是 ZULU 时间) 。

HTH

欢呼,

G'day,

I'll ask the obvious here, did you check the date and time on both machines?

Edit: ... and the MySQL timezone was the same on both machines?

Update: Ok. The problem is in the fact that the timestamp string being passed into UNIX_TIMESTAMP is interpreted to be a value in the current timezone which is then converted back to UTC so, because you're in MEZ, two hours is subtracted to return it back to UTC so 7200 is subtracted from your timestamp when it is converted back to a Unix timestring.

Hence, the variation you see when using UNIX_TIMESTAMP() to convert is back to a Unix Epoch timestring.

BTW Shouldn't you be using a TIMESTAMP type for storing off your UTC_TIMESTAMPs instead of DATETIME type?

Update: Decoupling presentation time from stored time is definitely the way to go. You can then reuse the same data all around the world and only have to convert to and from local time when you are presenting the data to a user.

If you don't do this then you are going to have to store off the timezone when the timestamp was made and then go into all sorts of complicated permutations of having to work out if

  • the local timezone was in daylight saving time when it was stored,
  • what the difference is between the timezone at the time that the data was stored and the timezone where the data is to be presented.

Leaving it all storeed as UTC gets rid of that.

Most users won't be that happy if they have to work out the local time themselves based on the UTC time returned so systems usually convert to current local time for the user.

This is of course if the user wants the data expressed in local time which is usually the case. The only widely used system I can think of, off the top of my head, that stores and presents its data in UTC is system for air traffic control and flight plan management which are always kept in UTC (or ZULU time to be more precise).

HTH

cheers,

谜兔 2024-07-31 07:12:08

你尝试过这样做吗?

一起执行此指令。

SET time_zone = 'UTC';
SELECT FROM_UNIXTIME(0), UNIX_TIMESTAMP('2009-06-17 12:00:00'); 
// 1970-01-01 00:00:00        1245240000

它们仅影响客户端会话,而不影响服务器配置。

Have you tried doing this?

Execute this instructions together.

SET time_zone = 'UTC';
SELECT FROM_UNIXTIME(0), UNIX_TIMESTAMP('2009-06-17 12:00:00'); 
// 1970-01-01 00:00:00        1245240000

They only affect the client session, not the server configuration.

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