1
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' 。我永远无法让它返回“还没有”为什么?

4

4 回答 4

1

尝试通过 指定您的日期YYYY-MM-DDstrtotime似乎不支持这种MM-DD-YYYY表示法。

于 2013-06-18T15:58:31.593 回答
1

您需要使用的是$date = '17-06-2013'; (DD-MM-YYYY)

$date = '2013-06-17';(YYYY-MM-DD)

并不是

$date = '06-17-2013';

于 2013-06-18T15:58:37.890 回答
1

您应该考虑使用 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";
}
于 2013-06-18T16:00:54.270 回答
1

如果您更改'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.
于 2013-06-18T16:19:20.400 回答