0

My unixtime is 1336608000

which is exactly 2012-5-10.

1 year 10 months ago from today - 2014-03-06

Now what I want to do is output the year and month ago as it is. so I want to display:

1 year 10 months ago.

The most timeAgo functions I see here in Stackoverflow and all over the internet shows the time ago as "2 years ago" which is wrong. I have tried like 8-10 php functions to display the exact month count

So my question is, Is there a php time ago function that shows the exact year and month count instead of rounding it up?

Thanks

Towfiq
  • 397
  • 5
  • 16
  • Since you don't seem to want to do the work yourself, this is a library/tool request, which is off-topic. – Lightness Races in Orbit Mar 05 '14 at 23:58
  • I think you got me wrong. I am not that good of a PHP developer. – Towfiq Mar 05 '14 at 23:59
  • That doesn't change what this site is and isn't for, I'm afraid. Please read the FAQ in future before posting, though you should know after almost 4 years that this is neither a library recommendation website, nor a code generator, nor a message board/forum. – Lightness Races in Orbit Mar 06 '14 at 00:00

1 Answers1

1

I found this answer by Glavić and it works perfectly.

I tweaked it for you...

Usage: echo time_elapsed_string('2012-5-10', true);

function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v;
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}
Community
  • 1
  • 1
Geert Wille
  • 1,656
  • 13
  • 18