看来我对strtotime函数不太了解。我的例子是,我想比较当前时间(现在)和特定时区的特定时间。
例如,“美国/纽约”时区的具体时间是“本周一14:00”:
$specificTime = strtotime("monday this week 14:00:00 America/New_York");我目前的代码是:
$now = strtotime("now America/New_York");
if ($now > $specificTime) {
//do something
}但是我发现上面的$now比现在的时间提前了6个小时。我猜数字6来自美国/纽约的-05:00,加上一个小时的夏令。
它应该从$now中删除时区,它将正确工作:
$now = strtotime("now");
if ($now > $specificTime) {
//do something
}有人能解释一下为什么strtotime("now America/New_York")比strtotime("now)领先6小时,为什么它们不相等?真的很困惑。
我在GMT+07:00上。
发布于 2013-11-28 09:48:00
简单调试:
<?php
$now = strtotime("now America/New_York");
echo date('r', $now);
// Thu, 28 Nov 2013 16:39:51 +0100..。显示该命令正在执行以下操作:
使用字符串执行日期操作非常复杂。想象一下,你会尝试用字符串函数做数学:strtofloat('one plus square root of half hundred')--会有足够的出错空间。因此,我的建议是保持简单,并且只在有一些好处时(如strtotime('+1 day') )与简单表达式一起使用。
如果您需要处理不同的时区,我建议您使用适当的DateTime对象。如果您选择使用Unix时间戳,请忘记时区: Unix时间戳根本没有时区信息。
发布于 2013-11-28 09:37:44
为此您可以使用DateTime。我相信在strtotime中设置时区是无效的。
$specificTime = new DateTime("monday this week 14:00:00", new DateTimeZone("America/New_York")));
$now = new DateTime("now", new DateTimeZone("America/New_York"));然后,您可以将unix时间戳与以下内容进行比较:
if ($now->getTimestamp() > $specificTime->getTimestamp()) {
// do something ...
}发布于 2013-11-28 09:43:06
每个时区之间都有时间偏移。
strtotime()函数将根据时区返回Unix时间戳。
除非在该参数中指定时区,否则它将使用默认时区。
默认时区,它是date_default_timezone_get()的返回值;
请看下面的代码:
<?php
// UTC
echo date_default_timezone_get(), "\n";
// 2013-11-28 14:41:37
echo date('Y-m-d H:i:s', strtotime("now America/New_York")), "\n";
// 2013-11-28 09:41:37
echo date('Y-m-d H:i:s', strtotime("now")), "\n";https://stackoverflow.com/questions/20262175
复制相似问题