[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2006 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Date; 11 12 use Joomla\CMS\Factory; 13 use Joomla\CMS\Language\Text; 14 use Joomla\Database\DatabaseDriver; 15 16 // phpcs:disable PSR1.Files.SideEffects 17 \defined('JPATH_PLATFORM') or die; 18 // phpcs:enable PSR1.Files.SideEffects 19 20 /** 21 * Date is a class that stores a date and provides logic to manipulate 22 * and render that date in a variety of formats. 23 * 24 * @method Date|bool add(\DateInterval $interval) Adds an amount of days, months, years, hours, minutes and seconds to a Date object. 25 * @method Date|bool sub(\DateInterval $interval) Subtracts an amount of days, months, years, hours, minutes and seconds from a Date object. 26 * @method Date|bool modify(string $modify) Alter the timestamp of this object by incre/decre-menting in a format accepted by strtotime(). 27 * 28 * @property-read string $daysinmonth t - Number of days in the given month. 29 * @property-read string $dayofweek N - ISO-8601 numeric representation of the day of the week. 30 * @property-read string $dayofyear z - The day of the year (starting from 0). 31 * @property-read boolean $isleapyear L - Whether it's a leap year. 32 * @property-read string $day d - Day of the month, 2 digits with leading zeros. 33 * @property-read string $hour H - 24-hour format of an hour with leading zeros. 34 * @property-read string $minute i - Minutes with leading zeros. 35 * @property-read string $second s - Seconds with leading zeros. 36 * @property-read string $microsecond u - Microseconds with leading zeros. 37 * @property-read string $month m - Numeric representation of a month, with leading zeros. 38 * @property-read string $ordinal S - English ordinal suffix for the day of the month, 2 characters. 39 * @property-read string $week W - ISO-8601 week number of year, weeks starting on Monday. 40 * @property-read string $year Y - A full numeric representation of a year, 4 digits. 41 * 42 * @since 1.7.0 43 */ 44 class Date extends \DateTime 45 { 46 public const DAY_ABBR = "\x021\x03"; 47 public const DAY_NAME = "\x022\x03"; 48 public const MONTH_ABBR = "\x023\x03"; 49 public const MONTH_NAME = "\x024\x03"; 50 51 /** 52 * The format string to be applied when using the __toString() magic method. 53 * 54 * @var string 55 * @since 1.7.0 56 */ 57 public static $format = 'Y-m-d H:i:s'; 58 59 /** 60 * Placeholder for a \DateTimeZone object with GMT as the time zone. 61 * 62 * @var object 63 * @since 1.7.0 64 * 65 * @deprecated 5.0 Without replacement 66 */ 67 protected static $gmt; 68 69 /** 70 * Placeholder for a \DateTimeZone object with the default server 71 * time zone as the time zone. 72 * 73 * @var object 74 * @since 1.7.0 75 * 76 * @deprecated 5.0 Without replacement 77 */ 78 protected static $stz; 79 80 /** 81 * The \DateTimeZone object for usage in rending dates as strings. 82 * 83 * @var \DateTimeZone 84 * @since 3.0.0 85 */ 86 protected $tz; 87 88 /** 89 * Constructor. 90 * 91 * @param string $date String in a format accepted by strtotime(), defaults to "now". 92 * @param mixed $tz Time zone to be used for the date. Might be a string or a DateTimeZone object. 93 * 94 * @since 1.7.0 95 */ 96 public function __construct($date = 'now', $tz = null) 97 { 98 // Create the base GMT and server time zone objects. 99 if (empty(self::$gmt) || empty(self::$stz)) { 100 // @TODO: This code block stays here only for B/C, can be removed in 5.0 101 self::$gmt = new \DateTimeZone('GMT'); 102 self::$stz = new \DateTimeZone(@date_default_timezone_get()); 103 } 104 105 // If the time zone object is not set, attempt to build it. 106 if (!($tz instanceof \DateTimeZone)) { 107 if (\is_string($tz)) { 108 $tz = new \DateTimeZone($tz); 109 } else { 110 $tz = new \DateTimeZone('UTC'); 111 } 112 } 113 114 // Backup active time zone 115 $activeTZ = date_default_timezone_get(); 116 117 // Force UTC timezone for correct time handling 118 date_default_timezone_set('UTC'); 119 120 // If the date is numeric assume a unix timestamp and convert it. 121 $date = is_numeric($date) ? date('c', $date) : $date; 122 123 // Call the DateTime constructor. 124 parent::__construct($date, $tz); 125 126 // Restore previously active timezone 127 date_default_timezone_set($activeTZ); 128 129 // Set the timezone object for access later. 130 $this->tz = $tz; 131 } 132 133 /** 134 * Magic method to access properties of the date given by class to the format method. 135 * 136 * @param string $name The name of the property. 137 * 138 * @return mixed A value if the property name is valid, null otherwise. 139 * 140 * @since 1.7.0 141 */ 142 public function __get($name) 143 { 144 $value = null; 145 146 switch ($name) { 147 case 'daysinmonth': 148 $value = $this->format('t', true); 149 break; 150 151 case 'dayofweek': 152 $value = $this->format('N', true); 153 break; 154 155 case 'dayofyear': 156 $value = $this->format('z', true); 157 break; 158 159 case 'isleapyear': 160 $value = (bool) $this->format('L', true); 161 break; 162 163 case 'day': 164 $value = $this->format('d', true); 165 break; 166 167 case 'hour': 168 $value = $this->format('H', true); 169 break; 170 171 case 'minute': 172 $value = $this->format('i', true); 173 break; 174 175 case 'second': 176 $value = $this->format('s', true); 177 break; 178 179 case 'month': 180 $value = $this->format('m', true); 181 break; 182 183 case 'ordinal': 184 $value = $this->format('S', true); 185 break; 186 187 case 'week': 188 $value = $this->format('W', true); 189 break; 190 191 case 'year': 192 $value = $this->format('Y', true); 193 break; 194 195 default: 196 $trace = debug_backtrace(); 197 trigger_error( 198 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], 199 E_USER_NOTICE 200 ); 201 } 202 203 return $value; 204 } 205 206 /** 207 * Magic method to render the date object in the format specified in the public 208 * static member Date::$format. 209 * 210 * @return string The date as a formatted string. 211 * 212 * @since 1.7.0 213 */ 214 public function __toString() 215 { 216 return (string) parent::format(self::$format); 217 } 218 219 /** 220 * Proxy for new Date(). 221 * 222 * @param string $date String in a format accepted by strtotime(), defaults to "now". 223 * @param mixed $tz Time zone to be used for the date. 224 * 225 * @return Date 226 * 227 * @since 1.7.3 228 */ 229 public static function getInstance($date = 'now', $tz = null) 230 { 231 return new static($date, $tz); 232 } 233 234 /** 235 * Translates day of week number to a string. 236 * 237 * @param integer $day The numeric day of the week. 238 * @param boolean $abbr Return the abbreviated day string? 239 * 240 * @return string The day of the week. 241 * 242 * @since 1.7.0 243 */ 244 public function dayToString($day, $abbr = false) 245 { 246 switch ($day) { 247 case 0: 248 return $abbr ? Text::_('SUN') : Text::_('SUNDAY'); 249 case 1: 250 return $abbr ? Text::_('MON') : Text::_('MONDAY'); 251 case 2: 252 return $abbr ? Text::_('TUE') : Text::_('TUESDAY'); 253 case 3: 254 return $abbr ? Text::_('WED') : Text::_('WEDNESDAY'); 255 case 4: 256 return $abbr ? Text::_('THU') : Text::_('THURSDAY'); 257 case 5: 258 return $abbr ? Text::_('FRI') : Text::_('FRIDAY'); 259 case 6: 260 return $abbr ? Text::_('SAT') : Text::_('SATURDAY'); 261 } 262 } 263 264 /** 265 * Gets the date as a formatted string in a local calendar. 266 * 267 * @param string $format The date format specification string (see {@link PHP_MANUAL#date}) 268 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. 269 * @param boolean $translate True to translate localised strings 270 * 271 * @return string The date string in the specified format format. 272 * 273 * @since 1.7.0 274 */ 275 public function calendar($format, $local = false, $translate = true) 276 { 277 return $this->format($format, $local, $translate); 278 } 279 280 /** 281 * Gets the date as a formatted string. 282 * 283 * @param string $format The date format specification string (see {@link PHP_MANUAL#date}) 284 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. 285 * @param boolean $translate True to translate localised strings 286 * 287 * @return string The date string in the specified format format. 288 * 289 * @since 1.7.0 290 */ 291 #[\ReturnTypeWillChange] 292 public function format($format, $local = false, $translate = true) 293 { 294 if ($translate) { 295 // Do string replacements for date format options that can be translated. 296 $format = preg_replace('/(^|[^\\\])D/', "\\1" . self::DAY_ABBR, $format); 297 $format = preg_replace('/(^|[^\\\])l/', "\\1" . self::DAY_NAME, $format); 298 $format = preg_replace('/(^|[^\\\])M/', "\\1" . self::MONTH_ABBR, $format); 299 $format = preg_replace('/(^|[^\\\])F/', "\\1" . self::MONTH_NAME, $format); 300 } 301 302 // If the returned time should not be local use UTC. 303 if ($local == false) { 304 parent::setTimezone(new \DateTimeZone('UTC')); 305 } 306 307 // Format the date. 308 $return = parent::format($format); 309 310 if ($translate) { 311 // Manually modify the month and day strings in the formatted time. 312 if (strpos($return, self::DAY_ABBR) !== false) { 313 $return = str_replace(self::DAY_ABBR, $this->dayToString(parent::format('w'), true), $return); 314 } 315 316 if (strpos($return, self::DAY_NAME) !== false) { 317 $return = str_replace(self::DAY_NAME, $this->dayToString(parent::format('w')), $return); 318 } 319 320 if (strpos($return, self::MONTH_ABBR) !== false) { 321 $return = str_replace(self::MONTH_ABBR, $this->monthToString(parent::format('n'), true), $return); 322 } 323 324 if (strpos($return, self::MONTH_NAME) !== false) { 325 $return = str_replace(self::MONTH_NAME, $this->monthToString(parent::format('n')), $return); 326 } 327 } 328 329 if ($local == false && $this->tz !== null) { 330 parent::setTimezone($this->tz); 331 } 332 333 return $return; 334 } 335 336 /** 337 * Get the time offset from GMT in hours or seconds. 338 * 339 * @param boolean $hours True to return the value in hours. 340 * 341 * @return float The time offset from GMT either in hours or in seconds. 342 * 343 * @since 1.7.0 344 */ 345 public function getOffsetFromGmt($hours = false) 346 { 347 return (float) $hours ? ($this->tz->getOffset($this) / 3600) : $this->tz->getOffset($this); 348 } 349 350 /** 351 * Translates month number to a string. 352 * 353 * @param integer $month The numeric month of the year. 354 * @param boolean $abbr If true, return the abbreviated month string 355 * 356 * @return string The month of the year. 357 * 358 * @since 1.7.0 359 */ 360 public function monthToString($month, $abbr = false) 361 { 362 switch ($month) { 363 case 1: 364 return $abbr ? Text::_('JANUARY_SHORT') : Text::_('JANUARY'); 365 case 2: 366 return $abbr ? Text::_('FEBRUARY_SHORT') : Text::_('FEBRUARY'); 367 case 3: 368 return $abbr ? Text::_('MARCH_SHORT') : Text::_('MARCH'); 369 case 4: 370 return $abbr ? Text::_('APRIL_SHORT') : Text::_('APRIL'); 371 case 5: 372 return $abbr ? Text::_('MAY_SHORT') : Text::_('MAY'); 373 case 6: 374 return $abbr ? Text::_('JUNE_SHORT') : Text::_('JUNE'); 375 case 7: 376 return $abbr ? Text::_('JULY_SHORT') : Text::_('JULY'); 377 case 8: 378 return $abbr ? Text::_('AUGUST_SHORT') : Text::_('AUGUST'); 379 case 9: 380 return $abbr ? Text::_('SEPTEMBER_SHORT') : Text::_('SEPTEMBER'); 381 case 10: 382 return $abbr ? Text::_('OCTOBER_SHORT') : Text::_('OCTOBER'); 383 case 11: 384 return $abbr ? Text::_('NOVEMBER_SHORT') : Text::_('NOVEMBER'); 385 case 12: 386 return $abbr ? Text::_('DECEMBER_SHORT') : Text::_('DECEMBER'); 387 } 388 } 389 390 /** 391 * Method to wrap the setTimezone() function and set the internal time zone object. 392 * 393 * @param \DateTimeZone $tz The new \DateTimeZone object. 394 * 395 * @return Date 396 * 397 * @since 1.7.0 398 * @note This method can't be type hinted due to a PHP bug: https://bugs.php.net/bug.php?id=61483 399 */ 400 #[\ReturnTypeWillChange] 401 public function setTimezone($tz) 402 { 403 $this->tz = $tz; 404 405 return parent::setTimezone($tz); 406 } 407 408 /** 409 * Gets the date as an ISO 8601 string. IETF RFC 3339 defines the ISO 8601 format 410 * and it can be found at the IETF Web site. 411 * 412 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. 413 * 414 * @return string The date string in ISO 8601 format. 415 * 416 * @link http://www.ietf.org/rfc/rfc3339.txt 417 * @since 1.7.0 418 */ 419 public function toISO8601($local = false) 420 { 421 return $this->format(\DateTimeInterface::RFC3339, $local, false); 422 } 423 424 /** 425 * Gets the date as an SQL datetime string. 426 * 427 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. 428 * @param DatabaseDriver $db The database driver or null to use Factory::getDbo() 429 * 430 * @return string The date string in SQL datetime format. 431 * 432 * @link http://dev.mysql.com/doc/refman/5.0/en/datetime.html 433 * @since 2.5.0 434 */ 435 public function toSql($local = false, DatabaseDriver $db = null) 436 { 437 if ($db === null) { 438 $db = Factory::getDbo(); 439 } 440 441 return $this->format($db->getDateFormat(), $local, false); 442 } 443 444 /** 445 * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition 446 * can be found at the IETF Web site. 447 * 448 * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. 449 * 450 * @return string The date string in RFC 822 format. 451 * 452 * @link http://www.ietf.org/rfc/rfc2822.txt 453 * @since 1.7.0 454 */ 455 public function toRFC822($local = false) 456 { 457 return $this->format(\DateTimeInterface::RFC2822, $local, false); 458 } 459 460 /** 461 * Gets the date as UNIX time stamp. 462 * 463 * @return integer The date as a UNIX timestamp. 464 * 465 * @since 1.7.0 466 */ 467 public function toUnix() 468 { 469 return (int) parent::format('U'); 470 } 471 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |