简体   繁体   中英

How can I Check How Many Years, Months and Days Left From Predefined Date using Current Date in oop php mysql

I have a predefined date from mysql database query and I want to know how many Years, Months and Days left from the predefined Date using a conditional statement. Here is what I have tried but the output is not accurate so someone should please help me out.

<?php
$date = new DateTime();
$edate = new DateTime("{$floor_end}");
$interval = $edate->diff($date);
if ($edate < $date){
echo  $interval->y . " Year(s), " . $interval->m." Month(s), ".$interval->d." Day(s) Left";
}else{
echo "Floor Work Time Has Finished";
}
?> 

You can't directly compare 2 DateTime object like that. Since you already have the interval object you can get the number of days between 2 DateTime to check

$date = new DateTime();
$edate = new DateTime("2030-01-01");
$interval = $edate->diff($date);
if ($interval->d > 0) {
    echo  $interval->y . " Year(s), " . $interval->m." Month(s), ".$interval->d." Day(s) Left";
} else {
    echo "Floor Work Time Has Finished";
}

Output:

9 Year(s), 4 Month(s), 0 Day(s) Left

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.

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