2016-07-31 40 views
0

所以我有這樣的代碼,負責顯示多少天前的用戶加入了一個網站:變化數月/年

<?php echo sprintf(__('Joined %s ago','PricerrTheme'),$joined); ?> 

然而,它現在顯示的是,例如「 加入150天前。

我想讓它顯示,例如,什麼」 加入5個月之前。

這應該是一個小編輯到代碼我張貼以上。誰能幫忙?

+0

請帶看看這個https://github.com/briannesbitt/Carbon –

+0

感謝您的回覆。但是這並不能幫助我。我不明白:( – Overloard

回答

0

您可以使用DateTimelook here)PHP類,讓這樣的代碼:

$interval = new DateInterval('P'.$joined.'D'); 
$days = $interval->format('%d'); // This will convert in days 
$months = $interval->format('%m'); // This will convert in months 
$years = $interval->format('%y'); // This will convert in years 

要確定您需要哪種的這些,你可以使用數學條件,如:

if($joined/30 > 0) { 
    if($joined/30 > 12) { 
     // Show years 
    } 
    // Show months 
} else { 
    // Show days 
} 
+0

嘿,感謝您的完整回覆,但不幸的是,我沒有這方面的知識,我嘗試了一些對您有意義的代碼,但每次它打破了網站,所以我實現了它的錯誤肯定 – Overloard

+0

告訴我你是如何實現的代碼,可能,我會幫助你:) – Syncro