繁体   English   中英

PHP如何减去或增加日期?

[英]php how to subtract or add dates?

因此,我想今天获取我们的当前日期(mdY),然后添加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";
}

我假设$today仅用于测试该代码是否可以工作。 您的代码试图减去一个您不能做的字符串。 date('md-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";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM