jesobreira

time since portugues

Jun 28th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.75 KB | None | 0 0
  1. function time_since($since) {
  2.     $chunks = array(
  3.         array(60 * 60 * 24 * 365 , 'ano'),
  4.         array(60 * 60 * 24 * 30 , 'mes'),
  5.         array(60 * 60 * 24 * 7, 'semana'),
  6.         array(60 * 60 * 24 , 'dia'),
  7.         array(60 * 60 , 'hora'),
  8.         array(60 , 'minuto'),
  9.         array(1 , 'segundo')
  10.     );
  11.  
  12.     for ($i = 0, $j = count($chunks); $i < $j; $i++) {
  13.         $seconds = $chunks[$i][0];
  14.         $name = $chunks[$i][1];
  15.         if (($count = floor($since / $seconds)) != 0) {
  16.             break;
  17.         }
  18.     }
  19.  
  20.     $print = ($count == 1) ? '1 '.$name : "$count {$name}s";
  21.     if($print=="$count mess") $print = "$count meses";
  22.    
  23.     return 'há '.$print;
  24. }
  25.  
  26. echo time_since(1372440149); // unix timestamp (pode usar strtotime())
Add Comment
Please, Sign In to add comment