簡體   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

我有一個來自 mysql 數據庫查詢的預定義日期,我想知道使用條件語句從預定義日期開始還剩下多少年、月和天。 這是我嘗試過的方法,但 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";
}
?> 

您不能像那樣直接比較 2 DateTime object。 由於您已經有了間隔 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:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM