简体   繁体   中英

Change number of days to months/years

So I have this code that is responsible to display how many days ago a user joined a website:

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

However, what it shows now is, for example, " Joined 150 days ago. "

What I want to make it display is, for example, " Joined 5 months ago. "

It should be a small edit to that code I posted above. Can anyone help?

You can use DateTime ( look here ) PHP Class and make code like this:

$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

To decide which of these you need, you can use mathematic conditions, like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM