public function since(KDate $date) { $date->addHours(-2); $date = strtotime($date->getDate()); $today = time(); // array of time period chunks $chunks = array(60 * 60 * 24 * 365, 60 * 60 * 24 * 30, 60 * 60 * 24 * 7, 60 * 60 * 24, 60 * 60, 60, 1); $since = $today - $date; for ($i = 0, $j = count($chunks); $i < $j; $i++) { $seconds = $chunks[$i]; if (0 != ($count = floor($since / $seconds))) { break; } } if ($count > 1) { $trans = array('YEARS', 'MONTHS', 'WEEKS', 'DAYS', 'HOURS', 'MINUTES', 'SECONDS'); } else { $trans = array('YEAR', 'MONTH', 'WEEK', 'DAY', 'HOUR', 'MINUTE', 'SECOND'); } $string = JText::sprintf(@$trans[@$i], $count); if ($string) { return JText::sprintf('POSTED_AGO', $string); } else { return JText::_('NOW'); } }
/** * 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); }