[ Index ]

PHP Cross Reference of phpBB-3.1.10-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          $format        = self::format_cache($format, $this->user);
  64          $relative    = ($format['is_short'] && !$force_absolute);
  65          $now        = new self($this->user, 'now', $this->user->timezone);
  66  
  67          $timestamp    = $this->getTimestamp();
  68          $now_ts        = $now->getTimeStamp();
  69  
  70          $delta        = $now_ts - $timestamp;
  71  
  72          if ($relative)
  73          {
  74              /*
  75              * Check the delta is less than or equal to 1 hour
  76              * and the delta not more than a minute in the past
  77              * and the delta is either greater than -5 seconds or timestamp
  78              * and current time are of the same minute (they must be in the same hour already)
  79              * finally check that relative dates are supported by the language pack
  80              */
  81              if ($delta <= 3600 && $delta > -60 &&
  82                  ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60))
  83                  && isset($this->user->lang['datetime']['AGO']))
  84              {
  85                  return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
  86              }
  87              else
  88              {
  89                  $midnight = clone $now;
  90                  $midnight->setTime(0, 0, 0);
  91  
  92                  $midnight    = $midnight->getTimestamp();
  93  
  94                  if ($timestamp <= $midnight + 2 * 86400)
  95                  {
  96                      $day = false;
  97  
  98                      if ($timestamp > $midnight + 86400)
  99                      {
 100                          $day = 'TOMORROW';
 101                      }
 102                      else if ($timestamp > $midnight)
 103                      {
 104                          $day = 'TODAY';
 105                      }
 106                      else if ($timestamp > $midnight - 86400)
 107                      {
 108                          $day = 'YESTERDAY';
 109                      }
 110  
 111                      if ($day !== false)
 112                      {
 113                          // Format using the short formatting and finally swap out the relative token placeholder with the correct value
 114                          return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang']));
 115                      }
 116                  }
 117              }
 118          }
 119  
 120          return strtr(parent::format($format['format_long']), $format['lang']);
 121      }
 122  
 123      /**
 124      * Magic method to convert DateTime object to string
 125      *
 126      * @return string Formatted date time, according to the users default settings.
 127      */
 128  	public function __toString()
 129      {
 130          return $this->format();
 131      }
 132  
 133      /**
 134      * Pre-processes the specified date format
 135      *
 136      * @param string $format Output format
 137      * @param user $user User object to use for localisation
 138      * @return array Processed date format
 139      */
 140  	static protected function format_cache($format, $user)
 141      {
 142          $lang = $user->lang_name;
 143  
 144          if (!isset(self::$format_cache[$lang]))
 145          {
 146              self::$format_cache[$lang] = array();
 147          }
 148  
 149          if (!isset(self::$format_cache[$lang][$format]))
 150          {
 151              // Is the user requesting a friendly date format (i.e. 'Today 12:42')?
 152              self::$format_cache[$lang][$format] = array(
 153                  'is_short'        => strpos($format, self::RELATIVE_WRAPPER) !== false,
 154                  'format_short'    => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1),
 155                  'format_long'    => str_replace(self::RELATIVE_WRAPPER, '', $format),
 156                  'lang'            => array_filter($user->lang['datetime'], 'is_string'),
 157              );
 158  
 159              // Short representation of month in format? Some languages use different terms for the long and short format of May
 160              if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
 161              {
 162                  self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short'];
 163              }
 164          }
 165  
 166          return self::$format_cache[$lang][$format];
 167      }
 168  }


Generated: Sun Feb 19 19:52:41 2017 Cross-referenced by PHPXref 0.7.1