10 defined(
'JPATH_PLATFORM') or die;
35 const DAY_ABBR =
"\x021\x03";
36 const DAY_NAME =
"\x022\x03";
37 const MONTH_ABBR =
"\x023\x03";
38 const MONTH_NAME =
"\x024\x03";
46 public static $format =
'Y-m-d H:i:s';
81 public function __construct($date =
'now', $tz = null)
84 if (empty(self::$gmt) || empty(self::$stz))
86 self::$gmt =
new DateTimeZone(
'GMT');
87 self::$stz =
new DateTimeZone(@date_default_timezone_get());
91 if (!($tz instanceof DateTimeZone))
97 elseif (is_string($tz))
99 $tz =
new DateTimeZone($tz);
104 date_default_timezone_set(
'UTC');
105 $date = is_numeric($date) ? date(
'c', $date) : $date;
108 parent::__construct($date, $tz);
111 date_default_timezone_set(self::$stz->getName());
126 public function __get($name)
133 $value = $this->format(
't',
true);
137 $value = $this->format(
'N',
true);
141 $value = $this->format(
'z',
true);
145 $value = (boolean) $this->format(
'L',
true);
149 $value = $this->format(
'd',
true);
153 $value = $this->format(
'H',
true);
157 $value = $this->format(
'i',
true);
161 $value = $this->format(
's',
true);
165 $value = $this->format(
'm',
true);
169 $value = $this->format(
'S',
true);
173 $value = $this->format(
'W',
true);
177 $value = $this->format(
'Y',
true);
181 $trace = debug_backtrace();
183 'Undefined property via __get(): ' . $name .
' in ' . $trace[0][
'file'] .
' on line ' . $trace[0][
'line'],
199 public function __toString()
201 return (
string) parent::format(self::$format);
214 public static function getInstance($date =
'now', $tz = null)
216 return new JDate($date, $tz);
229 public function dayToString($day, $abbr =
false)
261 public function calendar($format, $local =
false, $translate =
true)
263 return $this->format($format, $local, $translate);
277 public function format($format, $local =
false, $translate =
true)
282 $format = preg_replace(
'/(^|[^\\\])D/',
"\\1" . self::DAY_ABBR, $format);
283 $format = preg_replace(
'/(^|[^\\\])l/',
"\\1" . self::DAY_NAME, $format);
284 $format = preg_replace(
'/(^|[^\\\])M/',
"\\1" . self::MONTH_ABBR, $format);
285 $format = preg_replace(
'/(^|[^\\\])F/',
"\\1" . self::MONTH_NAME, $format);
291 parent::setTimezone(self::$gmt);
295 $return = parent::format($format);
300 if (strpos($return, self::DAY_ABBR) !==
false)
302 $return = str_replace(self::DAY_ABBR, $this->dayToString(parent::format(
'w'),
true), $return);
305 if (strpos($return, self::DAY_NAME) !==
false)
307 $return = str_replace(self::DAY_NAME, $this->dayToString(parent::format(
'w')), $return);
310 if (strpos($return, self::MONTH_ABBR) !==
false)
312 $return = str_replace(self::MONTH_ABBR, $this->monthToString(parent::format(
'n'),
true), $return);
315 if (strpos($return, self::MONTH_NAME) !==
false)
317 $return = str_replace(self::MONTH_NAME, $this->monthToString(parent::format(
'n')), $return);
323 parent::setTimezone($this->tz);
338 public function getOffsetFromGMT($hours =
false)
340 return (
float) $hours ? ($this->tz->getOffset($this) / 3600) : $this->tz->getOffset($this);
353 public function monthToString($month, $abbr =
false)
394 public function setTimezone($tz)
398 return parent::setTimezone($tz);
412 public function toISO8601($local =
false)
414 return $this->format(DateTime::RFC3339, $local,
false);
435 return $this->format($db->getDateFormat(), $local,
false);
449 public function toRFC822($local =
false)
451 return $this->format(DateTime::RFC2822, $local,
false);
461 public function toUnix()
463 return (
int) parent::format(
'U');