date_default_timezone_set('Europe/London');
$date = '06-17-2013';
if (strtotime("now") > strtotime("+5 days", strtotime($date))) {
echo '5 days have gone by';
} else {
echo 'not yet';
}
这个函数总是返回 '5 days have got by' 。我永远无法让它返回“还没有”为什么?
尝试通过 指定您的日期YYYY-MM-DD
。strtotime
似乎不支持这种MM-DD-YYYY
表示法。
您需要使用的是$date = '17-06-2013';
(DD-MM-YYYY)
或$date = '2013-06-17';
(YYYY-MM-DD)
并不是
$date = '06-17-2013';
您应该考虑使用 SPL DateTime 对象。
尝试这个:
$date = new DateTime('2013-06-17');
$date->add(new DateInterval("P5D");
$now = new DateTime();
if($now > $date) {
echo "5 Days have passed";
}
else{
echo "not yet";
}
如果您更改'06-17-2013'
为此'06/17/2013'
,它将按预期工作
<?php
date_default_timezone_set('Europe/London');
$date = '06/17/2013';
if (strtotime("now") > strtotime("+5 days", strtotime($date))) {
echo '5 days have gone by';
} else {
echo 'not yet';
}
?>
在 strtotime 函数中使用正斜杠 / 和连字符 - 是有区别的。
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator
between the various components: if the separator is a slash (/), then the American
m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the
European d-m-y format is assumed.