Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

datetime.php

Zuletzt modifiziert: 09.10.2024, 12:51 - Dateigröße: 5.02 KiB


 001  <?php
 002  /**
 003  *
 004  * This file is part of the phpBB Forum Software package.
 005  *
 006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 007  * @license GNU General Public License, version 2 (GPL-2.0)
 008  *
 009  * For full copyright and license information, please see
 010  * the docs/CREDITS.txt file.
 011  *
 012  */
 013   
 014  namespace phpbb;
 015   
 016  /**
 017  * phpBB custom extensions to the PHP DateTime class
 018  * This handles the relative formats phpBB employs
 019  */
 020  class datetime extends \DateTime
 021  {
 022      /**
 023      * String used to wrap the date segment which should be replaced by today/tomorrow/yesterday
 024      */
 025      const RELATIVE_WRAPPER = '|';
 026   
 027      /**
 028      * @var user User who is the context for this DateTime instance
 029      */
 030      protected $user;
 031   
 032      /**
 033      * @var array Date formats are preprocessed by phpBB, to save constant recalculation they are cached.
 034      */
 035      static protected $format_cache = array();
 036   
 037      /**
 038      * Constructs a new instance of \phpbb\datetime, expanded to include an argument to inject
 039      * the user context and modify the timezone to the users selected timezone if one is not set.
 040      *
 041      * @param user $user object for context.
 042      * @param string $time String in a format accepted by strtotime().
 043      * @param \DateTimeZone $timezone Time zone of the time.
 044      */
 045      public function __construct($user, $time = 'now', \DateTimeZone $timezone = null)
 046      {
 047          $this->user    = $user;
 048          $timezone    = $timezone ?: $this->user->timezone;
 049   
 050          parent::__construct($time, $timezone);
 051      }
 052   
 053      /**
 054      * Formats the current date time into the specified format
 055      *
 056      * @param string $format Optional format to use for output, defaults to users chosen format
 057      * @param boolean $force_absolute Force output of a non relative date
 058      * @return string Formatted date time
 059      */
 060      public function format($format = '', $force_absolute = false)
 061      {
 062          $format        = $format ? $format : $this->user->date_format;
 063          $format        = self::format_cache($format, $this->user);
 064          $relative    = ($format['is_short'] && !$force_absolute);
 065          $now        = new self($this->user, 'now', $this->user->timezone);
 066   
 067          $timestamp    = $this->getTimestamp();
 068          $now_ts        = $now->getTimeStamp();
 069   
 070          $delta        = $now_ts - $timestamp;
 071   
 072          if ($relative)
 073          {
 074              /*
 075              * Check the delta is less than or equal to 1 hour
 076              * and the delta not more than a minute in the past
 077              * and the delta is either greater than -5 seconds or timestamp
 078              * and current time are of the same minute (they must be in the same hour already)
 079              * finally check that relative dates are supported by the language pack
 080              */
 081              if ($delta <= 3600 && $delta > -60 &&
 082                  ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60))
 083                  && isset($this->user->lang['datetime']['AGO']))
 084              {
 085                  return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
 086              }
 087              else
 088              {
 089                  $midnight = clone $now;
 090                  $midnight->setTime(0, 0, 0);
 091   
 092                  $midnight    = $midnight->getTimestamp();
 093   
 094                  if ($timestamp <= $midnight + 2 * 86400)
 095                  {
 096                      $day = false;
 097   
 098                      if ($timestamp > $midnight + 86400)
 099                      {
 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  }
 169