MySQL 和 PHP 中的时间戳比较(UTC、+0000、时区)
我正在尝试确定 JSON Twitter feed 中给出的表示日期和时间的字符串是否在 MySQL 中的时间戳列范围内。
以下是示例字符串:
'Sat, 31 Oct 2009 23:48:37 +0000',
根据 API 的 +0000
( created_at
)表明它确实是 UTC
。现在,我使用 strtotime
和 date
只是为了确认时间。包含:
$t = 'Sat, 31 Oct 2009 23:48:37 +0000';
$timestamp = strtotime($t);
echo date('M d Y H:m:s', $timestamp);
我得到Oct 31 2009 19:10:37
。如果删除 +0000
,我会得到 Oct 31 2009 23:10:37
。因此,有 +0000
和没有的差别是 4 小时。我猜测是因为我当地的时区(美国马里兰州 = America/New_York
)并且与 UTC 明显不同。
我不太确定在尝试确定此时间戳是否在数据库中存储的两个时间戳(即 2009)的范围内时是否应该删除
和 +0000
或使用它-10-30 23:16:382009-11-25 12:00:00
。我现在感到愚蠢和有点困惑,当我填充这些时间戳时,YYYY-MM-DD H:M:S 来自 Javascript 日期时间选择器,示例格式为 10/31/2009 11:40 am< /code> 并且我像这样使用 STR_TO_DATE:
STR_TO_DATE("10/31/2009 11:40 am", "%m/%d/%Y %l:%i %p")'),
我应该保留 +0000
还是删除它? 精神崩溃
I'm trying to determine whether a string that represents the date and time, given in a JSON Twitter feed is within a range of timestamp columns in MySQL.
Here's the example string:
'Sat, 31 Oct 2009 23:48:37 +0000',
The +0000
according to the API ( created_at
) indicates it is indeed UTC
. Now, I'm using strtotime
and date
just to confirm the time. With:
$t = 'Sat, 31 Oct 2009 23:48:37 +0000';
$timestamp = strtotime($t);
echo date('M d Y H:m:s', $timestamp);
I get Oct 31 2009 19:10:37
. If I remove the +0000
I get Oct 31 2009 23:10:37
. So the difference between having +0000
and not having it is 4 hours. I'm guessing because of my local timezone ( Maryland, USA = America/New_York
) and that differing from the UTC obviously.
I'm not quite sure if I should be stripping the +0000
or using it when trying to determine if this timestamp is within the range of the two timestamps stored in my database, which are 2009-10-30 23:16:38
and 2009-11-25 12:00:00
. I feel silly and a bit confused now, when I populated these timestamps the YYYY-MM-DD H:M:S came from a Javascript date time picker, an example format is 10/31/2009 11:40 am
and I use STR_TO_DATE like so:
STR_TO_DATE("10/31/2009 11:40 am", "%m/%d/%Y %l:%i %p")'),
Should I leave the +0000
or strip it? Mentally taps out
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,您应该保留时区信息,前提是您还正确设置服务器时区。否则还有什么意义,你所有的时间比较都将减少 4 个小时。 :o)
要比较时间,您应该将其保留为 UNIX 时间戳,即
strtotime
的结果。澄清一下:在比较不同时区的时间之前,请确保它们全部转换为同一时区。在没有时区信息的情况下比较伦敦时间 20:00 与纽约时间 20:00 将产生不正确的结果。
strtotime
会将所有时间转换为您当地的时区;如果输入中存在时区信息,它将尊重它并适当地转换时间,否则它会假设时间已经本地化。如果数据库中的所有时间都是本地时间,则绝对应该确保本地化要与它们进行比较的所有时间戳。另一种策略是在存储或比较之前始终将所有时间转换为 UTC。
随意选择,只要坚持这样做即可。
You should of course leave the timezone information in, provided you're also properly setting the server timezone. Otherwise what's the point, all your time comparisons will be 4 hours off. :o)
To compare the time you should leave it as UNIX timestamp, i.e. the result of
strtotime
.To clarify: Before comparing times from different timezones, make sure they're all converted to the same timezone. Comparing London time 20:00 to New York time 20:00 without timezone information will yield incorrect results.
strtotime
will convert all times to your local timezone; if timezone information is present in the input it will honor it and convert the time appropriately, otherwise it'll assume the time is already localized. If all the times in your database are local, you should absolutely make sure to localize all timestamps you want to compare against them.An alternative strategy would be to always convert all times to UTC before storing or comparing them.
Take your pick, just do so consistently.
在 PHP 中,您可以简单地使用 substring 函数将 json twitter 时间分解为其组件,
从那里我认为您已经有了它。不要忘记根据 GMT 差异进行调整。
In PHP you can simply use the substring function to break down the json twitter time into its components as so
From there I think you already have it. Dont' forget to adjust for GMT differences.