Calculate time like 1 sec ago in php

Hi, Calculate timestamp in php like 1 sec ago, 2 minutes ago, 3 days ago, 4 weeks ago, 5 months ago, 6 years ago and so on. Use this simple php function. Just pass the unix timestamp as the parameter. It will return the output in simple text like 1 sec ago.


  function time_ago($time){
    $time_diff = time() - $time;
    $seconds   = $time_diff;
    $minutes   = round($time_diff / 60);
    $hours     = round($time_diff / (60 * 60));
    $days      = round($time_diff / (60 * 60 * 24));
    $weeks     = round($time_diff / (60 * 60 * 24 * 7));
    $months    = round($time_diff / (60 * 60 * 24 * 30));
    $years     = round($time_diff / (60 * 60 * 24 * 365));
    
    if ($seconds <= 60) {
        $ago =  "$seconds seconds ago";
    } else if ($minutes <= 60) {
        if ($minutes == 1) {
            $ago =  "1 minute ago";
        } else {
            $ago =  "$minutes minutes ago";
        }
    } else if ($hours <= 24) {
        if ($hours == 1) {
            $ago =  "1 hour ago";
        } else {
            $ago =  "$hours hours ago";
        }
    } else if ($days <= 7) {
        if ($days == 1) {
            $ago =  "1 day ago";
        } else {
            $ago =  "$days days ago";
        }
    } else if ($weeks <= 4) {
        if ($weeks == 1) {
            $ago =  "1 week ago";
        } else {
            $ago =  "$weeks weeks ago";
        }
    } else if ($months <= 12) {
        if ($months == 1) {
            $ago =  "1 month ago";
        } else {
            $ago =  "$months months ago";
        }
    } else {
        if ($years == 1) {
            $ago =  "1 year ago";
        } else {
            $ago =  "$years years ago";
        }
    }
  return $ago;
  }


Call function like this
$time ="13344298422";
echo time_ago($time);
Calculate time like 1 sec ago in php Calculate time like 1 sec ago in php Reviewed by JS Pixels on April 15, 2012 Rating: 5

No comments:

Altaf Web. Powered by Blogger.