[英]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.我有一个来自 mysql 数据库查询的预定义日期,我想知道使用条件语句从预定义日期开始还剩下多少年、月和天。 Here is what I have tried but the output is not accurate so someone should please help me out.
这是我尝试过的方法,但 output 不准确,所以请有人帮助我。
<?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.您不能像那样直接比较 2
DateTime
object。 Since you already have the interval object you can get the number of days between 2 DateTime to check由于您已经有了间隔 object 您可以获得 2 DateTime 之间的天数来检查
$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: Output:
9 Year(s), 4 Month(s), 0 Day(s) Left
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.