Ejemplo n.º 1
0
 /**
  * Return a human friendly format of the date.
  *
  * @param KDate $date
  * @param array $config Optional array to pass format
  *
  * @return string
  */
 public function humanize($date, $config = array())
 {
     $config = new KConfig($config);
     $config->append(array('format' => '%B %d %Y', 'relative' => false, 'offset' => null));
     $format = $config->format;
     $diff = $this->_current_time->getDate(DATE_FORMAT_UNIXTIME) - $date->getDate(DATE_FORMAT_UNIXTIME);
     if ($config->relative) {
         $timeLeft = $diff < 0 ? '-FUTURE' : '';
         $diff = abs($diff);
         if ($diff < 1) {
             return sprintf(JText::_('LIB-AN-DATE-MOMENT'), $diff);
         }
         if ($diff < 60) {
             return $diff > 1 ? sprintf(JText::_('LIB-AN-DATE-SECONDS' . $timeLeft), $diff) : sprintf(JText::_('LIB-AN-DATE-SECOND' . $timeLeft), $diff);
         }
         $diff = round($diff / 60);
         if ($diff < 60) {
             return $diff > 1 ? sprintf(JText::_('LIB-AN-DATE-MINUTES' . $timeLeft), $diff) : sprintf(JText::_('LIB-AN-DATE-MINUTE' . $timeLeft), $diff);
         }
         $diff = round($diff / 60);
         if ($diff < 24) {
             return $diff > 1 ? sprintf(JText::_('LIB-AN-DATE-HOURS' . $timeLeft), $diff) : sprintf(JText::_('LIB-AN-DATE-HOUR' . $timeLeft), $diff);
         }
         $diff = round($diff / 24);
         if ($diff < 7) {
             return $diff > 1 ? sprintf(JText::_('LIB-AN-DATE-DAYS' . $timeLeft), $diff) : sprintf(JText::_('LIB-AN-DATE-DAY' . $timeLeft), $diff);
         }
         $diff = round($diff / 7);
         if ($diff < 4) {
             return $diff > 1 ? sprintf(JText::_('LIB-AN-DATE-WEEKS' . $timeLeft), $diff) : sprintf(JText::_('LIB-AN-DATE-WEEK' . $timeLeft), $diff);
         }
     } elseif ($config->offset) {
         $date->addHours($config->offset);
     }
     return $date->getDate($format);
 }