2016-07-31 90 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