Advertisement
Guest User

Untitled

a guest
Aug 7th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.    
  3. /**
  4.  * RelativeTime - pretty printed
  5.  *
  6.  * @package Site5 Helpdesk
  7.  * @author Dejan Marjanovic <dmarjanovic@site5.com>
  8.  */
  9. class Site5_RelativeTime
  10. {
  11.  
  12.     private $interval = '';
  13.  
  14.     public function __construct()
  15.     {
  16.         call_user_func_array(array(__CLASS__, 'calculate'), func_get_args());
  17.     }
  18.  
  19.     public function calculate($start, $end = NULL)
  20.     {
  21.  
  22.         if ( empty($start))
  23.             return false;
  24.        
  25.         if (empty($end))
  26.             $end = time();
  27.        
  28.         if ( ! is_numeric($start))
  29.             $start = strtotime($start);
  30.            
  31.         if ( ! is_numeric($end))
  32.             $end = strtotime($end);        
  33.        
  34.         if($start > $end)
  35.             $future = TRUE;
  36.        
  37.         $start = '@' . $start;
  38.         $end = '@' . $end;
  39.                
  40.         if ( ! ($start instanceof DateTime))
  41.             $start = new DateTime($start);
  42.        
  43.         if ($end === null)
  44.             $end = new DateTime();
  45.        
  46.         if ( ! ($end instanceof DateTime))
  47.             $end = new DateTime($end);
  48.        
  49.         $interval = $end->diff($start);
  50.        
  51.         $get_plural = function($int, $str)
  52.         {
  53.             return $int > 1? $str.'s': $str;
  54.         };
  55.        
  56.         $format = array();
  57.        
  58.         if ($interval->y !== 0)
  59.             $format[] = "%y " . $get_plural($interval->y, "year");
  60.         if ($interval->m !== 0)
  61.             $format[] = "%m " . $get_plural($interval->m, "month");
  62.         if ($interval->d !== 0)
  63.             $format[] = "%d " . $get_plural($interval->d, "day");
  64.         if ($interval->h !== 0)
  65.             $format[] = "%h " . $get_plural($interval->h, "hour");
  66.         if ($interval->i !== 0)
  67.             $format[] = "%i " . $get_plural($interval->i, "minute");
  68.        
  69.        
  70.         if ($interval->s !== 0)
  71.         {
  72.             if ( ! count($format))
  73.             {
  74.                 $this->interval = "less than a minute";
  75.                 return;
  76.             }
  77.             else
  78.             {
  79.                 $format[] = "%s " . $get_plural($interval->s, "second");
  80.             }
  81.         }
  82.        
  83.         if (count($format) > 1)
  84.         {
  85.             $format = array_shift($format) . " and " . array_shift($format);
  86.         }
  87.         else
  88.         {
  89.             $format = array_pop($format);
  90.         }
  91.        
  92.         $tense = ($future === TRUE)? 'from now': 'ago';
  93.        
  94.         $this->interval = $interval->format($format) . ' ' . $tense;
  95.     }
  96.    
  97.     public function __toString()
  98.     {
  99.         return $this->interval;
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement