Built From Developers For Developers
» Kuikie.com » Snippets » PHP » PHP. Function Ago
  Categories
  PHP
    Arrays
    Date & Time
    Files & Directories
    Globalization
    Images
    OOP
    Other
    Strings
    User Interface
 

PHP. Function Ago

This small PHP function is used to calculate the time span between now and a specified time in the past.

//======================== START OF FUNCTION ==========================//
// FUNCTION: ago                                                       //
//=====================================================================//
function ago($timestamp){
    $timestamp = (int) $timestamp;
    if ($timestamp == 0) return 'n/a';
    $difference = time() - $timestamp;
    $periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");
    for($j = 0; $difference >= $lengths[$j]; $j++) {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if($difference != 1) $periods[$j].= "s";
    $text = "$difference $periods[$j] ago";
    return $text;
}
//=====================================================================//
//  FUNCTION: ago                                                      //
//========================= END OF FUNCTION ===========================//
 
©2012 Sinevia Ltd Terms of Use         Privacy Policy Valid XHTML 1.0 Transitional