zen*_*zen 1 php if-statement date strtotime
如何确定到期日期是否少于7天?
到期日期格式如下:2016-04-13
我这里有一个代码,但它不起作用:
if($record->$c < date('Y-m-d', strtotime('-7 day'))){
// this is true
}
Run Code Online (Sandbox Code Playgroud)
希望有人能帮助我.
只需将两个单位转换为unix时间戳,进行减法,然后将其除以86400:
$expiry_date = '2016-04-18';
$today = time();
$interval = strtotime($expiry_date) - $today;
$days = floor($interval / 86400); // 1 day
if($days < 7) {
echo 'less';
}
Run Code Online (Sandbox Code Playgroud)
或者使用其他方式DateTime:
$expiry_date = '2016-04-18';
$expiry_date = new DateTime($expiry_date);
$today = new DateTime();
$interval = $today->diff($expiry_date);
$day = $interval->format('%r%a');
if($day < 7) {
echo 'less';
}
Run Code Online (Sandbox Code Playgroud)
示例条件:
$expiry_date = '2016-04-18';
$today = time();
$interval = strtotime($expiry_date) - $today;
$day = floor($interval / 86400); // 1 day
if($day >= 1 && $day < 7) {
echo 'between 1 - 7 days';
} elseif($day <= 0) {
echo 'deadline';
} else {
echo 'soon';
}
Run Code Online (Sandbox Code Playgroud)
只是根据你想要做的事情来改变/调整它.