Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
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. |
|
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
datetime.php
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 #[\ReturnTypeWillChange]
061 public function format($format = '', $force_absolute = false)
062 {
063 $format = $format ? $format : $this->user->date_format;
064
065 if (substr($this->user->lang_name, 0,2) != 'en')
066 {
067 $format = preg_replace('/([^\\\])S/','$1', $format);
068 }
069
070 $format = self::format_cache($format, $this->user);
071 $relative = ($format['is_short'] && !$force_absolute);
072 $now = new self($this->user, 'now', $this->user->timezone);
073
074 $timestamp = $this->getTimestamp();
075 $now_ts = $now->getTimeStamp();
076
077 $delta = $now_ts - $timestamp;
078
079 if ($relative)
080 {
081 /*
082 * Check the delta is less than or equal to 1 hour
083 * and the delta not more than a minute in the past
084 * and the delta is either greater than -5 seconds or timestamp
085 * and current time are of the same minute (they must be in the same hour already)
086 * finally check that relative dates are supported by the language pack
087 */
088 if ($delta <= 3600 && $delta > -60 &&
089 ($delta >= -5 || (($now_ts / 60) % 60) == (($timestamp / 60) % 60))
090 && isset($this->user->lang['datetime']['AGO']))
091 {
092 return $this->user->lang(array('datetime', 'AGO'), max(0, (int) floor($delta / 60)));
093 }
094 else
095 {
096 $midnight = clone $now;
097 $midnight->setTime(0, 0, 0);
098
099 $midnight = $midnight->getTimestamp();
100
101 if ($timestamp < $midnight + 2 * 86400)
102 {
103 $day = false;
104
105 if ($timestamp >= $midnight + 86400)
106 {
107 $day = 'TOMORROW';
108 }
109 else if ($timestamp >= $midnight)
110 {
111 $day = 'TODAY';
112 }
113 else if ($timestamp >= $midnight - 86400)
114 {
115 $day = 'YESTERDAY';
116 }
117
118 if ($day !== false)
119 {
120 // Format using the short formatting and finally swap out the relative token placeholder with the correct value
121 return str_replace(self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER, $this->user->lang['datetime'][$day], strtr(parent::format($format['format_short']), $format['lang']));
122 }
123 }
124 }
125 }
126
127 return strtr(parent::format($format['format_long']), $format['lang']);
128 }
129
130 /**
131 * Magic method to convert DateTime object to string
132 *
133 * @return string Formatted date time, according to the users default settings.
134 */
135 public function __toString()
136 {
137 return $this->format();
138 }
139
140 /**
141 * Pre-processes the specified date format
142 *
143 * @param string $format Output format
144 * @param user $user User object to use for localisation
145 * @return array Processed date format
146 */
147 static protected function format_cache($format, $user)
148 {
149 $lang = $user->lang_name;
150
151 if (!isset(self::$format_cache[$lang]))
152 {
153 self::$format_cache[$lang] = array();
154 }
155
156 if (!isset(self::$format_cache[$lang][$format]))
157 {
158 // Is the user requesting a friendly date format (i.e. 'Today 12:42')?
159 self::$format_cache[$lang][$format] = array(
160 'is_short' => strpos($format, self::RELATIVE_WRAPPER) !== false,
161 'format_short' => substr($format, 0, strpos($format, self::RELATIVE_WRAPPER)) . self::RELATIVE_WRAPPER . self::RELATIVE_WRAPPER . substr(strrchr($format, self::RELATIVE_WRAPPER), 1),
162 'format_long' => str_replace(self::RELATIVE_WRAPPER, '', $format),
163 'lang' => array_filter($user->lang['datetime'], 'is_string'),
164 );
165
166 // Short representation of month in format? Some languages use different terms for the long and short format of May
167 if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
168 {
169 self::$format_cache[$lang][$format]['lang']['May'] = $user->lang['datetime']['May_short'];
170 }
171 }
172
173 return self::$format_cache[$lang][$format];
174 }
175 }
176