1k views

Calculate a distance of time in words

This is a really useful function that calculates the distance of time between two dates in PHP. For example "half a minute" or "3 months".

PHP
  1. // Returns the distance of time in words between two dates
  2. function distance_of_time_in_words($from_time, $to_time = null, $include_seconds = false)
  3. {
  4.   $to_time = $to_time? $to_time: time();
  5.  
  6.   $distance_in_minutes = floor(abs($to_time - $from_time) / 60);
  7.   $distance_in_seconds = floor(abs($to_time - $from_time));
  8.  
  9.   $string = '';
  10.   $parameters = array();
  11.  
  12.   if ($distance_in_minutes <= 1)
  13.   {
  14.     if (!$include_seconds)
  15.     {
  16.       $string = $distance_in_minutes == 0 ? 'less than a minute' : '1 minute';
  17.     }
  18.     else
  19.     {
  20.       if ($distance_in_seconds <= 5)
  21.       {
  22.         $string = 'less than 5 seconds';
  23.       }
  24.       else if ($distance_in_seconds >= 6 && $distance_in_seconds <= 10)
  25.       {
  26.         $string = 'less than 10 seconds';
  27.       }
  28.       else if ($distance_in_seconds >= 11 && $distance_in_seconds <= 20)
  29.       {
  30.         $string = 'less than 20 seconds';
  31.       }
  32.       else if ($distance_in_seconds >= 21 && $distance_in_seconds <= 40)
  33.       {
  34.         $string = 'half a minute';
  35.       }
  36.       else if ($distance_in_seconds >= 41 && $distance_in_seconds <= 59)
  37.       {
  38.         $string = 'less than a minute';
  39.       }
  40.       else
  41.       {
  42.         $string = '1 minute';
  43.       }
  44.     }
  45.   }
  46.   else if ($distance_in_minutes >= 2 && $distance_in_minutes <= 44)
  47.   {
  48.     $string = '%minutes% minutes';
  49.     $parameters['%minutes%'] = $distance_in_minutes;
  50.   }
  51.   else if ($distance_in_minutes >= 45 && $distance_in_minutes <= 89)
  52.   {
  53.     $string = 'about 1 hour';
  54.   }
  55.   else if ($distance_in_minutes >= 90 && $distance_in_minutes <= 1439)
  56.   {
  57.     $string = 'about %hours% hours';
  58.     $parameters['%hours%'] = round($distance_in_minutes / 60);
  59.   }
  60.   else if ($distance_in_minutes >= 1440 && $distance_in_minutes <= 2879)
  61.   {
  62.     $string = '1 day';
  63.   }
  64.   else if ($distance_in_minutes >= 2880 && $distance_in_minutes <= 43199)
  65.   {
  66.     $string = '%days% days';
  67.     $parameters['%days%'] = round($distance_in_minutes / 1440);
  68.   }
  69.   else if ($distance_in_minutes >= 43200 && $distance_in_minutes <= 86399)
  70.   {
  71.     $string = 'about 1 month';
  72.   }
  73.   else if ($distance_in_minutes >= 86400 && $distance_in_minutes <= 525959)
  74.   {
  75.     $string = '%months% months';
  76.     $parameters['%months%'] = round($distance_in_minutes / 43200);
  77.   }
  78.   else if ($distance_in_minutes >= 525960 && $distance_in_minutes <= 1051919)
  79.   {
  80.     $string = 'about 1 year';
  81.   }
  82.   else
  83.   {
  84.     $string = 'over %years% years';
  85.     $parameters['%years%'] = floor($distance_in_minutes / 525960);
  86.   }
  87.  
  88.   return strtr($string, $parameters);
  89. }
  90.  

­

Tags: PHP date/time

Embed: