简体   繁体   English

PHP 中的 date_diff 问题

[英]Issue with date_diff in PHP

I want to check expired date is before one month or not for that I have use below code我想检查过期日期是否在一个月之前,因为我使用了以下代码

$expire_date = '2021-01-14 04:59:59';
date_default_timezone_set("Asia/Kolkata");

$date1 = new DateTime(date('Y-m-d H:i:s'));
$date2 = new DateTime($expire_date);
$interval = $date1->diff($date2);

echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ".PHP_EOL; die(); // difference 0 years, 10 months, 8 days

if($interval->m > 1 && $interval->y <= 0){
    $status = "Yes"; // for expired
}else{
    $status = "No"; // not expired
}

with this case it is showing me wrong data so how can I find expire date before one month from the current date in PHP在这种情况下,它向我显示了错误的数据,所以如何在 PHP 中找到距当前日期前一个月的到期日期

One way to do this is to generate a date which is one month from now, and compare the expiry date with that.一种方法是生成一个从现在起一个月后的日期,并将到期日期与该日期进行比较。 If it is less, then the expiry is within one month:如果小于,则在一个月内到期:

$now = new DateTime();
$now->modify('+1 month');
$date2 = new DateTime($expire_date);

if ($date2 < $now) {
    echo "expiry is in less than 1 month\n";
}
else {
    echo "expiry is more than 1 month away\n";
}

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

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