/Zend/Date.php
PHP | 4750 lines | 2844 code | 605 blank | 1301 comment | 530 complexity | e9a59c6c3e8a9bc5dbe01f49adef311c MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Date
- * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id: Date.php 11062 2008-08-25 18:19:05Z thomas $
- */
- /**
- * Include needed Date classes
- */
- require_once 'Zend/Date/DateObject.php';
- require_once 'Zend/Locale.php';
- require_once 'Zend/Locale/Format.php';
- require_once 'Zend/Locale/Math.php';
- /**
- * @category Zend
- * @package Zend_Date
- * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- class Zend_Date extends Zend_Date_DateObject
- {
- private $_Locale = null;
- // Fractional second variables
- private $_Fractional = 0;
- private $_Precision = 3;
- private static $_Options = array(
- 'format_type' => 'iso', // format for date strings 'iso' or 'php'
- 'fix_dst' => true, // fix dst on summer/winter time change
- 'extend_month' => false, // false - addMonth like SQL, true like excel
- 'cache' => null, // cache to set
- 'timesync' => null // timesync server to set
- );
- // Class wide Date Constants
- // day formats
- const DAY = 'DAY'; // d - 2 digit day of month, 01-31
- const DAY_SHORT = 'DAY_SHORT'; // j - 1,2 digit day of month, 1-31
- const DAY_SUFFIX = 'DAY_SUFFIX'; // S - english suffix day of month, st-th
- const DAY_OF_YEAR = 'DAY_OF_YEAR'; // z - Number of day of year
- const WEEKDAY = 'WEEKDAY'; // l - full day name - locale aware, Monday - Sunday
- const WEEKDAY_SHORT = 'WEEKDAY_SHORT'; // --- 3 letter day of week - locale aware, Mon-Sun
- const WEEKDAY_NARROW = 'WEEKDAY_NARROW'; // --- 1 letter day name - locale aware, M-S
- const WEEKDAY_NAME = 'WEEKDAY_NAME'; // D - abbreviated day name, 1-3 letters - locale aware, Mon-Sun
- const WEEKDAY_8601 = 'WEEKDAY_8601'; // N - digit weekday ISO 8601, 1-7 1 = monday, 7=sunday
- const WEEKDAY_DIGIT = 'WEEKDAY_DIGIT'; // w - weekday, 0-6 0=sunday, 6=saturday
- // week formats
- const WEEK = 'WEEK'; // W - number of week ISO8601, 1-53
- // month formats
- const MONTH = 'MONTH'; // m - 2 digit month, 01-12
- const MONTH_SHORT = 'MONTH_SHORT'; // n - 1 digit month, no leading zeros, 1-12
- const MONTH_DAYS = 'MONTH_DAYS'; // t - Number of days this month
- const MONTH_NAME = 'MONTH_NAME'; // F - full month name - locale aware, January-December
- const MONTH_NAME_SHORT = 'MONTH_NAME_SHORT'; // M - 3 letter monthname - locale aware, Jan-Dec
- const MONTH_NAME_NARROW = 'MONTH_NAME_NARROW'; // --- 1 letter month name - locale aware, J-D
- // year formats
- const YEAR = 'YEAR'; // Y - 4 digit year
- const YEAR_SHORT = 'YEAR_SHORT'; // y - 2 digit year, leading zeros 00-99
- const YEAR_8601 = 'YEAR_8601'; // o - number of year ISO8601
- const YEAR_SHORT_8601= 'YEAR_SHORT_8601';// --- 2 digit number of year ISO8601
- const LEAPYEAR = 'LEAPYEAR'; // L - is leapyear ?, 0-1
- // time formats
- const MERIDIEM = 'MERIDIEM'; // A,a - AM/PM - locale aware, AM/PM
- const SWATCH = 'SWATCH'; // B - Swatch Internet Time
- const HOUR = 'HOUR'; // H - 2 digit hour, leading zeros, 00-23
- const HOUR_SHORT = 'HOUR_SHORT'; // G - 1 digit hour, no leading zero, 0-23
- const HOUR_AM = 'HOUR_AM'; // h - 2 digit hour, leading zeros, 01-12 am/pm
- const HOUR_SHORT_AM = 'HOUR_SHORT_AM'; // g - 1 digit hour, no leading zero, 1-12 am/pm
- const MINUTE = 'MINUTE'; // i - 2 digit minute, leading zeros, 00-59
- const MINUTE_SHORT = 'MINUTE_SHORT'; // --- 1 digit minute, no leading zero, 0-59
- const SECOND = 'SECOND'; // s - 2 digit second, leading zeros, 00-59
- const SECOND_SHORT = 'SECOND_SHORT'; // --- 1 digit second, no leading zero, 0-59
- const MILLISECOND = 'MILLISECOND'; // --- milliseconds
- // timezone formats
- const TIMEZONE_NAME = 'TIMEZONE_NAME'; // e - timezone string
- const DAYLIGHT = 'DAYLIGHT'; // I - is Daylight saving time ?, 0-1
- const GMT_DIFF = 'GMT_DIFF'; // O - GMT difference, -1200 +1200
- const GMT_DIFF_SEP = 'GMT_DIFF_SEP'; // P - seperated GMT diff, -12:00 +12:00
- const TIMEZONE = 'TIMEZONE'; // T - timezone, EST, GMT, MDT
- const TIMEZONE_SECS = 'TIMEZONE_SECS'; // Z - timezone offset in seconds, -43200 +43200
- // date strings
- const ISO_8601 = 'ISO_8601'; // c - ISO 8601 date string
- const RFC_2822 = 'RFC_2822'; // r - RFC 2822 date string
- const TIMESTAMP = 'TIMESTAMP'; // U - unix timestamp
- // additional formats
- const ERA = 'ERA'; // --- short name of era, locale aware,
- const ERA_NAME = 'ERA_NAME'; // --- full name of era, locale aware,
- const DATES = 'DATES'; // --- standard date, locale aware
- const DATE_FULL = 'DATE_FULL'; // --- full date, locale aware
- const DATE_LONG = 'DATE_LONG'; // --- long date, locale aware
- const DATE_MEDIUM = 'DATE_MEDIUM'; // --- medium date, locale aware
- const DATE_SHORT = 'DATE_SHORT'; // --- short date, locale aware
- const TIMES = 'TIMES'; // --- standard time, locale aware
- const TIME_FULL = 'TIME_FULL'; // --- full time, locale aware
- const TIME_LONG = 'TIME_LONG'; // --- long time, locale aware
- const TIME_MEDIUM = 'TIME_MEDIUM'; // --- medium time, locale aware
- const TIME_SHORT = 'TIME_SHORT'; // --- short time, locale aware
- const ATOM = 'ATOM'; // --- DATE_ATOM
- const COOKIE = 'COOKIE'; // --- DATE_COOKIE
- const RFC_822 = 'RFC_822'; // --- DATE_RFC822
- const RFC_850 = 'RFC_850'; // --- DATE_RFC850
- const RFC_1036 = 'RFC_1036'; // --- DATE_RFC1036
- const RFC_1123 = 'RFC_1123'; // --- DATE_RFC1123
- const RFC_3339 = 'RFC_3339'; // --- DATE_RFC3339
- const RSS = 'RSS'; // --- DATE_RSS
- const W3C = 'W3C'; // --- DATE_W3C
- /**
- * Generates the standard date object, could be a unix timestamp, localized date,
- * string, integer, array and so on. Also parts of dates or time are supported
- * Always set the default timezone: http://php.net/date_default_timezone_set
- * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
- * For detailed instructions please look in the docu.
- *
- * @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
- * ,depending on $part. If null the actual time is set
- * @param string $part OPTIONAL Defines the input format of $date
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return Zend_Date
- * @throws Zend_Date_Exception
- */
- public function __construct($date = null, $part = null, $locale = null)
- {
- if (Zend_Locale::isLocale($date)) {
- $locale = $date;
- $date = null;
- $part = null;
- } else if (Zend_Locale::isLocale($part)) {
- $locale = $part;
- $part = null;
- }
- $this->setLocale($locale);
- if (is_string($date) && defined('self::' . $date)) {
- $part = $date;
- $date = null;
- }
- if (is_null($date)) {
- $date = self::now($locale);
- if (($part !== null) && ($part !== self::TIMESTAMP)) {
- $date = $date->get($part);
- }
- }
- if ($date instanceof Zend_TimeSync_Protocol) {
- $date = $date->getInfo();
- $date = $this->_getTime($date['offset']);
- $part = null;
- } else if (parent::$_defaultOffset != 0) {
- $date = $this->_getTime(parent::$_defaultOffset);
- }
- // set the timezone and offset for $this
- $zone = @date_default_timezone_get();
- $this->setTimezone($zone);
- // try to get timezone from date-string
- if (!is_int($date)) {
- $zone = $this->getTimezoneFromString($date);
- $this->setTimezone($zone);
- }
- // set datepart
- if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
- // switch off dst handling for value setting
- $this->setUnixTimestamp($this->getGmtOffset());
- $this->set($date, $part, $this->_Locale);
- // DST fix
- if ((is_array($date) === true) and (isset($date['hour']) === true)) {
- $hour = $this->toString('H');
- $hour = $date['hour'] - $hour;
- if ($hour !== 0) {
- $this->addTimestamp($hour * 3600);
- }
- }
- } else {
- $this->setUnixTimestamp($date);
- }
- }
- /**
- * Sets class wide options, if no option was given, the actual set options will be returned
- *
- * @param array $options Options to set
- * @throws Zend_Date_Exception
- * @return Options array if no option was given
- */
- public static function setOptions(array $options = array())
- {
- if (empty($options)) {
- return self::$_Options;
- }
- foreach ($options as $name => $value) {
- $name = strtolower($name);
- if (array_key_exists($name, self::$_Options)) {
- switch($name) {
- case 'format_type' :
- if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", $value);
- }
- break;
- case 'fix_dst' :
- if (!is_bool($value)) {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception("'fix_dst' has to be boolean", $value);
- }
- break;
- case 'extend_month' :
- if (!is_bool($value)) {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception("'extend_month' has to be boolean", $value);
- }
- break;
- case 'cache' :
- if (!$value instanceof Zend_Cache_Core) {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception("Instance of Zend_Cache expected");
- }
- parent::$_cache = $value;
- Zend_Locale_Data::setCache($value);
- break;
- case 'timesync' :
- if (!$value instanceof Zend_TimeSync_Protocol) {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
- }
- $date = $value->getInfo();
- parent::$_defaultOffset = $date['offset'];
- break;
- }
- self::$_Options[$name] = $value;
- }
- else {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception("Unknown option: $name = $value");
- }
- }
- }
- /**
- * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
- * If the timestamp is too large for integers, then the return value will be a string.
- * This function does not return the timestamp as an object.
- * Use clone() or copyPart() instead.
- *
- * @return integer|string UNIX timestamp
- */
- public function getTimestamp()
- {
- return $this->getUnixTimestamp();
- }
- /**
- * Returns the calculated timestamp
- * HINT: timestamps are always GMT
- *
- * @param string $calc Type of calculation to make
- * @param string|integer|array|Zend_Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
- * @return Zend_Date|integer
- * @throws Zend_Date_Exception
- */
- private function _timestamp($calc, $stamp)
- {
- if ($stamp instanceof Zend_Date) {
- // extract timestamp from object
- $stamp = $stamp->get(self::TIMESTAMP, true);
- }
- if (is_array($stamp)) {
- if (isset($stamp['timestamp']) === true) {
- $stamp = $stamp['timestamp'];
- } else {
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception('no timestamp given in array');
- }
- }
- if ($calc === 'set') {
- $return = $this->setUnixTimestamp($stamp);
- } else {
- $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
- }
- if ($calc != 'cmp') {
- return $this;
- }
- return $return;
- }
- /**
- * Sets a new timestamp
- *
- * @param integer|string|array|Zend_Date $timestamp Timestamp to set
- * @return Zend_Date
- * @throws Zend_Date_Exception
- */
- public function setTimestamp($timestamp)
- {
- return $this->_timestamp('set', $timestamp);
- }
- /**
- * Adds a timestamp
- *
- * @param integer|string|array|Zend_Date $timestamp Timestamp to add
- * @return Zend_Date
- * @throws Zend_Date_Exception
- */
- public function addTimestamp($timestamp)
- {
- return $this->_timestamp('add', $timestamp);
- }
- /**
- * Subtracts a timestamp
- *
- * @param integer|string|array|Zend_Date $timestamp Timestamp to sub
- * @return Zend_Date
- * @throws Zend_Date_Exception
- */
- public function subTimestamp($timestamp)
- {
- return $this->_timestamp('sub', $timestamp);
- }
- /**
- * Compares two timestamps, returning the difference as integer
- *
- * @param integer|string|array|Zend_Date $timestamp Timestamp to compare
- * @return integer 0 = equal, 1 = later, -1 = earlier
- * @throws Zend_Date_Exception
- */
- public function compareTimestamp($timestamp)
- {
- return $this->_timestamp('cmp', $timestamp);
- }
- /**
- * Returns a string representation of the object
- * Supported format tokens are:
- * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
- * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
- * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
- *
- * Additionally format tokens but non ISO conform are:
- * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
- * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
- * r - RFC2822 format, U - unix timestamp
- *
- * Not supported ISO tokens are
- * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
- * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
- * v - wall zone
- *
- * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
- * @param string $type OPTIONAL Type for the format string which overrides the standard setting
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return string
- */
- public function toString($format = null, $type = null, $locale = null)
- {
- if ((strlen($format) != 2) and (Zend_Locale::isLocale($format))) {
- $locale = $format;
- $format = null;
- }
- if (Zend_Locale::isLocale($type)) {
- $locale = $type;
- $type = null;
- }
- if ($locale === null) {
- $locale = $this->getLocale();
- }
- if ($format === null) {
- $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
- } else if (((self::$_Options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
- $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
- }
- // get format tokens
- $j = 0;
- $comment = false;
- $output = array();
- for($i = 0; $i < strlen($format); ++$i) {
- if ($format[$i] == "'") {
- if ($comment == false) {
- $comment = true;
- ++$j;
- $output[$j] = "'";
- } else if (isset($format[$i+1]) and ($format[$i+1] == "'")) {
- $output[$j] .= "'";
- ++$i;
- } else {
- $comment = false;
- }
- continue;
- }
- if (isset($output[$j]) and ($output[$j][0] == $format[$i]) or
- ($comment == true)) {
- $output[$j] .= $format[$i];
- } else {
- ++$j;
- $output[$j] = $format[$i];
- }
- }
- $notset = false;
- // fill format tokens with date information
- for($i = 1; $i <= count($output); ++$i) {
- // fill fixed tokens
- switch ($output[$i]) {
- // special formats
- case 'SS' :
- $output[$i] = $this->date('S', $this->getUnixTimestamp(), false);
- break;
- case 'eee' :
- $output[$i] = $this->date('N', $this->getUnixTimestamp(), false);
- break;
- case 'ddd' :
- $output[$i] = $this->date('t', $this->getUnixTimestamp(), false);
- break;
- case 'l' :
- $output[$i] = $this->date('L', $this->getUnixTimestamp(), false);
- break;
- case 'B' :
- $output[$i] = $this->date('B', $this->getUnixTimestamp(), false);
- break;
- case 'I' :
- $output[$i] = $this->date('I', $this->getUnixTimestamp(), false);
- break;
- case 'X' :
- $output[$i] = $this->date('Z', $this->getUnixTimestamp(), false);
- break;
- case 'r' :
- $output[$i] = $this->date('r', $this->getUnixTimestamp(), false);
- break;
- case 'U' :
- $output[$i] = $this->getUnixTimestamp();
- break;
- // eras
- case 'GGGGG' :
- $output[$i] = substr($this->get(self::ERA, $locale), 0, 1) . ".";
- break;
- case 'GGGG' :
- $output[$i] = $this->get(self::ERA_NAME, $locale);
- break;
- case 'GGG' :
- case 'GG' :
- case 'G' :
- $output[$i] = $this->get(self::ERA, $locale);
- break;
- // years
- case 'yy' :
- $output[$i] = str_pad($this->get(self::YEAR_SHORT, $locale), 2, '0', STR_PAD_LEFT);
- break;
- // ISO years
- case 'YY' :
- $output[$i] = str_pad($this->get(self::YEAR_SHORT_8601, $locale), 2, '0', STR_PAD_LEFT);
- break;
- // months
- case 'MMMMM' :
- $output[$i] = substr($this->get(self::MONTH_NAME_NARROW, $locale), 0, 1);
- break;
- case 'MMMM' :
- $output[$i] = $this->get(self::MONTH_NAME, $locale);
- break;
- case 'MMM' :
- $output[$i] = $this->get(self::MONTH_NAME_SHORT, $locale);
- break;
- case 'MM' :
- $output[$i] = $this->get(self::MONTH, $locale);
- break;
- case 'M' :
- $output[$i] = $this->get(self::MONTH_SHORT, $locale);
- break;
- // week
- case 'ww' :
- $output[$i] = str_pad($this->get(self::WEEK, $locale), 2, '0', STR_PAD_LEFT);
- break;
- case 'w' :
- $output[$i] = $this->get(self::WEEK, $locale);
- break;
- // monthday
- case 'dd' :
- $output[$i] = $this->get(self::DAY, $locale);
- break;
- case 'd' :
- $output[$i] = $this->get(self::DAY_SHORT, $locale);
- break;
- // yearday
- case 'DDD' :
- $output[$i] = str_pad($this->get(self::DAY_OF_YEAR, $locale), 3, '0', STR_PAD_LEFT);
- break;
- case 'DD' :
- $output[$i] = str_pad($this->get(self::DAY_OF_YEAR, $locale), 2, '0', STR_PAD_LEFT);
- break;
- case 'D' :
- $output[$i] = $this->get(self::DAY_OF_YEAR, $locale);
- break;
- // weekday
- case 'EEEEE' :
- $output[$i] = $this->get(self::WEEKDAY_NARROW, $locale);
- break;
- case 'EEEE' :
- $output[$i] = $this->get(self::WEEKDAY, $locale);
- break;
- case 'EEE' :
- $output[$i] = $this->get(self::WEEKDAY_SHORT, $locale);
- break;
- case 'EE' :
- $output[$i] = $this->get(self::WEEKDAY_NAME, $locale);
- break;
- case 'E' :
- $output[$i] = $this->get(self::WEEKDAY_NARROW, $locale);
- break;
- // weekday number
- case 'ee' :
- $output[$i] = str_pad($this->get(self::WEEKDAY_8601, $locale), 2, '0', STR_PAD_LEFT);
- break;
- case 'e' :
- $output[$i] = $this->get(self::WEEKDAY_8601, $locale);
- break;
- // period
- case 'a' :
- $output[$i] = $this->get(self::MERIDIEM, $locale);
- break;
- // hour
- case 'hh' :
- $output[$i] = $this->get(self::HOUR_AM, $locale);
- break;
- case 'h' :
- $output[$i] = $this->get(self::HOUR_SHORT_AM, $locale);
- break;
- case 'HH' :
- $output[$i] = $this->get(self::HOUR, $locale);
- break;
- case 'H' :
- $output[$i] = $this->get(self::HOUR_SHORT, $locale);
- break;
- // minute
- case 'mm' :
- $output[$i] = $this->get(self::MINUTE, $locale);
- break;
- case 'm' :
- $output[$i] = $this->get(self::MINUTE_SHORT, $locale);
- break;
- // second
- case 'ss' :
- $output[$i] = $this->get(self::SECOND, $locale);
- break;
- case 's' :
- $output[$i] = $this->get(self::SECOND_SHORT, $locale);
- break;
- case 'S' :
- $output[$i] = $this->get(self::MILLISECOND, $locale);
- break;
- // zone
- // @todo v needs to be reworked as it's the long wall time and not the timezone
- case 'vvvv' :
- case 'zzzz' :
- $output[$i] = $this->get(self::TIMEZONE_NAME, $locale);
- break;
- // @todo v needs to be reworked as it's the short wall time and not the timezone
- case 'v' :
- case 'zzz' :
- case 'zz' :
- case 'z' :
- $output[$i] = $this->get(self::TIMEZONE, $locale);
- break;
- // zone offset
- case 'ZZZZ' :
- $output[$i] = $this->get(self::GMT_DIFF_SEP, $locale);
- break;
- case 'ZZZ' :
- case 'ZZ' :
- case 'Z' :
- $output[$i] = $this->get(self::GMT_DIFF, $locale);
- break;
- default :
- $notset = true;
- break;
- }
- // fill variable tokens
- if ($notset == true) {
- if (($output[$i][0] !== "'") and (preg_match('/y+/', $output[$i]))) {
- $length = strlen($output[$i]);
- $output[$i] = $this->get(self::YEAR, $locale);
- $output[$i] = str_pad($output[$i], $length, '0', STR_PAD_LEFT);
- }
- if (($output[$i][0] !== "'") and (preg_match('/Y+/', $output[$i]))) {
- $length = strlen($output[$i]);
- $output[$i] = $this->get(self::YEAR_8601, $locale);
- $output[$i] = str_pad($output[$i], $length, '0', STR_PAD_LEFT);
- }
- if (($output[$i][0] !== "'") and (preg_match('/A+/', $output[$i]))) {
- $length = strlen($output[$i]);
- $seconds = $this->get(self::TIMESTAMP, $locale);
- $month = $this->get(self::MONTH_SHORT, $locale);
- $day = $this->get(self::DAY_SHORT, $locale);
- $year = $this->get(self::YEAR, $locale);
- $seconds -= $this->mktime(0, 0, 0, $month, $day, $year, false);
- $output[$i] = str_pad($seconds, $length, '0', STR_PAD_LEFT);
- }
- if ($output[$i][0] === "'") {
- $output[$i] = substr($output[$i], 1);
- }
- }
- $notset = false;
- }
- return implode('', $output);
- }
- /**
- * Returns a string representation of the date which is equal with the timestamp
- *
- * @return string
- */
- public function __toString()
- {
- return $this->toString(null, $this->_Locale);
- }
- /**
- * Returns a integer representation of the object
- * But returns false when the given part is no value f.e. Month-Name
- *
- * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
- * @return integer|false
- */
- public function toValue($part = null)
- {
- $result = $this->get($part);
- if (is_numeric($result)) {
- return intval("$result");
- } else {
- return false;
- }
- }
- /**
- * Returns an array representation of the object
- *
- * @return array
- */
- public function toArray()
- {
- return array('day' => $this->get(self::DAY_SHORT),
- 'month' => $this->get(self::MONTH_SHORT),
- 'year' => $this->get(self::YEAR),
- 'hour' => $this->get(self::HOUR_SHORT),
- 'minute' => $this->get(self::MINUTE_SHORT),
- 'second' => $this->get(self::SECOND_SHORT),
- 'timezone' => $this->get(self::TIMEZONE),
- 'timestamp' => $this->get(self::TIMESTAMP),
- 'weekday' => $this->get(self::WEEKDAY_DIGIT),
- 'dayofyear' => $this->get(self::DAY_OF_YEAR),
- 'week' => $this->get(self::WEEK),
- 'gmtsecs' => $this->get(self::TIMEZONE_SECS));
- }
- /**
- * Returns a representation of a date or datepart
- * This could be for example a localized monthname, the time without date,
- * the era or only the fractional seconds. There are about 50 different supported date parts.
- * For a complete list of supported datepart values look into the docu
- *
- * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return integer|string date or datepart
- */
- public function get($part = null, $locale = null)
- {
- if ($locale === null) {
- $locale = $this->getLocale();
- }
- if (Zend_Locale::isLocale($part)) {
- $locale = $part;
- $part = null;
- }
- if ($part === null) {
- $part = self::TIMESTAMP;
- }
- if (!defined("self::".$part)) {
- return $this->toString($part, $locale);
- }
- switch($part) {
- // day formats
- case self::DAY :
- return $this->date('d', $this->getUnixTimestamp(), false);
- break;
- case self::WEEKDAY_SHORT :
- $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
- $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
- return substr($day, 0, 3);
- break;
- case self::DAY_SHORT :
- return $this->date('j', $this->getUnixTimestamp(), false);
- break;
- case self::WEEKDAY :
- $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
- return Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
- break;
- case self::WEEKDAY_8601 :
- return $this->date('N', $this->getUnixTimestamp(), false);
- break;
- case self::DAY_SUFFIX :
- return $this->date('S', $this->getUnixTimestamp(), false);
- break;
- case self::WEEKDAY_DIGIT :
- return $this->date('w', $this->getUnixTimestamp(), false);
- break;
- case self::DAY_OF_YEAR :
- return $this->date('z', $this->getUnixTimestamp(), false);
- break;
- case self::WEEKDAY_NARROW :
- $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
- $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
- return substr($day, 0, 1);
- break;
- case self::WEEKDAY_NAME :
- $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
- return Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
- break;
- // week formats
- case self::WEEK :
- return $this->date('W', $this->getUnixTimestamp(), false);
- break;
- // month formats
- case self::MONTH_NAME :
- $month = $this->date('n', $this->getUnixTimestamp(), false);
- return Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month));
- break;
- case self::MONTH :
- return $this->date('m', $this->getUnixTimestamp(), false);
- break;
- case self::MONTH_NAME_SHORT :
- $month = $this->date('n', $this->getUnixTimestamp(), false);
- return Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
- break;
- case self::MONTH_SHORT :
- return $this->date('n', $this->getUnixTimestamp(), false);
- break;
- case self::MONTH_DAYS :
- return $this->date('t', $this->getUnixTimestamp(), false);
- break;
- case self::MONTH_NAME_NARROW :
- $month = $this->date('n', $this->getUnixTimestamp(), false);
- $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
- return substr($mon, 0, 1);
- break;
- // year formats
- case self::LEAPYEAR :
- return $this->date('L', $this->getUnixTimestamp(), false);
- break;
- case self::YEAR_8601 :
- return $this->date('o', $this->getUnixTimestamp(), false);
- break;
- case self::YEAR :
- return $this->date('Y', $this->getUnixTimestamp(), false);
- break;
- case self::YEAR_SHORT :
- return $this->date('y', $this->getUnixTimestamp(), false);
- break;
- case self::YEAR_SHORT_8601 :
- $year = $this->date('o', $this->getUnixTimestamp(), false);
- return substr($year, -2);
- break;
- // time formats
- case self::MERIDIEM :
- $am = $this->date('a', $this->getUnixTimestamp(), false);
- if ($am == 'am') {
- return Zend_Locale_Data::getContent($locale, 'am');
- }
- return Zend_Locale_Data::getContent($locale, 'pm');
- break;
- case self::SWATCH :
- return $this->date('B', $this->getUnixTimestamp(), false);
- break;
- case self::HOUR_SHORT_AM :
- return $this->date('g', $this->getUnixTimestamp(), false);
- break;
- case self::HOUR_SHORT :
- return $this->date('G', $this->getUnixTimestamp(), false);
- break;
- case self::HOUR_AM :
- return $this->date('h', $this->getUnixTimestamp(), false);
- break;
- case self::HOUR :
- return $this->date('H', $this->getUnixTimestamp(), false);
- break;
- case self::MINUTE :
- return $this->date('i', $this->getUnixTimestamp(), false);
- break;
- case self::SECOND :
- return $this->date('s', $this->getUnixTimestamp(), false);
- break;
- case self::MINUTE_SHORT :
- return $this->date('i', $this->getUnixTimestamp(), false);
- break;
- case self::SECOND_SHORT :
- return $this->date('s', $this->getUnixTimestamp(), false);
- break;
- case self::MILLISECOND :
- return $this->_Fractional;
- break;
- // timezone formats
- case self::TIMEZONE_NAME :
- return $this->date('e', $this->getUnixTimestamp(), false);
- break;
- case self::DAYLIGHT :
- return $this->date('I', $this->getUnixTimestamp(), false);
- break;
- case self::GMT_DIFF :
- return $this->date('O', $this->getUnixTimestamp(), false);
- break;
- case self::GMT_DIFF_SEP :
- return $this->date('P', $this->getUnixTimestamp(), false);
- break;
- case self::TIMEZONE :
- return $this->date('T', $this->getUnixTimestamp(), false);
- break;
- case self::TIMEZONE_SECS :
- return $this->date('Z', $this->getUnixTimestamp(), false);
- break;
- // date strings
- case self::ISO_8601 :
- return $this->date('c', $this->getUnixTimestamp(), false);
- break;
- case self::RFC_2822 :
- return $this->date('r', $this->getUnixTimestamp(), false);
- break;
- case self::TIMESTAMP :
- return $this->getUnixTimestamp();
- break;
- // additional formats
- case self::ERA :
- $year = $this->date('Y', $this->getUnixTimestamp(), false);
- if ($year < 0) {
- return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0'));
- }
- return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1'));
- break;
- case self::ERA_NAME :
- $year = $this->date('Y', $this->getUnixTimestamp(), false);
- if ($year < 0) {
- return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0'));
- }
- return Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1'));
- break;
- case self::DATES :
- return $this->toString(Zend_Locale_Format::getDateFormat($locale), 'iso', $locale);
- break;
- case self::DATE_FULL :
- $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
- return $this->toString($date, 'iso', $locale);
- break;
- case self::DATE_LONG :
- $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
- return $this->toString($date, 'iso', $locale);
- break;
- case self::DATE_MEDIUM :
- $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
- return $this->toString($date, 'iso', $locale);
- break;
- case self::DATE_SHORT :
- $date = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
- return $this->toString($date, 'iso', $locale);
- break;
- case self::TIMES :
- return $this->toString(Zend_Locale_Format::getTimeFormat($locale), 'iso', $locale);
- break;
- case self::TIME_FULL :
- $time = Zend_Locale_Data::getContent($locale, 'time', 'full');
- return $this->toString($time, 'iso', $locale);
- break;
- case self::TIME_LONG :
- $time = Zend_Locale_Data::getContent($locale, 'time', 'long');
- return $this->toString($time, 'iso', $locale);
- break;
- case self::TIME_MEDIUM :
- $time = Zend_Locale_Data::getContent($locale, 'time', 'medium');
- return $this->toString($time, 'iso', $locale);
- break;
- case self::TIME_SHORT :
- $time = Zend_Locale_Data::getContent($locale, 'time', 'short');
- return $this->toString($time, 'iso', $locale);
- break;
- case self::ATOM :
- return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
- break;
- case self::COOKIE :
- return $this->date('l\, d\-M\-y H\:i\:s e', $this->getUnixTimestamp(), false);
- break;
- case self::RFC_822 :
- return $this->date('D\, d M y H\:i\:s O', $this->getUnixTimestamp(), false);
- break;
- case self::RFC_850 :
- return $this->date('l\, d\-M\-y H\:i\:s e', $this->getUnixTimestamp(), false);
- break;
- case self::RFC_1036 :
- return $this->date('D\, d M y H\:i\:s O', $this->getUnixTimestamp(), false);
- break;
- case self::RFC_1123 :
- return $this->date('D\, d M Y H\:i\:s O', $this->getUnixTimestamp(), false);
- break;
- case self::RFC_3339 :
- return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
- break;
- case self::RSS :
- return $this->date('D\, d M Y H\:i\:s O', $this->getUnixTimestamp(), false);
- break;
- case self::W3C :
- return $this->date('Y\-m\-d\TH\:i\:sP', $this->getUnixTimestamp(), false);
- break;
- }
- }
- /**
- * Return digit from standard names (english)
- * Faster implementation than locale aware searching
- *
- * @param string $name
- * @return integer Number of this month
- * @throws Zend_Date_Exception
- */
- private function getDigitFromName($name)
- {
- switch($name) {
- case "Jan":
- return 1;
- case "Feb":
- return 2;
- case "Mar":
- return 3;
- case "Apr":
- return 4;
- case "May":
- return 5;
- case "Jun":
- return 6;
- case "Jul":
- return 7;
- case "Aug":
- return 8;
- case "Sep":
- return 9;
- case "Oct":
- return 10;
- case "Nov":
- return 11;
- case "Dec":
- return 12;
- default:
- require_once 'Zend/Date/Exception.php';
- throw new Zend_Date_Exception('Month ($name) is not a known month');
- }
- }
- /**
- * Counts the exact year number
- * < 70 - 2000 added, >70 < 100 - 1900, others just returned
- *
- * @param integer $value year number
- * @return integer Number of year
- */
- private static function _century($value)
- {
- if ($value >= 0) {
- if ($value < 70) {
- $value += 2000;
- } else if ($value < 100) {
- $value += 1900;
- }
- }
- return $value;
- }
- /**
- * Sets the given date as new date or a given datepart as new datepart returning the new datepart
- * This could be for example a localized dayname, the date without time,
- * the month or only the seconds. There are about 50 different supported date parts.
- * For a complete list of supported datepart values look into the docu
- *
- * @param string|integer|array|Zend_Date $date Date or datepart to set
- * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return integer|string new datepart
- * @throws Zend_Date_Exception
- */
- public function set($date, $part = null, $locale = null)
- {
- $zone = $this->getTimezoneFromString($date);
- $this->setTimezone($zone);
- $result = $this->_calculate('set', $date, $part, $locale);
- return $result;
- }
- /**
- * Adds a date or datepart to the existing date, by extracting $part from $date,
- * and modifying this object by adding that part. The $part is then extracted from
- * this object and returned as an integer or numeric string (for large values, or $part's
- * corresponding to pre-defined formatted date strings).
- * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
- * There are about 50 different supported date parts.
- * For a complete list of supported datepart values look into the docu.
- *
- * @param string|integer|array|Zend_Date $date Date or datepart to add
- * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return integer|string new datepart
- * @throws Zend_Date_Exception
- */
- public function add($date, $part = null, $locale = null)
- {
- $this->_calculate('add', $date, $part, $locale);
- $result = $this->get($part, $locale);
- return $result;
- }
- /**
- * Subtracts a date from another date.
- * This could be for example a RFC2822 date, the time,
- * the year or only the timestamp. There are about 50 different supported date parts.
- * For a complete list of supported datepart values look into the docu
- * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
- *
- * @param string|integer|array|Zend_Date $date Date or datepart to subtract
- * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return integer|string new datepart
- * @throws Zend_Date_Exception
- */
- public function sub($date, $part = null, $locale = null)
- {
- $this->_calculate('sub', $date, $part, $locale);
- $result = $this->get($part, $locale);
- return $result;
- }
- /**
- * Compares a date or datepart with the existing one.
- * Returns -1 if earlier, 0 if equal and 1 if later.
- *
- * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
- * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
- * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
- * @return integer 0 = equal, 1 = later, -1 = earlier
- * @throws Zend_Date_Exception
- */
- public function compare($date, $part = null, $locale = null)
- {
- $compare = $this->_calculate('cmp', $date, $part, $locale);
- if ($compare > 0) {
- return 1;
- } else if ($compare < 0) {
- return -1;
- }
- return 0;
- }
- /**
- * Returns a new instance of Zend_Date with the selected part copied.
- * To make an exact copy, use PHP's clone keyword.
- * For a complete list of supported date part values look into the docu.
- * If a date part is copied, all other date parts are set to standard values.
- * For example: If only YEAR is copied, the returned date object is equal to
- * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
- * If only HOUR is copied, the returned date object is equal to
- * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
- *
- * @param string $part Part of the date to compare, if null the timestamp is subtracted
- * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
- * @return Zend_Date
- */
- public function copyPart($part, $locale = null)
- {
- $clone = clone $this; // copy all instance variables
- $clone->setUnixTimestamp(0); // except the timestamp
- if ($locale != null) {
- $clone->setLocale($locale); // set an other locale if selected
- }
- $clone->set($this, $part);
- return $clone;
- }
- /**
- * Internal function, returns the offset of a given timezone
- *
- * @param string $zone
- * @return integer
- */
- public function getTimezoneFromString($zone)
- {
- if (is_array($zone)) {
- return $this->getTimezone();
- }
- if ($zone instanceof Zend_Date) {
- return $zone->getTimezone();
- }
- $match = array();
- preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
- if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
- $zone = "Etc/GMT";
- $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
- $zone .= (int) abs($match[count($match) - 1]);
- return $zone;
- }
- preg_match('/(\w{3,30})/', $zone, $match);
- try {
- if (!empty($match)) {
- $oldzone = $this->getTimezone();
- $result = $this->setTimezone($match[count($match) - 1]);
- $this->setTimezone($oldzone);
- if ($result !== $oldzone) {
- return $match[count($match) - 1];
- }
- }
- } catch (Exception $e) {
- // fall through
- }
- return $this->getTimezone();
- }
- /**
- * Calculates the date or object
- *
- * @param string $calc Calculation to make
- * @param string|integer $date Date for calculation
- * @param string|integer $comp Second date for calculation
- * @param boolean|integer $dst Use dst correction if option is set
- * @return integer|string|Zend_Date new ti…
Large files files are truncated, but you can click here to view the full file