简体   繁体   English

如何使用 oop php mysql 中的当前日期检查距离预定义日期还有多少年、月和天

[英]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.

相关问题 如何将出生日期转换为年龄(年,月,日)和年龄(年,月,日)到出生日期转换为PHP? - How to convert Birth date to age ( years, months, days ) and age( years, months, days ) to birth date conversion in PHP? PHP-如何根据两个POST日期显示年,月和日? - PHP - how to display years,months and days based on two POST date? 如何从 mysql 日期中提取年、月、日、小时、分钟、秒? - How extract years, months, days, hours, minutes, seconds from a mysql date? Php从日期开始剩下多少天和几小时 - Php get how many days and hours left from a date PHP日期:如何检查当前年份是否已经过去几个月? - PHP date: How can I check if current year has no more the bygone months? 我如何使用PHP从现在开始回响这么多的日期? - How can I echo a date so many days from now using PHP? 如何使用php从mysql中存储的日期计算一年中的剩余天数 - How Can I Calculate remain days of the year from stored Date in mysql using php 以日期的年、月、日分隔 - Separated by years, months, days of the date 如何使用date_diff输出“X年,Y个月和Z天”? - How to output "X years, Y months and Z days" using date_diff? 如何计算从当前系统日期到过去和未来日期的365天,并使用php和mysql相应地获取数据 - How to count 365 days from current system date to past and future date and fetch the data accordingly using php and mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM