Advertisement
kiler129

Untitled

Apr 24th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.66 KB | None | 0 0
  1. <?php
  2.   static public function humanTimeDiff($from, $to, $precise=true) {
  3.    $difference = $to - $from;
  4.    $periods = array("sec", "min", "hour", "day", "week", "month", "years", "decade");
  5.    $lengths = array("60","60","24","7","4.35","12","10");
  6.    
  7.    for($j = 0; $difference >= $lengths[$j]; $j++) { $mod = $difference%$lengths[$j]; $difference /= $lengths[$j]; }
  8.    $difference = round($difference);
  9.  
  10.    if($difference != 1) $periods[$j].= "s";
  11.    $text = "$difference ".(($difference!=1)?($periods[$j]."s"):$periods[$j]);
  12.    
  13.    if($precise && $mod > 0) $text .= " ".$mod." ".(($mod!=1)?($periods[($j-1)]."s"):$periods[($j-1)]);
  14.    return $text;
  15.   }
  16.  
  17. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement