简体   繁体   English

MySQL和PHP中的时间戳比较(UT​​C,+ 0000,时区)

[英]Timestamp comparison in MySQL and PHP ( UTC, +0000, Timezones )

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. 我正在尝试确定在JSON Twitter提要中给出的表示日期和时间的字符串是否在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 . 根据APIcreated_at )的+0000表示它确实是UTC Now, I'm using strtotime and date just to confirm the time. 现在,我正在使用strtotimedate来确认时间。 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 . 我得到Oct 31 2009 19:10:37 If I remove the +0000 I get Oct 31 2009 23:10:37 . 如果我删除+0000Oct 31 2009 23:10:37 So the difference between having +0000 and not having it is 4 hours. 所以+0000和没有它之间的区别是4小时。 I'm guessing because of my local timezone ( Maryland, USA = America/New_York ) and that differing from the UTC obviously. 我猜是因为我当地的时区(马里兰州,美国= America/New_York ),显然不同于UTC。

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 . 我不确定在尝试确定此时间戳是否在我的数据库中存储的两个时间戳的范围内时,是否应该剥离+0000或使用它,这是2009-10-30 23:16:38 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: 我现在感到愚蠢和有点困惑,当我填写这些时间戳时,YYYY-MM-DD H:M:S来自Javascript日期时间选择器,示例格式是10/31/2009 11:40 am我使用STR_TO_DATE像这样:

STR_TO_DATE("10/31/2009 11:40 am", "%m/%d/%Y %l:%i %p")'),

Should I leave the +0000 or strip it? 我应该离开+0000还是+0000它? Mentally taps out 精神上轻拍

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. 否则,重要的是,你所有的时间比较将是4小时。 :o) :O)

To compare the time you should leave it as UNIX timestamp, ie the result of strtotime . 要比较你应该把它作为UNIX时间戳的时间,即strtotime的结果。

$twitterTS    = strtotime('Sat, 31 Oct 2009 23:48:37 +0000');
$localStartTS = strtotime('Sat, 31 Oct 2009 19:00:00'); // timezone is -0400 implicitly
$localEndTS   = strtotime('Sat, 31 Oct 2009 20:00:00');

if ($localStartTS <= $twitterTS && $twitterTS <= $localEndTS) {
    // twitter timestamp is within range
}

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. 比较伦敦时间20:00到纽约时间20:00没有时区信息将产生不正确的结果。 strtotime will convert all times to your local timezone; strtotime会将所有时间转换为您当地的时区; 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. 另一种策略是在存储或比较它们之前始终将所有时间转换为UTC。

Take your pick, just do so consistently. 接受你的选择,一直这样做。

In PHP you can simply use the substring function to break down the json twitter time into its components as so 在PHP中,您可以简单地使用substring函数将json twitter时间细分为其组件

//'Sat, 31 Oct 2009 23:48:37 +0000'
$hour = substring($jsontime,18,2);
$minute = substring($jsontime,22,2);
...

$phpDatetime mktime($hour,$minute,$second,$month,$day,$year);

From there I think you already have it. 从那里我认为你已经拥有它。 Dont' forget to adjust for GMT differences. 不要忘记调整GMT差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM