How can i find out if expiry date is less than 7 days from now?
The expiry date format looks like this: 2016-04-13
I have a code here, but it doesn't work:
if($record->$c < date('Y-m-d', strtotime('-7 day'))){
// this is true
}
Hope anyone could help me.
Just convert both units to unix timestamp, make your subtraction, then divide it by 86400:
$expiry_date = '2016-04-18';
$today = time();
$interval = strtotime($expiry_date) - $today;
$days = floor($interval / 86400); // 1 day
if($days < 7) {
echo 'less';
}
Or another way with DateTime
classes:
$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';
}
Example conditions:
$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';
}
Just change / tweak it depending on what you're trying to do.
Recommend exploring date_diff()
function:
http://php.net/manual/en/function.date-diff.php
My challenge was different:
// Count days is date range
function count_days_in_range($date1, $date2) {
$date1 = date_create($date1);
$date2 = date_create($date2);
$interval = date_diff($date1, $date2);
$days = $interval->days;
return $days;
}
But this function will make your life much easier.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.