0

I am trying to convert timestamp to ago. I have done it this way

function ago2($time_ago) {
    $time_ago =  strtotime($time_ago) ? strtotime($time_ago) : $time_ago;
    $time  = time() - $time_ago;
    switch($time):
        case $time <= 60;
            return ($time == 1) ? '1 second ago' : $time.' seconds ago';
        case $time >= 60 && $time < 3600;
            return (round($time/60) == 1) ? '1 minute ago' : round($time/60).' minutes ago';
        case $time >= 3600 && $time < 86400;
            return (round($time/3600) == 1) ? '1 hour ago' : round($time/3600).' hours ago';
        case $time >= 86400 && $time < 604800;
            return (round($time/86400) == 1) ? '1 day ago' : round($time/86400).' days ago';
        case $time >= 604800 && $time < 2600640;
            return (round($time/604800) == 1) ? '1 week ago' : round($time/604800).' weeks ago';
        case $time >= 2600640 && $time < 31207680;
            return (round($time/2600640) == 1) ? '1 month ago' : round($time/2600640).' months ago';
        case $time >= 31207680;
            return (round($time/31207680) == 1) ? '1 year ago' : round($time/31207680).' years ago' ;
    endswitch;
}

It works well. However, if the date is older than 1 year, it just puts timestamp instead of converting to xxx ago. Where I am doing mistake ?...

  • Is this question closed? I was going to suggest taking a look at the Carbon DateTime extension: http://carbon.nesbot.com/docs/#api-humandiff Couldn't live without it! – Jethro Hazelhurst Feb 15 '17 at 12:32
  • Look at [DateTime](http://php.net/manual/en/class.datetime.php) and [DateInterval](http://php.net/manual/en/class.dateinterval.php) classes – RiggsFolly Feb 15 '17 at 12:33
  • How come you close my question ?! I I just did it my way, other methods for doing the same job doesnt make sense to me. I am asking what is the problem on my method! – Zikreden Matmazel Feb 15 '17 at 12:43

0 Answers0