faysalmirmd

Find Relative time Php

May 14th, 2013
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1. /**
  2.       *
  3.       * @desc relative time generator
  4.       * @param timestamp $time
  5.       * @return string
  6.       */  
  7.      static function _time_ago_en($time)
  8.      {
  9.         if(!is_numeric($time))
  10.             $time = strtotime($time);
  11.  
  12.         $periods = array("second", "minute", "hour", "day", "week", "month", "year","decade", "age");
  13.         $lengths = array("60","60","24","7","4.35","12","10","100");
  14.  
  15.         $now = time();
  16.  
  17.         $difference = $now - $time;
  18.         if ($difference <= 10 && $difference >= 0)
  19.             return $tense = 'just now';
  20.         elseif($difference > 0)
  21.             $tense = 'ago';
  22.         elseif($difference < 0)
  23.             $tense = 'later';
  24.  
  25.         for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  26.             $difference /= $lengths[$j];
  27.         }
  28.  
  29.         $difference = round($difference);
  30.  
  31.         $period =  $periods[$j] . ($difference >1 ? 's' :'');
  32.         return "{$difference} {$period} {$tense} ";
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment