Example #1
0
 /**
  * Formats the current date time into the specified format
  *
  * @param string $format Optional format to use for output, defaults to users chosen format
  * @param boolean $force_absolute Force output of a non relative date
  * @return string Formatted date time
  */
 public function format($format = '', $force_absolute = false)
 {
     $format = $format ? $format : $this->user->date_format;
     $format = self::format_cache($format, $this->user);
     $relative = $format['is_short'] && !$force_absolute;
     $now = new self($this->user, 'now', $this->user->timezone);
     $timestamp = $this->getTimestamp();
     $now_ts = $now->getTimeStamp();
     $delta = $now_ts - $timestamp;
     if ($relative) {
         /*
          * Check the delta is less than or equal to 1 hour
          * and the delta not more than a minute in the past
          * and the delta is either greater than -5 seconds or timestamp
          * and current time are of the same minute (they must be in the same hour already)
          * finally check that relative dates are supported by the language pack
          */
         if ($delta <= 3600 && $delta > -60 && ($delta >= -5 || $now_ts / 60 % 60 == $timestamp / 60 % 60) && isset($this->user->lang['datetime']['AGO'])) {
             return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
         } else {
             $midnight = clone $now;
             $midnight->setTime(0, 0, 0);
             $midnight = $midnight->getTimestamp();
             if ($timestamp <= $midnight + 2 * 86400) {
                 $day = false;
                 if ($timestamp > $midnight + 86400) {
                     $day = 'TOMORROW';
                 } else {
                     if ($timestamp > $midnight) {
                         $day = 'TODAY';
                     } else {
                         if ($timestamp > $midnight - 86400) {
                             $day = 'YESTERDAY';
                         }
                     }
                 }
                 if ($day !== false) {
                     // Format using the short formatting and finally swap out the relative token placeholder with the correct value
                     return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang']));
                 }
             }
         }
     }
     return strtr(parent::format($format['format_long']), $format['lang']);
 }