[ Index ]

PHP Cross Reference of phpBB-3.2.2-deutsch

title

Body

[close]

/phpbb/ -> datetime.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb;
  15  
  16  /**
  17  * phpBB custom extensions to the PHP DateTime class
  18  * This handles the relative formats phpBB employs
  19  */
  20  class datetime extends \DateTime
  21  {
  22      /**
  23      * String used to wrap the date segment which should be replaced by today/tomorrow/yesterday
  24      */
  25      const RELATIVE_WRAPPER = '|';
  26  
  27      /**
  28      * @var user User who is the context for this DateTime instance
  29      */
  30      protected $user;
  31  
  32      /**
  33      * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached.
  34      */
  35      static protected $format_cache = array();
  36  
  37      /**
  38      * Constructs a new instance of \phpbb\datetime, expanded to include an argument to inject
  39      * the user context and modify the timezone to the users selected timezone if one is not set.
  40      *
  41      * @param user $user object for context.
  42      * @param string $time String in a format accepted by strtotime().
  43      * @param \DateTimeZone $timezone Time zone of the time.
  44      */
  45  	public function __construct($user, $time = 'now', \DateTimeZone $timezone = null)
  46      {
  47          $this->user    = $user;
  48          $timezone    = $timezone ?: $this->user->timezone;
  49  
  50          parent::__construct($time, $timezone);
  51      }
  52  
  53      /**
  54      * Formats the current date time into the specified format
  55      *
  56      * @param string $format Optional format to use for output, defaults to users chosen format
  57      * @param boolean $force_absolute Force output of a non relative date
  58      * @return string Formatted date time
  59      */
  60  	public function format($format = '', $force_absolute = false)
  61      {
  62          $format        = $format ? $format : $this->user->date_format;
  63  
  64          if (substr($this->user->lang_name, 0,2) != 'en')
  65          {
  66              $format = preg_replace('/([^\\\])S/','$1', $format);
  67          }
  68  
  69          $format        = self::format_cache($format, $this->user);
  70          $relative    = ($format['is_short'] && !$force_absolute);
  71          $now        = new self($this->user, 'now', $this->user->timezone);
  72  
  73          $timestamp    = $this->getTimestamp();
  74          $now_ts        = $now->getTimeStamp();
  75  
  76          $delta        = $now_ts - $timestamp;
  77  
  78          if ($relative)
  79          {
  80              /*
  81              * Check the delta is less than or equal to 1 hour
  82              * and the delta not more than a minute in the past
  83              * and the delta is either greater than -5 seconds or timestamp
  84              * and current time are of the same minute (they must be in the same hour already)
  85              * finally check that relative dates are supported by the language pack
  86              */
  87              if ($delta <= 3600 && $delta > -60 &&
  88                  ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60))
  89                  && isset($this->user->lang['datetime']['AGO']))
  90              {
  91                  return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
  92              }
  93              else
  94              {
  95                  $midnight = clone $now;
  96                  $midnight->setTime(0, 0, 0);
  97  
  98                  $midnight    = $midnight->getTimestamp();
  99  
 100                  if ($timestamp <= $midnight + 2 * 86400)
 101                  {
 102                      $day = false;
 103  
 104                      if ($timestamp > $midnight + 86400)
 105                      {
 106                          $day = 'TOMORROW';
 107                      }
 108                      else if ($timestamp > $midnight)
 109                      {
 110                          $day = 'TODAY';
 111                      }
 112                      else if ($timestamp > $midnight - 86400)
 113                      {
 114                          $day = 'YESTERDAY';
 115                      }
 116  
 117                      if ($day !== false)
 118                      {
 119                          // Format using the short formatting and finally swap out the relative token placeholder with the correct value
 120                          return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang']));
 121                      }
 122                  }
 123              }
 124          }
 125  
 126          return strtr(parent::format($format['format_long']), $format['lang']);
 127      }
 128  
 129      /**
 130      * Magic method to convert DateTime object to string
 131      *
 132      * @return string Formatted date time, according to the users default settings.
 133      */
 134  	public function __toString()
 135      {
 136          return $this->format();
 137      }
 138  
 139      /**
 140      * Pre-processes the specified date format
 141      *
 142      * @param string $format Output format
 143      * @param user $user User object to use for localisation
 144      * @return array Processed date format
 145      */
 146  	static protected function format_cache($format, $user)
 147      {
 148          $lang = $user->lang_name;
 149  
 150          if (!isset(self::$format_cache[$lang]))
 151          {
 152              self::$format_cache[$lang] = array();
 153          }
 154  
 155          if (!isset(self::$format_cache[$lang][$format]))
 156          {
 157              // Is the user requesting a friendly date format (i.e. 'Today 12:42')?
 158              self::$format_cache[$lang][$format] = array(
 159                  'is_short'        => strpos($format, self::RELATIVE_WRAPPER) !== false,
 160                  'format_short'    => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1),
 161                  'format_long'    => str_replace(self::RELATIVE_WRAPPER, '', $format),
 162                  'lang'            => array_filter($user->lang['datetime'], 'is_string'),
 163              );
 164  
 165              // Short representation of month in format? Some languages use different terms for the long and short format of May
 166              if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
 167              {
 168                  self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short'];
 169              }
 170          }
 171  
 172          return self::$format_cache[$lang][$format];
 173      }
 174  }


Generated: Thu Jan 11 23:14:31 2018 Cross-referenced by PHPXref 0.7.1