getName()); // Set the timezone object for access later. $this->tz = $tz; } /** * Magic method to access properties of the date given by class to the format method. * * @param string $name The name of the property. * * @return mixed A value if the property name is valid, null otherwise. */ public function __get($name) { $value = null; switch ($name) { case 'daysinmonth': $value = $this->format('t', true); break; case 'dayofweek': $value = $this->format('N', true); break; case 'dayofyear': $value = $this->format('z', true); break; case 'isleapyear': $value = (boolean) $this->format('L', true); break; case 'day': $value = $this->format('d', true); break; case 'hour': $value = $this->format('H', true); break; case 'minute': $value = $this->format('i', true); break; case 'second': $value = $this->format('s', true); break; case 'month': $value = $this->format('m', true); break; case 'ordinal': $value = $this->format('S', true); break; case 'week': $value = $this->format('W', true); break; case 'year': $value = $this->format('Y', true); break; default: $trace = debug_backtrace(); trigger_error( 'Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_NOTICE ); } return $value; } /** * Magic method to render the date object in the format specified in the public * static member Date::$format. * * @return string The date as a formatted string. */ public function __toString() { return (string) parent::format(self::$format); } /** * Gets the date as a formatted string. * * @param string $format The date format specification string (see {@link PHP_MANUAL#date}) * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. * * @return string The date string in the specified format format. */ public function format($format, $local = false) { // If the returned time should not be local use GMT. if ($local == false) { parent::setTimezone(self::$gmt); } // Format the date. $return = parent::format($format); if ($local == false) { parent::setTimezone($this->tz); } return $return; } /** * Get the time offset from GMT in hours or seconds. * * @param boolean $hours True to return the value in hours. * * @return float The time offset from GMT either in hours or in seconds. */ public function getOffsetFromGMT($hours = false) { return (float) $hours ? ($this->tz->getOffset($this) / 3600) : $this->tz->getOffset($this); } /** * Method to wrap the setTimezone() function and set the internal time zone object. * * @param \DateTimeZone $tz The new DateTimeZone object. * * @return Date * * @note This method can't be type hinted due to a PHP bug: https://bugs.php.net/bug.php?id=61483 */ public function setTimezone($tz) { $this->tz = $tz; return parent::setTimezone($tz); } /** * Gets the date as an ISO 8601 string. IETF RFC 3339 defines the ISO 8601 format * and it can be found at the IETF Web site. * * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. * * @return string The date string in ISO 8601 format. * * @link http://www.ietf.org/rfc/rfc3339.txt */ public function toISO8601($local = false) { return $this->format(\DateTime::RFC3339, $local); } /** * Gets the date as an RFC 822 string. IETF RFC 2822 supercedes RFC 822 and its definition * can be found at the IETF Web site. * * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. * * @return string The date string in RFC 822 format. * * @link http://www.ietf.org/rfc/rfc2822.txt */ public function toRFC822($local = false) { return $this->format(\DateTime::RFC2822, $local); } /** * Gets the date as UNIX time stamp. * * @return integer The date as a UNIX timestamp. */ public function toUnix() { return (int) parent::format('U'); } /** * Gets the date as an SQL datetime string. * * @param boolean $local True to return the date string in the local time zone, false to return it in GMT. * @param Driver $db The database driver or null to use JFactory::getDbo() * * @return string The date string in SQL datetime format. * * @link http://dev.mysql.com/doc/refman/5.0/en/datetime.html */ public function toSql($local = false, Driver $db = null) { if ($db === null) { $db = Driver::getInstance(); } return $this->format($db->getDateFormat(), $local); } }