Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL和PHP中的时间戳比较(UTC,0000,时区)_Php_Mysql_Timezone_Timestamp_Utc - Fatal编程技术网

MySQL和PHP中的时间戳比较(UTC,0000,时区)

MySQL和PHP中的时间戳比较(UTC,0000,时区),php,mysql,timezone,timestamp,utc,Php,Mysql,Timezone,Timestamp,Utc,我试图确定JSON推特提要中给出的表示日期和时间的字符串是否在MySQL中的时间戳列范围内 以下是示例字符串: 'Sat, 31 Oct 2009 23:48:37 +0000', 根据(在创建)的+0000表示它确实是UTC。现在,我使用strotime和date来确认时间。与: $t = 'Sat, 31 Oct 2009 23:48:37 +0000'; $timestamp = strtotime($t); echo date('M d Y H:m:s', $timestamp);

我试图确定JSON推特提要中给出的表示日期和时间的字符串是否在MySQL中的时间戳列范围内

以下是示例字符串:

'Sat, 31 Oct 2009 23:48:37 +0000',
根据(
创建)的
+0000
表示它确实是
UTC
。现在,我使用
strotime
date
来确认时间。与:

$t = 'Sat, 31 Oct 2009 23:48:37 +0000';
$timestamp = strtotime($t);

echo date('M d Y H:m:s', $timestamp);
我得到
2009年10月31日19:10:37
。如果我删除
+0000
我会得到
2009年10月31日23:10:37
。因此,拥有
+0000
和不拥有它的区别是4小时。我猜是因为我的当地时区(美国马里兰州=
美国/纽约
)和UTC明显不同

我不太确定我是否应该剥离
+0000
或者在尝试确定此时间戳是否在数据库中存储的两个时间戳的范围内时使用它,这两个时间戳是
2009-10-30 23:16:38
2009-11-25 12:00:00
。我现在觉得很傻,有点困惑,当我填充这些时间戳时,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")'),

我应该留下
+0000
还是剥掉它?在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);

从那里我想你已经有了。别忘了调整GMT差异。

当然,如果您也正确地进行了调整,您应该保留时区信息。否则的话,你所有的时间比较都会有4个小时的休息时间o)

要比较时间,应将其保留为UNIX时间戳,即
strotime
的结果

$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
}
$twitterTS=strottime('Sat,2009年10月31日23:48:37+0000');
$localStartTS=strottime('Sat,2009年10月31日19:00:00');//时区隐式为-0400
$localEndTS=strotime('2009年10月31日星期六20:00:00');
如果($localStartTS