php如何减去或添加日期?

时间:2016-03-27 20:51:55

标签: php sql date

所以我想今天得到我们当前的日期(m-d-Y)然后再加30天。我会把它放在mySQL数据库中。然后每天它会说明接近14天还剩多少天。

$today = strtotime(date('Y-m-d H:i:s'));
$expireDay = date('m-d-Y', strtotime("+30 days"));
$timeToEnd = $expireDay - $today;
if ($timeToEnd <= strtotime("$expireDay - 14 days")// this is not correct syntax
{
echo "less than 14 days";
echo $timeToEnd('d')//here is where I am having problems with my syntax
} else {
echo "more than 14 days";
}

1 个答案:

答案 0 :(得分:2)

我认为$today仅用于测试此代码是否应该起作用的情况。您的代码试图减去您无法执行的字符串。 date('m-d-Y', strtotime("+30 days"));是一个日期字符串03-27-2016,要减去需要strtotime将给出的整数。

$today = time();
$expireDay = strtotime("+30 days");
$daysleft = (($expireDay - $today) / 86400);
if ($daysleft <= 14 ) {
     echo "less than 14 days";
     echo $daysleft;
} else {
     echo "more than 14 days";
}