PHP strtotime 和 MySQL UNIX_TIMESTAMP 返回不同的整数

发布于 2024-09-19 08:42:39 字数 194 浏览 3 评论 0原文

echo strtotime('2010-09-11 12:15:01');

返回:1284207301

SELECT UNIX_TIMESTAMP('2010-09-11 12:15:01')

返回:1284200101

为什么有差异?

echo strtotime('2010-09-11 12:15:01');

Returns: 1284207301

SELECT UNIX_TIMESTAMP('2010-09-11 12:15:01')

Returns: 1284200101

Why the difference?

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

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

发布评论

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

评论(4

任性一次 2024-09-26 08:42:39

答案就在 php.net 关于 strtotime 的文章中:

此函数将使用 TZ 环境变量(如果可用)来计算时间戳。从 PHP 5.1.0 开始,有更简单的方法来定义所有日期/时间函数中使用的时区。 date_default_timezone_get() 函数页面中解释了该过程。

简短版本:strtotime 默认使用您的时区,UNIX_TIMESTAMP 则不使用。

The answer is in the php.net article about strtotime:

This function will use the TZ environment variable (if available) to calculate the timestamp. Since PHP 5.1.0 there are easier ways to define the timezone that is used across all date/time functions. That process is explained in the date_default_timezone_get() function page.

Short version: strtotime uses your timezone by default, UNIX_TIMESTAMP does not.

一世旳自豪 2024-09-26 08:42:39

这通常是由于将 PHP 和 MySQL 设置为使用不同的时区,以及如果它们恰好位于不同的服务器上,则系统时间存在差异。

UNIX 时间戳是自 UNIX 纪元(UTC 时间 1970 年 1 月 1 日午夜)以来经过的秒数,因此时区差异将给出不同的时间戳。

This is commonly caused by setting PHP and MySQL to use different timezones, as well as discrepancies in the system time if they happen to be located in different servers.

UNIX timestamps are the number of seconds that have passed since the UNIX epoch on midnight January 1, 1970 UTC so timezone differences will give different timestamps.

为你鎻心 2024-09-26 08:42:39

1284207301-1284200101 = 7200 -> 2小时。如此完美的差异并不是由于错误,而是时区差异。很可能一个是 UTC/GMT,另一个是 UTC +/- 2

1284207301-1284200101 = 7200 -> 2 hours. Such a perfectly rounded difference isn't due to a bug, it's a timezone diference. Most likely one's in UTC/GMT, and the other's UTC +/- 2

陌上芳菲 2024-09-26 08:42:39

我的猜测是 Unix 时间是 GMT,另一个是你当地的时区,两个输出之间正好有 2 小时的差异 (7200/60/60)

My guess is that Unix time is GMT and the other is your local timezone, there is exactly 2 hours difference (7200/60/60) between the 2 outputs

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