2015-11-06 65 views
0

我曾經訪問了一些帖子,檢索了兩個日期之間的日期差異,但它沒有給我我正在尋找的答案。閱讀文檔時,我有問題了解它是如何工作的。有條件的DateDiff

我試過編碼,但它不像我期待的那樣。這裏是我的代碼:

<?php 
$currentDate = new DateTime(); 
$createDateJoin = date_create($getDate['date_joined']); 
$dateJoin = date_format($createDateJoin, "Y-m-d"); 

$yearDifference = $currentDate->diff($createDateJoin); 

    if ($yearDifference->d < 31 && $yearDifference->m = 0 && $yearDifference->y == 0) { 
     echo $yearDifference->d . " days"; 
     } else if ($yearDifference->m > 3) { 
       echo $yearDifference->m . " month"; 
     } else if ($yearDifference->y > 1) { 
       echo $yearDifference->y . " years"; 
     } else { 
       echo "Not yet assigned"; 
     } 
?> 

正如你可以從上面我的代碼看,我試圖做一個打印時,計算兩個日期之間的差額後,它符合從程序的$yearDifference-> .The行爲的條件我所經歷的並不打印出我想要的東西(例如,工作時間超過1年的員工將打印出他們有多少年的工作,對於剛剛進來的人來說有幾個月,新員工不到一個月就會打印出日子) 。

我想知道怎麼做->d/m/y工作,我怎麼能真正使用的dmy正確地繪製出具體的日期。而且我也注意到,當我將$yearDifference作爲Stringint時,它出現了不同的條件結果。那麼我應該怎麼處理這個類型來更容易地操縱它呢?非常感謝幫助。

回答

0

您可以使用此代碼來獲得日期不同的方法差異()對象返回多個值來檢查,你可以print_r的你的對象,將打印通過差異返回的所有數據成員()方法

<?php 
echo get_date_diff(strtotime('1990-10-12'),strtotime('2015-10-14')); 
function get_date_diff($date,$dateEnd) { 
    $dStart = new DateTime(date("Y-m-d", $date)); 
    $dEnd = new DateTime(date("Y-m-d", $dateEnd)); 
    $dDiff = $dStart->diff($dEnd); 
    return($dDiff->y.' years <br>'.$dDiff->m.' months <br>'.$dDiff->d. ' days'); 
} 

?>