7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Include needed Date classes
25 require_once 'Zend/Date/DateObject.php';
26 require_once 'Zend/Locale.php';
27 require_once 'Zend/Locale/Format.php';
28 require_once 'Zend/Locale/Math.php';
33 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license http://framework.zend.com/license/new-bsd New BSD License
36 class Zend_Date
extends Zend_Date_DateObject
38 private $_locale = null;
40 // Fractional second variables
41 private $_fractional = 0;
42 private $_precision = 3;
44 private static $_options = array(
45 'format_type' => 'iso', // format for date strings 'iso' or 'php'
46 'fix_dst' => true, // fix dst on summer/winter time change
47 'extend_month' => false, // false - addMonth like SQL, true like excel
48 'cache' => null, // cache to set
49 'timesync' => null // timesync server to set
52 // Class wide Date Constants
54 const DAY_SHORT
= 'd';
55 const DAY_SUFFIX
= 'SS';
56 const DAY_OF_YEAR
= 'D';
57 const WEEKDAY
= 'EEEE';
58 const WEEKDAY_SHORT
= 'EEE';
59 const WEEKDAY_NARROW
= 'E';
60 const WEEKDAY_NAME
= 'EE';
61 const WEEKDAY_8601
= 'eee';
62 const WEEKDAY_DIGIT
= 'e';
65 const MONTH_SHORT
= 'M';
66 const MONTH_DAYS
= 'ddd';
67 const MONTH_NAME
= 'MMMM';
68 const MONTH_NAME_SHORT
= 'MMM';
69 const MONTH_NAME_NARROW
= 'MMMMM';
71 const YEAR_SHORT
= 'yy';
72 const YEAR_8601
= 'Y';
73 const YEAR_SHORT_8601
= 'YY';
78 const HOUR_SHORT
= 'H';
80 const HOUR_SHORT_AM
= 'h';
82 const MINUTE_SHORT
= 'm';
84 const SECOND_SHORT
= 's';
85 const MILLISECOND
= 'S';
86 const TIMEZONE_NAME
= 'zzzz';
89 const GMT_DIFF_SEP
= 'ZZZZ';
91 const TIMEZONE_SECS
= 'X';
94 const TIMESTAMP
= 'U';
96 const ERA_NAME
= 'GGGG';
97 const ERA_NARROW
= 'GGGGG';
99 const DATE_FULL
= 'FFFFF';
100 const DATE_LONG
= 'FFFF';
101 const DATE_MEDIUM
= 'FFF';
102 const DATE_SHORT
= 'FF';
104 const TIME_FULL
= 'TTTTT';
105 const TIME_LONG
= 'TTTT';
106 const TIME_MEDIUM
= 'TTT';
107 const TIME_SHORT
= 'TT';
108 const DATETIME
= 'K';
109 const DATETIME_FULL
= 'KKKKK';
110 const DATETIME_LONG
= 'KKKK';
111 const DATETIME_MEDIUM
= 'KKK';
112 const DATETIME_SHORT
= 'KK';
114 const COOKIE
= 'CCC';
116 const RFC_850
= 'RR';
117 const RFC_1036
= 'RRR';
118 const RFC_1123
= 'RRRR';
119 const RFC_3339
= 'RRRRR';
124 * Generates the standard date object, could be a unix timestamp, localized date,
125 * string, integer, array and so on. Also parts of dates or time are supported
126 * Always set the default timezone: http://php.net/date_default_timezone_set
127 * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
128 * For detailed instructions please look in the docu.
130 * @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
131 * ,depending on $part. If null the actual time is set
132 * @param string $part OPTIONAL Defines the input format of $date
133 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
135 * @throws Zend_Date_Exception
137 public function __construct($date = null, $part = null, $locale = null)
139 if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol
) and
140 !($date instanceof Zend_Date
)) {
141 if ($locale instanceof Zend_Locale
) {
146 $date = (string) $date;
150 if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol
) and
151 !($date instanceof Zend_Date
) and !defined($date) and Zend_Locale
::isLocale($date, true, false)) {
155 } else if (($part !== null) and !defined($part) and Zend_Locale
::isLocale($part, true, false)) {
160 $this->setLocale($locale);
161 if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
166 if ($date === null) {
167 if ($part === null) {
169 } else if ($part !== self
::TIMESTAMP
) {
170 $date = self
::now($locale);
171 $date = $date->get($part);
175 if ($date instanceof Zend_TimeSync_Protocol
) {
176 $date = $date->getInfo();
177 $date = $this->_getTime($date['offset']);
179 } else if (parent
::$_defaultOffset != 0) {
180 $date = $this->_getTime(parent
::$_defaultOffset);
183 // set the timezone and offset for $this
184 $zone = @date_default_timezone_get
();
185 $this->setTimezone($zone);
187 // try to get timezone from date-string
188 if (!is_int($date)) {
189 $zone = $this->getTimezoneFromString($date);
190 $this->setTimezone($zone);
194 if (($part !== null && $part !== self
::TIMESTAMP
) or (!is_numeric($date))) {
195 // switch off dst handling for value setting
196 $this->setUnixTimestamp($this->getGmtOffset());
197 $this->set($date, $part, $this->_locale
);
200 if (is_array($date) === true) {
201 if (!isset($date['hour'])) {
205 $hour = $this->toString('H', 'iso', true);
206 $hour = $date['hour'] - $hour;
210 $this->addTimestamp(3600);
214 $this->subTimestamp(3600);
218 $this->addTimestamp(7200);
222 $this->subTimestamp(7200);
227 $this->setUnixTimestamp($date);
232 * Sets class wide options, if no option was given, the actual set options will be returned
234 * @param array $options Options to set
235 * @throws Zend_Date_Exception
236 * @return Options array if no option was given
238 public static function setOptions(array $options = array())
240 if (empty($options)) {
241 return self
::$_options;
244 foreach ($options as $name => $value) {
245 $name = strtolower($name);
247 if (array_key_exists($name, self
::$_options)) {
250 if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
251 require_once 'Zend/Date/Exception.php';
252 throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", 0, null, $value);
256 if (!is_bool($value)) {
257 require_once 'Zend/Date/Exception.php';
258 throw new Zend_Date_Exception("'fix_dst' has to be boolean", 0, null, $value);
261 case 'extend_month' :
262 if (!is_bool($value)) {
263 require_once 'Zend/Date/Exception.php';
264 throw new Zend_Date_Exception("'extend_month' has to be boolean", 0, null, $value);
268 if ($value === null) {
269 parent
::$_cache = null;
271 if (!$value instanceof Zend_Cache_Core
) {
272 require_once 'Zend/Date/Exception.php';
273 throw new Zend_Date_Exception("Instance of Zend_Cache expected");
276 parent
::$_cache = $value;
277 Zend_Locale_Data
::setCache($value);
281 if ($value === null) {
282 parent
::$_defaultOffset = 0;
284 if (!$value instanceof Zend_TimeSync_Protocol
) {
285 require_once 'Zend/Date/Exception.php';
286 throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
289 $date = $value->getInfo();
290 parent
::$_defaultOffset = $date['offset'];
294 self
::$_options[$name] = $value;
297 require_once 'Zend/Date/Exception.php';
298 throw new Zend_Date_Exception("Unknown option: $name = $value");
304 * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
305 * If the timestamp is too large for integers, then the return value will be a string.
306 * This function does not return the timestamp as an object.
307 * Use clone() or copyPart() instead.
309 * @return integer|string UNIX timestamp
311 public function getTimestamp()
313 return $this->getUnixTimestamp();
317 * Returns the calculated timestamp
318 * HINT: timestamps are always GMT
320 * @param string $calc Type of calculation to make
321 * @param string|integer|array|Zend_Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
322 * @return Zend_Date|integer
323 * @throws Zend_Date_Exception
325 private function _timestamp($calc, $stamp)
327 if ($stamp instanceof Zend_Date
) {
328 // extract timestamp from object
329 $stamp = $stamp->getTimestamp();
332 if (is_array($stamp)) {
333 if (isset($stamp['timestamp']) === true) {
334 $stamp = $stamp['timestamp'];
336 require_once 'Zend/Date/Exception.php';
337 throw new Zend_Date_Exception('no timestamp given in array');
341 if ($calc === 'set') {
342 $return = $this->setUnixTimestamp($stamp);
344 $return = $this->_calcdetail($calc, $stamp, self
::TIMESTAMP
, null);
346 if ($calc != 'cmp') {
353 * Sets a new timestamp
355 * @param integer|string|array|Zend_Date $timestamp Timestamp to set
356 * @return Zend_Date Provides fluid interface
357 * @throws Zend_Date_Exception
359 public function setTimestamp($timestamp)
361 return $this->_timestamp('set', $timestamp);
367 * @param integer|string|array|Zend_Date $timestamp Timestamp to add
368 * @return Zend_Date Provides fluid interface
369 * @throws Zend_Date_Exception
371 public function addTimestamp($timestamp)
373 return $this->_timestamp('add', $timestamp);
377 * Subtracts a timestamp
379 * @param integer|string|array|Zend_Date $timestamp Timestamp to sub
380 * @return Zend_Date Provides fluid interface
381 * @throws Zend_Date_Exception
383 public function subTimestamp($timestamp)
385 return $this->_timestamp('sub', $timestamp);
389 * Compares two timestamps, returning the difference as integer
391 * @param integer|string|array|Zend_Date $timestamp Timestamp to compare
392 * @return integer 0 = equal, 1 = later, -1 = earlier
393 * @throws Zend_Date_Exception
395 public function compareTimestamp($timestamp)
397 return $this->_timestamp('cmp', $timestamp);
401 * Returns a string representation of the object
402 * Supported format tokens are:
403 * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
404 * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
405 * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
407 * Additionally format tokens but non ISO conform are:
408 * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
409 * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
410 * r - RFC2822 format, U - unix timestamp
412 * Not supported ISO tokens are
413 * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
414 * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
417 * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
418 * @param string $type OPTIONAL Type for the format string which overrides the standard setting
419 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
422 public function toString($format = null, $type = null, $locale = null)
424 if (is_object($format)) {
425 if ($format instanceof Zend_Locale
) {
429 $format = (string) $format;
433 if (is_object($type)) {
434 if ($type instanceof Zend_Locale
) {
438 $type = (string) $type;
442 if (($format !== null) && !defined($format)
443 && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
444 && Zend_Locale
::isLocale($format, null, false)) {
449 if (($type !== null) and ($type != 'php') and ($type != 'iso') and
450 Zend_Locale
::isLocale($type, null, false)) {
455 if ($locale === null) {
456 $locale = $this->getLocale();
459 if ($format === null) {
460 $format = Zend_Locale_Format
::getDateFormat($locale) . ' ' . Zend_Locale_Format
::getTimeFormat($locale);
461 } else if (((self
::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
462 $format = Zend_Locale_Format
::convertPhpToIsoFormat($format);
465 return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
469 * Returns a string representation of the date which is equal with the timestamp
473 public function __toString()
475 return $this->toString(null, $this->_locale
);
479 * Returns a integer representation of the object
480 * But returns false when the given part is no value f.e. Month-Name
482 * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
483 * @return integer|false
485 public function toValue($part = null)
487 $result = $this->get($part);
488 if (is_numeric($result)) {
489 return intval("$result");
496 * Returns an array representation of the object
500 public function toArray()
502 return array('day' => $this->toString(self
::DAY_SHORT
, 'iso'),
503 'month' => $this->toString(self
::MONTH_SHORT
, 'iso'),
504 'year' => $this->toString(self
::YEAR
, 'iso'),
505 'hour' => $this->toString(self
::HOUR_SHORT
, 'iso'),
506 'minute' => $this->toString(self
::MINUTE_SHORT
, 'iso'),
507 'second' => $this->toString(self
::SECOND_SHORT
, 'iso'),
508 'timezone' => $this->toString(self
::TIMEZONE
, 'iso'),
509 'timestamp' => $this->toString(self
::TIMESTAMP
, 'iso'),
510 'weekday' => $this->toString(self
::WEEKDAY_8601
, 'iso'),
511 'dayofyear' => $this->toString(self
::DAY_OF_YEAR
, 'iso'),
512 'week' => $this->toString(self
::WEEK
, 'iso'),
513 'gmtsecs' => $this->toString(self
::TIMEZONE_SECS
, 'iso'));
517 * Returns a representation of a date or datepart
518 * This could be for example a localized monthname, the time without date,
519 * the era or only the fractional seconds. There are about 50 different supported date parts.
520 * For a complete list of supported datepart values look into the docu
522 * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
523 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
524 * @return string date or datepart
526 public function get($part = null, $locale = null)
528 if ($locale === null) {
529 $locale = $this->getLocale();
532 if (($part !== null) && !defined($part)
533 && ($part != 'ee') && ($part != 'ss') && ($part != 'GG') && ($part != 'MM') && ($part != 'EE') && ($part != 'TT')
534 && Zend_Locale
::isLocale($part, null, false)) {
539 if ($part === null) {
540 $part = self
::TIMESTAMP
;
541 } else if (self
::$_options['format_type'] == 'php') {
542 $part = Zend_Locale_Format
::convertPhpToIsoFormat($part);
545 return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
549 * Internal method to apply tokens
551 * @param string $part
552 * @param string $locale
555 private function _toToken($part, $locale) {
560 for ($i = 0; $i < strlen($part); ++
$i) {
561 if ($part[$i] == "'") {
562 $comment = $comment ?
false : true;
563 if (isset($part[$i+
1]) && ($part[$i+
1] == "'")) {
564 $comment = $comment ?
false : true;
574 $format .= '\\' . $part[$i];
578 if (!isset($part[$i+
1]) ||
(isset($orig[0]) && ($orig[0] != $part[$i+
1]))) {
579 $format .= $this->_parseIsoToDate($orig, $locale);
589 * Internal parsing method
591 * @param string $token
592 * @param string $locale
595 private function _parseIsoToDate($token, $locale) {
601 case self
::WEEKDAY_SHORT
:
602 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
603 $day = Zend_Locale_Data
::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
604 return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
607 case self
::DAY_SHORT
:
612 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
613 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
616 case self
::WEEKDAY_8601
:
621 return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT
));
624 case self
::DAY_SUFFIX
:
628 case self
::WEEKDAY_DIGIT
:
632 case self
::DAY_OF_YEAR
:
637 return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT
));
641 return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT
));
644 case self
::WEEKDAY_NARROW
:
646 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
647 $day = Zend_Locale_Data
::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
648 return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
651 case self
::WEEKDAY_NAME
:
652 $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
653 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
657 $week = $this->date('W', $this->getUnixTimestamp(), false);
658 return $this->_toComment(($week[0] == '0') ?
$week[1] : $week);
665 case self
::MONTH_NAME
:
666 $month = $this->date('n', $this->getUnixTimestamp(), false);
667 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
674 case self
::MONTH_NAME_SHORT
:
675 $month = $this->date('n', $this->getUnixTimestamp(), false);
676 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
679 case self
::MONTH_SHORT
:
683 case self
::MONTH_DAYS
:
687 case self
::MONTH_NAME_NARROW
:
688 $month = $this->date('n', $this->getUnixTimestamp(), false);
689 $mon = Zend_Locale_Data
::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
690 return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
693 case self
::LEAPYEAR
:
697 case self
::YEAR_8601
:
705 case self
::YEAR_SHORT
:
709 case self
::YEAR_SHORT_8601
:
710 return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
713 case self
::MERIDIEM
:
714 $am = $this->date('a', $this->getUnixTimestamp(), false);
716 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'am'));
719 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'pm'));
726 case self
::HOUR_SHORT_AM
:
730 case self
::HOUR_SHORT
:
743 return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT
));
747 return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT
));
750 case self
::MINUTE_SHORT
:
754 case self
::SECOND_SHORT
:
758 case self
::MILLISECOND
:
759 return $this->_toComment($this->getMilliSecond());
762 case self
::TIMEZONE_NAME
:
767 case self
::DAYLIGHT
:
771 case self
::GMT_DIFF
:
777 case self
::GMT_DIFF_SEP
:
781 case self
::TIMEZONE
:
788 case self
::TIMEZONE_SECS
:
792 case self
::ISO_8601
:
796 case self
::RFC_2822
:
800 case self
::TIMESTAMP
:
807 $year = $this->date('Y', $this->getUnixTimestamp(), false);
809 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
812 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
815 case self
::ERA_NARROW
:
816 $year = $this->date('Y', $this->getUnixTimestamp(), false);
818 return $this->_toComment(iconv_substr(Zend_Locale_Data
::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
821 return $this->_toComment(iconv_substr(Zend_Locale_Data
::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
824 case self
::ERA_NAME
:
825 $year = $this->date('Y', $this->getUnixTimestamp(), false);
827 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'era', array('gregorian', 'Names', '0')));
830 return $this->_toComment(Zend_Locale_Data
::getContent($locale, 'era', array('gregorian', 'Names', '1')));
834 return $this->_toToken(Zend_Locale_Format
::getDateFormat($locale), $locale);
837 case self
::DATE_FULL
:
838 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'full')), $locale);
841 case self
::DATE_LONG
:
842 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'long')), $locale);
845 case self
::DATE_MEDIUM
:
846 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
849 case self
::DATE_SHORT
:
850 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'short')), $locale);
854 return $this->_toToken(Zend_Locale_Format
::getTimeFormat($locale), $locale);
857 case self
::TIME_FULL
:
858 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'time', 'full'), $locale);
861 case self
::TIME_LONG
:
862 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'time', 'long'), $locale);
865 case self
::TIME_MEDIUM
:
866 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'time', 'medium'), $locale);
869 case self
::TIME_SHORT
:
870 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'time', 'short'), $locale);
873 case self
::DATETIME
:
874 return $this->_toToken(Zend_Locale_Format
::getDateTimeFormat($locale), $locale);
877 case self
::DATETIME_FULL
:
878 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
881 case self
::DATETIME_LONG
:
882 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
885 case self
::DATETIME_MEDIUM
:
886 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
889 case self
::DATETIME_SHORT
:
890 return $this->_toToken(Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
894 return 'Y\-m\-d\TH\:i\:sP';
898 return 'l\, d\-M\-y H\:i\:s e';
902 return 'D\, d M y H\:i\:s O';
906 return 'l\, d\-M\-y H\:i\:s e';
909 case self
::RFC_1036
:
910 return 'D\, d M y H\:i\:s O';
913 case self
::RFC_1123
:
914 return 'D\, d M Y H\:i\:s O';
917 case self
::RFC_3339
:
918 return 'Y\-m\-d\TH\:i\:sP';
922 return 'D\, d M Y H\:i\:s O';
926 return 'Y\-m\-d\TH\:i\:sP';
936 if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
940 $length = iconv_strlen($token, 'UTF-8');
941 return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT
));
945 if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
949 $length = iconv_strlen($token, 'UTF-8');
950 return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT
));
954 $length = iconv_strlen($token, 'UTF-8');
955 $result = substr($this->getMilliSecond(), 0, 3);
956 $result +
= $this->date('s', $this->getUnixTimestamp(), false) * 1000;
957 $result +
= $this->date('i', $this->getUnixTimestamp(), false) * 60000;
958 $result +
= $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
960 return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT
));
964 return $this->_toComment($token);
968 * Private function to make a comment of a token
970 * @param string $token
973 private function _toComment($token)
975 $token = str_split($token);
977 foreach ($token as $tok) {
978 $result .= '\\' . $tok;
985 * Return digit from standard names (english)
986 * Faster implementation than locale aware searching
988 * @param string $name
989 * @return integer Number of this month
990 * @throws Zend_Date_Exception
992 private function _getDigitFromName($name)
1032 require_once 'Zend/Date/Exception.php';
1033 throw new Zend_Date_Exception('Month ($name) is not a known month');
1038 * Counts the exact year number
1039 * < 70 - 2000 added, >70 < 100 - 1900, others just returned
1041 * @param integer $value year number
1042 * @return integer Number of year
1044 public static function getFullYear($value)
1049 } else if ($value < 100) {
1057 * Sets the given date as new date or a given datepart as new datepart returning the new datepart
1058 * This could be for example a localized dayname, the date without time,
1059 * the month or only the seconds. There are about 50 different supported date parts.
1060 * For a complete list of supported datepart values look into the docu
1062 * @param string|integer|array|Zend_Date $date Date or datepart to set
1063 * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
1064 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
1065 * @return Zend_Date Provides fluid interface
1066 * @throws Zend_Date_Exception
1068 public function set($date, $part = null, $locale = null)
1070 if (self
::$_options['format_type'] == 'php') {
1071 $part = Zend_Locale_Format
::convertPhpToIsoFormat($part);
1074 $zone = $this->getTimezoneFromString($date);
1075 $this->setTimezone($zone);
1077 $this->_calculate('set', $date, $part, $locale);
1082 * Adds a date or datepart to the existing date, by extracting $part from $date,
1083 * and modifying this object by adding that part. The $part is then extracted from
1084 * this object and returned as an integer or numeric string (for large values, or $part's
1085 * corresponding to pre-defined formatted date strings).
1086 * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
1087 * There are about 50 different supported date parts.
1088 * For a complete list of supported datepart values look into the docu.
1090 * @param string|integer|array|Zend_Date $date Date or datepart to add
1091 * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
1092 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
1093 * @return Zend_Date Provides fluid interface
1094 * @throws Zend_Date_Exception
1096 public function add($date, $part = self
::TIMESTAMP
, $locale = null)
1098 if (self
::$_options['format_type'] == 'php') {
1099 $part = Zend_Locale_Format
::convertPhpToIsoFormat($part);
1102 $this->_calculate('add', $date, $part, $locale);
1107 * Subtracts a date from another date.
1108 * This could be for example a RFC2822 date, the time,
1109 * the year or only the timestamp. There are about 50 different supported date parts.
1110 * For a complete list of supported datepart values look into the docu
1111 * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
1113 * @param string|integer|array|Zend_Date $date Date or datepart to subtract
1114 * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
1115 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
1116 * @return Zend_Date Provides fluid interface
1117 * @throws Zend_Date_Exception
1119 public function sub($date, $part = self
::TIMESTAMP
, $locale = null)
1121 if (self
::$_options['format_type'] == 'php') {
1122 $part = Zend_Locale_Format
::convertPhpToIsoFormat($part);
1125 $this->_calculate('sub', $date, $part, $locale);
1130 * Compares a date or datepart with the existing one.
1131 * Returns -1 if earlier, 0 if equal and 1 if later.
1133 * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
1134 * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
1135 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
1136 * @return integer 0 = equal, 1 = later, -1 = earlier
1137 * @throws Zend_Date_Exception
1139 public function compare($date, $part = self
::TIMESTAMP
, $locale = null)
1141 if (self
::$_options['format_type'] == 'php') {
1142 $part = Zend_Locale_Format
::convertPhpToIsoFormat($part);
1145 $compare = $this->_calculate('cmp', $date, $part, $locale);
1149 } else if ($compare < 0) {
1156 * Returns a new instance of Zend_Date with the selected part copied.
1157 * To make an exact copy, use PHP's clone keyword.
1158 * For a complete list of supported date part values look into the docu.
1159 * If a date part is copied, all other date parts are set to standard values.
1160 * For example: If only YEAR is copied, the returned date object is equal to
1161 * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
1162 * If only HOUR is copied, the returned date object is equal to
1163 * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
1165 * @param string $part Part of the date to compare, if null the timestamp is subtracted
1166 * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
1167 * @return Zend_Date New clone with requested part
1169 public function copyPart($part, $locale = null)
1171 $clone = clone $this; // copy all instance variables
1172 $clone->setUnixTimestamp(0); // except the timestamp
1173 if ($locale != null) {
1174 $clone->setLocale($locale); // set an other locale if selected
1176 $clone->set($this, $part);
1181 * Internal function, returns the offset of a given timezone
1183 * @param string $zone
1186 public function getTimezoneFromString($zone)
1188 if (is_array($zone)) {
1189 return $this->getTimezone();
1192 if ($zone instanceof Zend_Date
) {
1193 return $zone->getTimezone();
1197 preg_match('/\dZ$/', $zone, $match);
1198 if (!empty($match)) {
1202 preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
1203 if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
1205 $zone .= ($match[count($match) - 1] < 0) ?
"+" : "-";
1206 $zone .= (int) abs($match[count($match) - 1]);
1210 preg_match('/([[:alpha:]\/]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
1212 if (!empty($match) and (!is_int($match[count($match) - 1]))) {
1213 $oldzone = $this->getTimezone();
1214 $this->setTimezone($match[count($match) - 1]);
1215 $result = $this->getTimezone();
1216 $this->setTimezone($oldzone);
1217 if ($result !== $oldzone) {
1218 return $match[count($match) - 1];
1221 } catch (Exception
$e) {
1225 return $this->getTimezone();
1229 * Calculates the date or object
1231 * @param string $calc Calculation to make
1232 * @param string|integer $date Date for calculation
1233 * @param string|integer $comp Second date for calculation
1234 * @param boolean|integer $dst Use dst correction if option is set
1235 * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
1237 private function _assign($calc, $date, $comp = 0, $dst = false)
1241 if (!empty($comp)) {
1242 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math
::$sub, $this->getUnixTimestamp(), $comp));
1244 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math
::$add, $this->getUnixTimestamp(), $date));
1245 $value = $this->getUnixTimestamp();
1248 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math
::$add, $this->getUnixTimestamp(), $date));
1249 $value = $this->getUnixTimestamp();
1252 $this->setUnixTimestamp(call_user_func(Zend_Locale_Math
::$sub, $this->getUnixTimestamp(), $date));
1253 $value = $this->getUnixTimestamp();
1257 return call_user_func(Zend_Locale_Math
::$comp, $comp, $date);
1261 // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
1262 if ((self
::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst
=== true)) {
1263 $hour = $this->toString(self
::HOUR
, 'iso');
1264 if ($hour != $dst) {
1265 if (($dst == ($hour +
1)) or ($dst == ($hour - 23))) {
1267 } else if (($dst == ($hour - 1)) or ($dst == ($hour +
23))) {
1270 $this->setUnixTimestamp($value);
1273 return $this->getUnixTimestamp();
1278 * Calculates the date or object
1280 * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
1281 * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
1282 * @param string $part Part of the date to calculate, if null the timestamp is used
1283 * @param string|Zend_Locale $locale Locale for parsing input
1284 * @return integer|string|Zend_Date new timestamp
1285 * @throws Zend_Date_Exception
1287 private function _calculate($calc, $date, $part, $locale)
1289 if ($date === null) {
1290 require_once 'Zend/Date/Exception.php';
1291 throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
1294 if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale
::isLocale($part, null, false))) {
1299 if ($locale === null) {
1300 $locale = $this->getLocale();
1303 $locale = (string) $locale;
1305 // Create date parts
1306 $year = $this->toString(self
::YEAR
, 'iso');
1307 $month = $this->toString(self
::MONTH_SHORT
, 'iso');
1308 $day = $this->toString(self
::DAY_SHORT
, 'iso');
1309 $hour = $this->toString(self
::HOUR_SHORT
, 'iso');
1310 $minute = $this->toString(self
::MINUTE_SHORT
, 'iso');
1311 $second = $this->toString(self
::SECOND_SHORT
, 'iso');
1312 // If object extract value
1313 if ($date instanceof Zend_Date
) {
1314 $date = $date->toString($part, 'iso', $locale);
1317 if (is_array($date) === true) {
1318 if (empty($part) === false) {
1322 case self
::DAY_SHORT
:
1323 if (isset($date['day']) === true) {
1324 $date = $date['day'];
1328 case self
::WEEKDAY_SHORT
:
1330 case self
::WEEKDAY_8601
:
1331 case self
::WEEKDAY_DIGIT
:
1332 case self
::WEEKDAY_NARROW
:
1333 case self
::WEEKDAY_NAME
:
1334 if (isset($date['weekday']) === true) {
1335 $date = $date['weekday'];
1336 $part = self
::WEEKDAY_DIGIT
;
1339 case self
::DAY_OF_YEAR
:
1340 if (isset($date['day_of_year']) === true) {
1341 $date = $date['day_of_year'];
1346 case self
::MONTH_SHORT
:
1347 case self
::MONTH_NAME
:
1348 case self
::MONTH_NAME_SHORT
:
1349 case self
::MONTH_NAME_NARROW
:
1350 if (isset($date['month']) === true) {
1351 $date = $date['month'];
1356 case self
::YEAR_SHORT
:
1357 case self
::YEAR_8601
:
1358 case self
::YEAR_SHORT_8601
:
1359 if (isset($date['year']) === true) {
1360 $date = $date['year'];
1366 case self
::HOUR_SHORT
:
1367 case self
::HOUR_SHORT_AM
:
1368 if (isset($date['hour']) === true) {
1369 $date = $date['hour'];
1374 case self
::MINUTE_SHORT
:
1375 if (isset($date['minute']) === true) {
1376 $date = $date['minute'];
1381 case self
::SECOND_SHORT
:
1382 if (isset($date['second']) === true) {
1383 $date = $date['second'];
1387 case self
::TIMEZONE
:
1388 case self
::TIMEZONE_NAME
:
1389 if (isset($date['timezone']) === true) {
1390 $date = $date['timezone'];
1393 case self
::TIMESTAMP
:
1394 if (isset($date['timestamp']) === true) {
1395 $date = $date['timestamp'];
1399 if (isset($date['week']) === true) {
1400 $date = $date['week'];
1403 case self
::TIMEZONE_SECS
:
1404 if (isset($date['gmtsecs']) === true) {
1405 $date = $date['gmtsecs'];
1409 require_once 'Zend/Date/Exception.php';
1410 throw new Zend_Date_Exception("datepart for part ($part) not found in array");
1415 if (isset($date['hour']) === true) {
1416 $hours = $date['hour'];
1419 if (isset($date['minute']) === true) {
1420 $minutes = $date['minute'];
1423 if (isset($date['second']) === true) {
1424 $seconds = $date['second'];
1427 if (isset($date['month']) === true) {
1428 $months = $date['month'];
1431 if (isset($date['day']) === true) {
1432 $days = $date['day'];
1435 if (isset($date['year']) === true) {
1436 $years = $date['year'];
1438 return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
1439 $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
1443 // $date as object, part of foreign date as own date
1448 if (is_numeric($date)) {
1449 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
intval($date), 1970, true),
1450 $this->mktime(0, 0, 0, 1, 1 +
intval($day), 1970, true), $hour);
1453 require_once 'Zend/Date/Exception.php';
1454 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
1457 case self
::WEEKDAY_SHORT
:
1458 $daylist = Zend_Locale_Data
::getList($locale, 'day');
1459 $weekday = (int) $this->toString(self
::WEEKDAY_DIGIT
, 'iso', $locale);
1462 foreach ($daylist as $key => $value) {
1463 if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
1472 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
$found, 1970, true),
1473 $this->mktime(0, 0, 0, 1, 1 +
$weekday, 1970, true), $hour);
1476 // Weekday not found
1477 require_once 'Zend/Date/Exception.php';
1478 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1481 case self
::DAY_SHORT
:
1482 if (is_numeric($date)) {
1483 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
intval($date), 1970, true),
1484 $this->mktime(0, 0, 0, 1, 1 +
intval($day), 1970, true), $hour);
1487 require_once 'Zend/Date/Exception.php';
1488 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
1492 $daylist = Zend_Locale_Data
::getList($locale, 'day');
1493 $weekday = (int) $this->toString(self
::WEEKDAY_DIGIT
, 'iso', $locale);
1496 foreach ($daylist as $key => $value) {
1497 if (strtoupper($value) == strtoupper($date)) {
1506 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
$found, 1970, true),
1507 $this->mktime(0, 0, 0, 1, 1 +
$weekday, 1970, true), $hour);
1510 // Weekday not found
1511 require_once 'Zend/Date/Exception.php';
1512 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1515 case self
::WEEKDAY_8601
:
1516 $weekday = (int) $this->toString(self
::WEEKDAY_8601
, 'iso', $locale);
1517 if ((intval($date) > 0) and (intval($date) < 8)) {
1518 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
intval($date), 1970, true),
1519 $this->mktime(0, 0, 0, 1, 1 +
$weekday, 1970, true), $hour);
1522 // Weekday not found
1523 require_once 'Zend/Date/Exception.php';
1524 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1527 case self
::DAY_SUFFIX
:
1528 require_once 'Zend/Date/Exception.php';
1529 throw new Zend_Date_Exception('day suffix not supported', 0, null, $date);
1532 case self
::WEEKDAY_DIGIT
:
1533 $weekday = (int) $this->toString(self
::WEEKDAY_DIGIT
, 'iso', $locale);
1534 if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
1535 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
$date, 1970, true),
1536 $this->mktime(0, 0, 0, 1, 1 +
$weekday, 1970, true), $hour);
1539 // Weekday not found
1540 require_once 'Zend/Date/Exception.php';
1541 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1544 case self
::DAY_OF_YEAR
:
1545 if (is_numeric($date)) {
1546 if (($calc == 'add') ||
($calc == 'sub')) {
1552 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, $date, $year, true),
1553 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1556 require_once 'Zend/Date/Exception.php';
1557 throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
1560 case self
::WEEKDAY_NARROW
:
1561 $daylist = Zend_Locale_Data
::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
1562 $weekday = (int) $this->toString(self
::WEEKDAY_DIGIT
, 'iso', $locale);
1564 foreach ($daylist as $key => $value) {
1565 if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
1574 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
$found, 1970, true),
1575 $this->mktime(0, 0, 0, 1, 1 +
$weekday, 1970, true), $hour);
1578 // Weekday not found
1579 require_once 'Zend/Date/Exception.php';
1580 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1583 case self
::WEEKDAY_NAME
:
1584 $daylist = Zend_Locale_Data
::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
1585 $weekday = (int) $this->toString(self
::WEEKDAY_DIGIT
, 'iso', $locale);
1587 foreach ($daylist as $key => $value) {
1588 if (strtoupper($value) == strtoupper($date)) {
1597 return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 +
$found, 1970, true),
1598 $this->mktime(0, 0, 0, 1, 1 +
$weekday, 1970, true), $hour);
1601 // Weekday not found
1602 require_once 'Zend/Date/Exception.php';
1603 throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
1608 if (is_numeric($date)) {
1609 $week = (int) $this->toString(self
::WEEK
, 'iso', $locale);
1610 return $this->_assign($calc, parent
::mktime(0, 0, 0, 1, 1 +
($date * 7), 1970, true),
1611 parent
::mktime(0, 0, 0, 1, 1 +
($week * 7), 1970, true), $hour);
1614 require_once 'Zend/Date/Exception.php';
1615 throw new Zend_Date_Exception("invalid date ($date) operand, week expected", 0, null, $date);
1619 case self
::MONTH_NAME
:
1620 $monthlist = Zend_Locale_Data
::getList($locale, 'month');
1622 foreach ($monthlist as $key => $value) {
1623 if (strtoupper($value) == strtoupper($date)) {
1629 $date = array_search($date, $monthlist);
1634 if ($calc == 'add') {
1637 if (self
::$_options['extend_month'] == false) {
1638 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1639 if ($parts['mday'] != $day) {
1640 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1643 } else if ($calc == 'sub') {
1644 $date = $month - $found;
1646 if (self
::$_options['extend_month'] == false) {
1647 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1648 if ($parts['mday'] != $day) {
1649 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1653 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day +
$fixday, $year, true),
1654 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1657 // Monthname not found
1658 require_once 'Zend/Date/Exception.php';
1659 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1663 if (is_numeric($date)) {
1665 if ($calc == 'add') {
1668 if (self
::$_options['extend_month'] == false) {
1669 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1670 if ($parts['mday'] != $day) {
1671 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1674 } else if ($calc == 'sub') {
1675 $date = $month - $date;
1677 if (self
::$_options['extend_month'] == false) {
1678 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1679 if ($parts['mday'] != $day) {
1680 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1684 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day +
$fixday, $year, true),
1685 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1688 require_once 'Zend/Date/Exception.php';
1689 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1692 case self
::MONTH_NAME_SHORT
:
1693 $monthlist = Zend_Locale_Data
::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
1695 foreach ($monthlist as $key => $value) {
1696 if (strtoupper($value) == strtoupper($date)) {
1702 $date = array_search($date, $monthlist);
1707 if ($calc == 'add') {
1710 if (self
::$_options['extend_month'] === false) {
1711 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1712 if ($parts['mday'] != $day) {
1713 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1716 } else if ($calc == 'sub') {
1717 $date = $month - $found;
1719 if (self
::$_options['extend_month'] === false) {
1720 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1721 if ($parts['mday'] != $day) {
1722 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1726 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day +
$fixday, $year, true),
1727 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1730 // Monthname not found
1731 require_once 'Zend/Date/Exception.php';
1732 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1735 case self
::MONTH_SHORT
:
1736 if (is_numeric($date) === true) {
1738 if ($calc === 'add') {
1741 if (self
::$_options['extend_month'] === false) {
1742 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1743 if ($parts['mday'] != $day) {
1744 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1747 } else if ($calc === 'sub') {
1748 $date = $month - $date;
1750 if (self
::$_options['extend_month'] === false) {
1751 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1752 if ($parts['mday'] != $day) {
1753 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1758 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day +
$fixday, $year, true),
1759 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1762 require_once 'Zend/Date/Exception.php';
1763 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1766 case self
::MONTH_DAYS
:
1767 require_once 'Zend/Date/Exception.php';
1768 throw new Zend_Date_Exception('month days not supported', 0, null, $date);
1771 case self
::MONTH_NAME_NARROW
:
1772 $monthlist = Zend_Locale_Data
::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
1774 foreach ($monthlist as $key => $value) {
1775 if (strtoupper($value) === strtoupper($date)) {
1781 $date = array_search($date, $monthlist);
1786 if ($calc === 'add') {
1789 if (self
::$_options['extend_month'] === false) {
1790 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1791 if ($parts['mday'] != $day) {
1792 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1795 } else if ($calc === 'sub') {
1796 $date = $month - $found;
1798 if (self
::$_options['extend_month'] === false) {
1799 $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
1800 if ($parts['mday'] != $day) {
1801 $fixday = ($parts['mday'] < $day) ?
-$parts['mday'] : ($parts['mday'] - $day);
1805 return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day +
$fixday, $year, true),
1806 $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
1809 // Monthname not found
1810 require_once 'Zend/Date/Exception.php';
1811 throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
1815 case self
::LEAPYEAR
:
1816 require_once 'Zend/Date/Exception.php';
1817 throw new Zend_Date_Exception('leap year not supported', 0, null, $date);
1820 case self
::YEAR_8601
:
1821 if (is_numeric($date)) {
1822 if ($calc === 'add') {
1825 } else if ($calc === 'sub') {
1826 $date = $year - $date;
1830 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
1831 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1834 require_once 'Zend/Date/Exception.php';
1835 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1839 if (is_numeric($date)) {
1840 if ($calc === 'add') {
1843 } else if ($calc === 'sub') {
1844 $date = $year - $date;
1848 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
1849 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1852 require_once 'Zend/Date/Exception.php';
1853 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1856 case self
::YEAR_SHORT
:
1857 if (is_numeric($date)) {
1858 $date = intval($date);
1859 if (($calc == 'set') ||
($calc == 'cmp')) {
1860 $date = self
::getFullYear($date);
1862 if ($calc === 'add') {
1865 } else if ($calc === 'sub') {
1866 $date = $year - $date;
1870 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
1871 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1874 require_once 'Zend/Date/Exception.php';
1875 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1878 case self
::YEAR_SHORT_8601
:
1879 if (is_numeric($date)) {
1880 $date = intval($date);
1881 if (($calc === 'set') ||
($calc === 'cmp')) {
1882 $date = self
::getFullYear($date);
1884 if ($calc === 'add') {
1887 } else if ($calc === 'sub') {
1888 $date = $year - $date;
1892 return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
1893 $this->mktime(0, 0, 0, $month, $day, $year, true), false);
1896 require_once 'Zend/Date/Exception.php';
1897 throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
1901 case self
::MERIDIEM
:
1902 require_once 'Zend/Date/Exception.php';
1903 throw new Zend_Date_Exception('meridiem not supported', 0, null, $date);
1907 if (is_numeric($date)) {
1908 $rest = intval($date);
1909 $hours = floor($rest * 24 / 1000);
1910 $rest = $rest - ($hours * 1000 / 24);
1911 $minutes = floor($rest * 1440 / 1000);
1912 $rest = $rest - ($minutes * 1000 / 1440);
1913 $seconds = floor($rest * 86400 / 1000);
1914 return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
1915 $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
1918 require_once 'Zend/Date/Exception.php';
1919 throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", 0, null, $date);
1922 case self
::HOUR_SHORT_AM
:
1923 if (is_numeric($date)) {
1924 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1925 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1928 require_once 'Zend/Date/Exception.php';
1929 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1932 case self
::HOUR_SHORT
:
1933 if (is_numeric($date)) {
1934 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1935 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1938 require_once 'Zend/Date/Exception.php';
1939 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1943 if (is_numeric($date)) {
1944 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1945 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1948 require_once 'Zend/Date/Exception.php';
1949 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1953 if (is_numeric($date)) {
1954 return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
1955 $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
1958 require_once 'Zend/Date/Exception.php';
1959 throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
1963 if (is_numeric($date)) {
1964 return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
1965 $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
1968 require_once 'Zend/Date/Exception.php';
1969 throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
1973 if (is_numeric($date)) {
1974 return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
1975 $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
1978 require_once 'Zend/Date/Exception.php';
1979 throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
1982 case self
::MILLISECOND
:
1983 if (is_numeric($date)) {
1986 return $this->setMillisecond($date);
1989 return $this->addMillisecond($date);
1992 return $this->subMillisecond($date);
1996 return $this->compareMillisecond($date);
1999 require_once 'Zend/Date/Exception.php';
2000 throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", 0, null, $date);
2003 case self
::MINUTE_SHORT
:
2004 if (is_numeric($date)) {
2005 return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
2006 $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
2009 require_once 'Zend/Date/Exception.php';
2010 throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
2013 case self
::SECOND_SHORT
:
2014 if (is_numeric($date)) {
2015 return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
2016 $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
2019 require_once 'Zend/Date/Exception.php';
2020 throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
2024 // break intentionally omitted
2025 case self
::TIMEZONE_NAME
:
2026 case self
::TIMEZONE
:
2027 case self
::TIMEZONE_SECS
:
2028 require_once 'Zend/Date/Exception.php';
2029 throw new Zend_Date_Exception('timezone not supported', 0, null, $date);
2032 case self
::DAYLIGHT
:
2033 require_once 'Zend/Date/Exception.php';
2034 throw new Zend_Date_Exception('daylight not supported', 0, null, $date);
2037 case self
::GMT_DIFF
:
2038 case self
::GMT_DIFF_SEP
:
2039 require_once 'Zend/Date/Exception.php';
2040 throw new Zend_Date_Exception('gmtdiff not supported', 0, null, $date);
2044 case self
::ISO_8601
:
2046 preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
2048 if (empty($datematch)) {
2049 preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
2052 if (empty($datematch)) {
2053 preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
2056 if (empty($datematch)) {
2057 preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
2060 if (!empty($datematch)) {
2061 $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
2062 $tmpdate = iconv_substr($date,
2063 $dateMatchCharCount,
2064 iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
2068 preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
2069 if (empty($timematch)) {
2070 preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
2072 if (empty($datematch) and empty($timematch)) {
2073 require_once 'Zend/Date/Exception.php';
2074 throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", 0, null, $date);
2076 if (!empty($timematch)) {
2077 $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
2078 $tmpdate = iconv_substr($tmpdate,
2079 $timeMatchCharCount,
2080 iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
2083 if (empty($datematch)) {
2084 $datematch[1] = 1970;
2087 } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
2088 $datematch[1] = self
::getFullYear($datematch[1]);
2090 if (empty($timematch)) {
2096 if (($calc == 'set') ||
($calc == 'cmp')) {
2101 $datematch[1] -= 1970;
2104 return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 +
$datematch[2], 1 +
$datematch[3], 1970 +
$datematch[1], false),
2105 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, false), false);
2108 case self
::RFC_2822
:
2109 $result = preg_match('/^\w{3},\s(\d{1,2})\s(\w{3})\s(\d{4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4})$/', $date, $match);
2111 require_once 'Zend/Date/Exception.php';
2112 throw new Zend_Date_Exception("no RFC 2822 format ($date)", 0, null, $date);
2115 $months = $this->_getDigitFromName($match[2]);
2117 if (($calc == 'set') ||
($calc == 'cmp')) {
2125 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$months, 1 +
$match[1], 1970 +
$match[3], false),
2126 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, false), false);
2129 case self
::TIMESTAMP
:
2130 if (is_numeric($date)) {
2131 return $this->_assign($calc, $date, $this->getUnixTimestamp());
2134 require_once 'Zend/Date/Exception.php';
2135 throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", 0, null, $date);
2138 // additional formats
2139 // break intentionally omitted
2141 case self
::ERA_NAME
:
2142 require_once 'Zend/Date/Exception.php';
2143 throw new Zend_Date_Exception('era not supported', 0, null, $date);
2148 $parsed = Zend_Locale_Format
::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
2150 if (($calc == 'set') ||
($calc == 'cmp')) {
2155 $parsed['year'] -= 1970;
2159 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2160 $this->mktime(0, 0, 0, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2161 } catch (Zend_Locale_Exception
$e) {
2162 require_once 'Zend/Date/Exception.php';
2163 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2167 case self
::DATE_FULL
:
2169 $format = Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'full'));
2170 $parsed = Zend_Locale_Format
::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2172 if (($calc == 'set') ||
($calc == 'cmp')) {
2177 $parsed['year'] -= 1970;
2180 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2181 $this->mktime(0, 0, 0, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2182 } catch (Zend_Locale_Exception
$e) {
2183 require_once 'Zend/Date/Exception.php';
2184 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2188 case self
::DATE_LONG
:
2190 $format = Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'long'));
2191 $parsed = Zend_Locale_Format
::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2193 if (($calc == 'set') ||
($calc == 'cmp')){
2198 $parsed['year'] -= 1970;
2201 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2202 $this->mktime(0, 0, 0, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2203 } catch (Zend_Locale_Exception
$e) {
2204 require_once 'Zend/Date/Exception.php';
2205 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2209 case self
::DATE_MEDIUM
:
2211 $format = Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'medium'));
2212 $parsed = Zend_Locale_Format
::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2214 if (($calc == 'set') ||
($calc == 'cmp')) {
2219 $parsed['year'] -= 1970;
2222 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2223 $this->mktime(0, 0, 0, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2224 } catch (Zend_Locale_Exception
$e) {
2225 require_once 'Zend/Date/Exception.php';
2226 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2230 case self
::DATE_SHORT
:
2232 $format = Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'short'));
2233 $parsed = Zend_Locale_Format
::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2235 $parsed['year'] = self
::getFullYear($parsed['year']);
2237 if (($calc == 'set') ||
($calc == 'cmp')) {
2242 $parsed['year'] -= 1970;
2245 return $this->_assign($calc, $this->mktime(0, 0, 0, 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2246 $this->mktime(0, 0, 0, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2247 } catch (Zend_Locale_Exception
$e) {
2248 require_once 'Zend/Date/Exception.php';
2249 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2255 if ($calc != 'set') {
2260 $parsed = Zend_Locale_Format
::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
2261 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2262 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2263 } catch (Zend_Locale_Exception
$e) {
2264 require_once 'Zend/Date/Exception.php';
2265 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2269 case self
::TIME_FULL
:
2271 $format = Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'full'));
2272 $parsed = Zend_Locale_Format
::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2273 if ($calc != 'set') {
2279 if (!isset($parsed['second'])) {
2280 $parsed['second'] = 0;
2283 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2284 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2285 } catch (Zend_Locale_Exception
$e) {
2286 require_once 'Zend/Date/Exception.php';
2287 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2291 case self
::TIME_LONG
:
2293 $format = Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'long'));
2294 $parsed = Zend_Locale_Format
::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2295 if ($calc != 'set') {
2300 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2301 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2302 } catch (Zend_Locale_Exception
$e) {
2303 require_once 'Zend/Date/Exception.php';
2304 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2308 case self
::TIME_MEDIUM
:
2310 $format = Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'medium'));
2311 $parsed = Zend_Locale_Format
::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2312 if ($calc != 'set') {
2317 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2318 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2319 } catch (Zend_Locale_Exception
$e) {
2320 require_once 'Zend/Date/Exception.php';
2321 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2325 case self
::TIME_SHORT
:
2327 $format = Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'short'));
2328 $parsed = Zend_Locale_Format
::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2329 if ($calc != 'set') {
2335 if (!isset($parsed['second'])) {
2336 $parsed['second'] = 0;
2339 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
2340 $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
2341 } catch (Zend_Locale_Exception
$e) {
2342 require_once 'Zend/Date/Exception.php';
2343 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2347 case self
::DATETIME
:
2349 $parsed = Zend_Locale_Format
::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
2350 if (($calc == 'set') ||
($calc == 'cmp')) {
2355 $parsed['year'] -= 1970;
2358 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2359 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2360 } catch (Zend_Locale_Exception
$e) {
2361 require_once 'Zend/Date/Exception.php';
2362 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2366 case self
::DATETIME_FULL
:
2368 $format = Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'full'));
2369 $parsed = Zend_Locale_Format
::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2371 if (($calc == 'set') ||
($calc == 'cmp')) {
2376 $parsed['year'] -= 1970;
2380 if (!isset($parsed['second'])) {
2381 $parsed['second'] = 0;
2384 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2385 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2386 } catch (Zend_Locale_Exception
$e) {
2387 require_once 'Zend/Date/Exception.php';
2388 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2392 case self
::DATETIME_LONG
:
2394 $format = Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'long'));
2395 $parsed = Zend_Locale_Format
::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2397 if (($calc == 'set') ||
($calc == 'cmp')){
2402 $parsed['year'] -= 1970;
2405 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2406 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2407 } catch (Zend_Locale_Exception
$e) {
2408 require_once 'Zend/Date/Exception.php';
2409 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2413 case self
::DATETIME_MEDIUM
:
2415 $format = Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'medium'));
2416 $parsed = Zend_Locale_Format
::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2417 if (($calc == 'set') ||
($calc == 'cmp')) {
2422 $parsed['year'] -= 1970;
2425 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2426 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2427 } catch (Zend_Locale_Exception
$e) {
2428 require_once 'Zend/Date/Exception.php';
2429 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2433 case self
::DATETIME_SHORT
:
2435 $format = Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'short'));
2436 $parsed = Zend_Locale_Format
::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
2438 $parsed['year'] = self
::getFullYear($parsed['year']);
2440 if (($calc == 'set') ||
($calc == 'cmp')) {
2445 $parsed['year'] -= 1970;
2449 if (!isset($parsed['second'])) {
2450 $parsed['second'] = 0;
2453 return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 +
$parsed['month'], 1 +
$parsed['day'], 1970 +
$parsed['year'], true),
2454 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), $hour);
2455 } catch (Zend_Locale_Exception
$e) {
2456 require_once 'Zend/Date/Exception.php';
2457 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2461 // ATOM and RFC_3339 are identical
2463 case self
::RFC_3339
:
2464 $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})\d{0,4}([+-]{1}\d{2}:\d{2}|Z)$/', $date, $match);
2466 require_once 'Zend/Date/Exception.php';
2467 throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", 0, null, $date);
2470 if (($calc == 'set') ||
($calc == 'cmp')) {
2478 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$match[2], 1 +
$match[3], 1970 +
$match[1], true),
2479 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), false);
2483 $result = preg_match("/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,20}$/", $date, $match);
2485 require_once 'Zend/Date/Exception.php';
2486 throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", 0, null, $date);
2488 $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') +
1;
2489 $match[0] = iconv_substr($match[0],
2491 iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
2494 $months = $this->_getDigitFromName($match[2]);
2495 $match[3] = self
::getFullYear($match[3]);
2497 if (($calc == 'set') ||
($calc == 'cmp')) {
2505 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$months, 1 +
$match[1], 1970 +
$match[3], true),
2506 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), false);
2510 case self
::RFC_1036
:
2511 // new RFC 822 format, identical to RFC 1036 standard
2512 $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
2514 require_once 'Zend/Date/Exception.php';
2515 throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", 0, null, $date);
2518 $months = $this->_getDigitFromName($match[2]);
2519 $match[3] = self
::getFullYear($match[3]);
2521 if (($calc == 'set') ||
($calc == 'cmp')) {
2529 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$months, 1 +
$match[1], 1970 +
$match[3], false),
2530 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, false), false);
2534 $result = preg_match('/^\w{6,9},\s(\d{2})-(\w{3})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})\s.{3,21}$/', $date, $match);
2536 require_once 'Zend/Date/Exception.php';
2537 throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", 0, null, $date);
2540 $months = $this->_getDigitFromName($match[2]);
2541 $match[3] = self
::getFullYear($match[3]);
2543 if (($calc == 'set') ||
($calc == 'cmp')) {
2551 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$months, 1 +
$match[1], 1970 +
$match[3], true),
2552 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), false);
2555 case self
::RFC_1123
:
2556 $result = preg_match('/^\w{0,3},{0,1}\s{0,1}(\d{1,2})\s(\w{3})\s(\d{2,4})\s(\d{2}):(\d{2}):{0,1}(\d{0,2})\s([+-]{1}\d{4}|\w{1,20})$/', $date, $match);
2558 require_once 'Zend/Date/Exception.php';
2559 throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", 0, null, $date);
2562 $months = $this->_getDigitFromName($match[2]);
2564 if (($calc == 'set') ||
($calc == 'cmp')) {
2572 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$months, 1 +
$match[1], 1970 +
$match[3], true),
2573 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), false);
2577 $result = preg_match('/^\w{3},\s(\d{2})\s(\w{3})\s(\d{2,4})\s(\d{1,2}):(\d{2}):(\d{2})\s.{1,21}$/', $date, $match);
2579 require_once 'Zend/Date/Exception.php';
2580 throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", 0, null, $date);
2583 $months = $this->_getDigitFromName($match[2]);
2584 $match[3] = self
::getFullYear($match[3]);
2586 if (($calc == 'set') ||
($calc == 'cmp')) {
2594 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$months, 1 +
$match[1], 1970 +
$match[3], true),
2595 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), false);
2599 $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
2601 require_once 'Zend/Date/Exception.php';
2602 throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", 0, null, $date);
2605 if (($calc == 'set') ||
($calc == 'cmp')) {
2613 return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 +
$match[2], 1 +
$match[3], 1970 +
$match[1], true),
2614 $this->mktime($hour, $minute, $second, 1 +
$month, 1 +
$day, 1970 +
$year, true), false);
2618 if (!is_numeric($date) ||
!empty($part)) {
2621 $part = Zend_Locale_Format
::getDateFormat($locale) . " ";
2622 $part .= Zend_Locale_Format
::getTimeFormat($locale);
2625 $parsed = Zend_Locale_Format
::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
2626 if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
2627 $parsed['year'] = self
::getFullYear($parsed['year']);
2630 if (($calc == 'set') ||
($calc == 'cmp')) {
2631 if (isset($parsed['month'])) {
2634 $parsed['month'] = 0;
2637 if (isset($parsed['day'])) {
2643 if (isset($parsed['year'])) {
2644 $parsed['year'] -= 1970;
2646 $parsed['year'] = 0;
2650 return $this->_assign($calc, $this->mktime(
2651 isset($parsed['hour']) ?
$parsed['hour'] : 0,
2652 isset($parsed['minute']) ?
$parsed['minute'] : 0,
2653 isset($parsed['second']) ?
$parsed['second'] : 0,
2654 isset($parsed['month']) ?
(1 +
$parsed['month']) : 1,
2655 isset($parsed['day']) ?
(1 +
$parsed['day']) : 1,
2656 isset($parsed['year']) ?
(1970 +
$parsed['year']) : 1970,
2657 false), $this->getUnixTimestamp(), false);
2658 } catch (Zend_Locale_Exception
$e) {
2659 if (!is_numeric($date)) {
2660 require_once 'Zend/Date/Exception.php';
2661 throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
2666 return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
2672 * Returns true when both date objects or date parts are equal.
2674 * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
2676 * @param string|integer|array|Zend_Date $date Date or datepart to equal with
2677 * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
2678 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2680 * @throws Zend_Date_Exception
2682 public function equals($date, $part = self
::TIMESTAMP
, $locale = null)
2684 $result = $this->compare($date, $part, $locale);
2694 * Returns if the given date or datepart is earlier
2696 * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
2698 * @param string|integer|array|Zend_Date $date Date or datepart to compare with
2699 * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
2700 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2702 * @throws Zend_Date_Exception
2704 public function isEarlier($date, $part = null, $locale = null)
2706 $result = $this->compare($date, $part, $locale);
2708 if ($result == -1) {
2716 * Returns if the given date or datepart is later
2718 * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
2719 * Returns if the given date is later
2721 * @param string|integer|array|Zend_Date $date Date or datepart to compare with
2722 * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
2723 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2725 * @throws Zend_Date_Exception
2727 public function isLater($date, $part = null, $locale = null)
2729 $result = $this->compare($date, $part, $locale);
2739 * Returns only the time of the date as new Zend_Date object
2741 * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
2743 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2746 public function getTime($locale = null)
2748 if (self
::$_options['format_type'] == 'php') {
2751 $format = self
::TIME_MEDIUM
;
2754 return $this->copyPart($format, $locale);
2758 * Returns the calculated time
2760 * @param string $calc Calculation to make
2761 * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
2762 * @param string $format Timeformat for parsing input
2763 * @param string|Zend_Locale $locale Locale for parsing input
2764 * @return integer|Zend_Date new time
2765 * @throws Zend_Date_Exception
2767 private function _time($calc, $time, $format, $locale)
2769 if ($time === null) {
2770 require_once 'Zend/Date/Exception.php';
2771 throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
2774 if ($time instanceof Zend_Date
) {
2775 // extract time from object
2776 $time = $time->toString('HH:mm:ss', 'iso');
2778 if (is_array($time)) {
2779 if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
2780 (isset($time['second']) === true)) {
2783 require_once 'Zend/Date/Exception.php';
2784 throw new Zend_Date_Exception("no hour, minute or second given in array");
2787 if (self
::$_options['format_type'] == 'php') {
2788 $format = Zend_Locale_Format
::convertPhpToIsoFormat($format);
2791 if ($locale === null) {
2792 $locale = $this->getLocale();
2795 $parsed = Zend_Locale_Format
::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
2796 } catch (Zend_Locale_Exception
$e) {
2797 require_once 'Zend/Date/Exception.php';
2798 throw new Zend_Date_Exception($e->getMessage(), 0, $e);
2802 if (!array_key_exists('hour', $parsed)) {
2803 $parsed['hour'] = 0;
2806 if (!array_key_exists('minute', $parsed)) {
2807 $parsed['minute'] = 0;
2810 if (!array_key_exists('second', $parsed)) {
2811 $parsed['second'] = 0;
2814 $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT
) . ":";
2815 $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT
) . ":";
2816 $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT
);
2819 $return = $this->_calcdetail($calc, $time, self
::TIMES
, 'de');
2820 if ($calc != 'cmp') {
2829 * Sets a new time for the date object. Format defines how to parse the time string.
2830 * Also a complete date can be given, but only the time is used for setting.
2831 * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
2832 * Returned is the new date object and the existing date is left as it was before
2834 * @param string|integer|array|Zend_Date $time Time to set
2835 * @param string $format OPTIONAL Timeformat for parsing input
2836 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2837 * @return Zend_Date Provides fluid interface
2838 * @throws Zend_Date_Exception
2840 public function setTime($time, $format = null, $locale = null)
2842 return $this->_time('set', $time, $format, $locale);
2847 * Adds a time to the existing date. Format defines how to parse the time string.
2848 * If only parts are given the other parts are set to 0.
2849 * If no format is given, the standardformat of this locale is used.
2850 * For example: HH:mm:ss -> 10 -> +10 hours
2852 * @param string|integer|array|Zend_Date $time Time to add
2853 * @param string $format OPTIONAL Timeformat for parsing input
2854 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2855 * @return Zend_Date Provides fluid interface
2856 * @throws Zend_Date_Exception
2858 public function addTime($time, $format = null, $locale = null)
2860 return $this->_time('add', $time, $format, $locale);
2865 * Subtracts a time from the existing date. Format defines how to parse the time string.
2866 * If only parts are given the other parts are set to 0.
2867 * If no format is given, the standardformat of this locale is used.
2868 * For example: HH:mm:ss -> 10 -> -10 hours
2870 * @param string|integer|array|Zend_Date $time Time to sub
2871 * @param string $format OPTIONAL Timeformat for parsing input
2872 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2873 * @return Zend_Date Provides fluid inteface
2874 * @throws Zend_Date_Exception
2876 public function subTime($time, $format = null, $locale = null)
2878 return $this->_time('sub', $time, $format, $locale);
2883 * Compares the time from the existing date. Format defines how to parse the time string.
2884 * If only parts are given the other parts are set to default.
2885 * If no format us given, the standardformat of this locale is used.
2886 * For example: HH:mm:ss -> 10 -> 10 hours
2888 * @param string|integer|array|Zend_Date $time Time to compare
2889 * @param string $format OPTIONAL Timeformat for parsing input
2890 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2891 * @return integer 0 = equal, 1 = later, -1 = earlier
2892 * @throws Zend_Date_Exception
2894 public function compareTime($time, $format = null, $locale = null)
2896 return $this->_time('cmp', $time, $format, $locale);
2900 * Returns a clone of $this, with the time part set to 00:00:00.
2902 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2905 public function getDate($locale = null)
2907 $orig = self
::$_options['format_type'];
2908 if (self
::$_options['format_type'] == 'php') {
2909 self
::$_options['format_type'] = 'iso';
2912 $date = $this->copyPart(self
::DATE_MEDIUM
, $locale);
2913 $date->addTimestamp($this->getGmtOffset());
2914 self
::$_options['format_type'] = $orig;
2920 * Returns the calculated date
2922 * @param string $calc Calculation to make
2923 * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
2924 * @param string $format Date format for parsing
2925 * @param string|Zend_Locale $locale Locale for parsing input
2926 * @return integer|Zend_Date new date
2927 * @throws Zend_Date_Exception
2929 private function _date($calc, $date, $format, $locale)
2931 if ($date === null) {
2932 require_once 'Zend/Date/Exception.php';
2933 throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
2936 if ($date instanceof Zend_Date
) {
2937 // extract date from object
2938 $date = $date->toString('d.M.y', 'iso');
2940 if (is_array($date)) {
2941 if ((isset($date['year']) === true) or (isset($date['month']) === true) or
2942 (isset($date['day']) === true)) {
2945 require_once 'Zend/Date/Exception.php';
2946 throw new Zend_Date_Exception("no day,month or year given in array");
2949 if ((self
::$_options['format_type'] == 'php') && !defined($format)) {
2950 $format = Zend_Locale_Format
::convertPhpToIsoFormat($format);
2953 if ($locale === null) {
2954 $locale = $this->getLocale();
2957 $parsed = Zend_Locale_Format
::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
2958 if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
2959 $parsed['year'] = self
::getFullYear($parsed['year']);
2961 } catch (Zend_Locale_Exception
$e) {
2962 require_once 'Zend/Date/Exception.php';
2963 throw new Zend_Date_Exception($e->getMessage(), 0, $e);
2967 if (!array_key_exists('day', $parsed)) {
2971 if (!array_key_exists('month', $parsed)) {
2972 $parsed['month'] = 1;
2975 if (!array_key_exists('year', $parsed)) {
2976 $parsed['year'] = 0;
2979 $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
2982 $return = $this->_calcdetail($calc, $date, self
::DATE_MEDIUM
, 'de');
2983 if ($calc != 'cmp') {
2991 * Sets a new date for the date object. Format defines how to parse the date string.
2992 * Also a complete date with time can be given, but only the date is used for setting.
2993 * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
2994 * Returned is the new date object and the existing time is left as it was before
2996 * @param string|integer|array|Zend_Date $date Date to set
2997 * @param string $format OPTIONAL Date format for parsing
2998 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
2999 * @return Zend_Date Provides fluid interface
3000 * @throws Zend_Date_Exception
3002 public function setDate($date, $format = null, $locale = null)
3004 return $this->_date('set', $date, $format, $locale);
3009 * Adds a date to the existing date object. Format defines how to parse the date string.
3010 * If only parts are given the other parts are set to 0.
3011 * If no format is given, the standardformat of this locale is used.
3012 * For example: MM.dd.YYYY -> 10 -> +10 months
3014 * @param string|integer|array|Zend_Date $date Date to add
3015 * @param string $format OPTIONAL Date format for parsing input
3016 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3017 * @return Zend_Date Provides fluid interface
3018 * @throws Zend_Date_Exception
3020 public function addDate($date, $format = null, $locale = null)
3022 return $this->_date('add', $date, $format, $locale);
3027 * Subtracts a date from the existing date object. Format defines how to parse the date string.
3028 * If only parts are given the other parts are set to 0.
3029 * If no format is given, the standardformat of this locale is used.
3030 * For example: MM.dd.YYYY -> 10 -> -10 months
3031 * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
3033 * @param string|integer|array|Zend_Date $date Date to sub
3034 * @param string $format OPTIONAL Date format for parsing input
3035 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3036 * @return Zend_Date Provides fluid interface
3037 * @throws Zend_Date_Exception
3039 public function subDate($date, $format = null, $locale = null)
3041 return $this->_date('sub', $date, $format, $locale);
3046 * Compares the date from the existing date object, ignoring the time.
3047 * Format defines how to parse the date string.
3048 * If only parts are given the other parts are set to 0.
3049 * If no format is given, the standardformat of this locale is used.
3050 * For example: 10.01.2000 => 10.02.1999 -> false
3052 * @param string|integer|array|Zend_Date $date Date to compare
3053 * @param string $format OPTIONAL Date format for parsing input
3054 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3055 * @return integer 0 = equal, 1 = later, -1 = earlier
3056 * @throws Zend_Date_Exception
3058 public function compareDate($date, $format = null, $locale = null)
3060 return $this->_date('cmp', $date, $format, $locale);
3065 * Returns the full ISO 8601 date from the date object.
3066 * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
3067 * (ISO 8601 defines several formats) use toString() instead.
3068 * This function does not return the ISO date as object. Use copy() instead.
3070 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3073 public function getIso($locale = null)
3075 return $this->toString(self
::ISO_8601
, 'iso', $locale);
3080 * Sets a new date for the date object. Not given parts are set to default.
3081 * Only supported ISO 8601 formats are accepted.
3082 * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
3083 * Returned is the new date object
3085 * @param string|integer|Zend_Date $date ISO Date to set
3086 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3087 * @return Zend_Date Provides fluid interface
3088 * @throws Zend_Date_Exception
3090 public function setIso($date, $locale = null)
3092 return $this->_calcvalue('set', $date, 'iso', self
::ISO_8601
, $locale);
3097 * Adds a ISO date to the date object. Not given parts are set to default.
3098 * Only supported ISO 8601 formats are accepted.
3099 * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
3100 * Returned is the new date object
3102 * @param string|integer|Zend_Date $date ISO Date to add
3103 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3104 * @return Zend_Date Provides fluid interface
3105 * @throws Zend_Date_Exception
3107 public function addIso($date, $locale = null)
3109 return $this->_calcvalue('add', $date, 'iso', self
::ISO_8601
, $locale);
3114 * Subtracts a ISO date from the date object. Not given parts are set to default.
3115 * Only supported ISO 8601 formats are accepted.
3116 * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
3117 * Returned is the new date object
3119 * @param string|integer|Zend_Date $date ISO Date to sub
3120 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3121 * @return Zend_Date Provides fluid interface
3122 * @throws Zend_Date_Exception
3124 public function subIso($date, $locale = null)
3126 return $this->_calcvalue('sub', $date, 'iso', self
::ISO_8601
, $locale);
3131 * Compares a ISO date with the date object. Not given parts are set to default.
3132 * Only supported ISO 8601 formats are accepted.
3133 * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
3134 * Returns if equal, earlier or later
3136 * @param string|integer|Zend_Date $date ISO Date to sub
3137 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3138 * @return integer 0 = equal, 1 = later, -1 = earlier
3139 * @throws Zend_Date_Exception
3141 public function compareIso($date, $locale = null)
3143 return $this->_calcvalue('cmp', $date, 'iso', self
::ISO_8601
, $locale);
3148 * Returns a RFC 822 compilant datestring from the date object.
3149 * This function does not return the RFC date as object. Use copy() instead.
3151 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3154 public function getArpa($locale = null)
3156 if (self
::$_options['format_type'] == 'php') {
3157 $format = 'D\, d M y H\:i\:s O';
3159 $format = self
::RFC_822
;
3162 return $this->toString($format, 'iso', $locale);
3167 * Sets a RFC 822 date as new date for the date object.
3168 * Only RFC 822 compilant date strings are accepted.
3169 * For example: Sat, 14 Feb 09 00:31:30 +0100
3170 * Returned is the new date object
3172 * @param string|integer|Zend_Date $date RFC 822 to set
3173 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3174 * @return Zend_Date Provides fluid interface
3175 * @throws Zend_Date_Exception
3177 public function setArpa($date, $locale = null)
3179 return $this->_calcvalue('set', $date, 'arpa', self
::RFC_822
, $locale);
3184 * Adds a RFC 822 date to the date object.
3185 * ARPA messages are used in emails or HTTP Headers.
3186 * Only RFC 822 compilant date strings are accepted.
3187 * For example: Sat, 14 Feb 09 00:31:30 +0100
3188 * Returned is the new date object
3190 * @param string|integer|Zend_Date $date RFC 822 Date to add
3191 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3192 * @return Zend_Date Provides fluid interface
3193 * @throws Zend_Date_Exception
3195 public function addArpa($date, $locale = null)
3197 return $this->_calcvalue('add', $date, 'arpa', self
::RFC_822
, $locale);
3202 * Subtracts a RFC 822 date from the date object.
3203 * ARPA messages are used in emails or HTTP Headers.
3204 * Only RFC 822 compilant date strings are accepted.
3205 * For example: Sat, 14 Feb 09 00:31:30 +0100
3206 * Returned is the new date object
3208 * @param string|integer|Zend_Date $date RFC 822 Date to sub
3209 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3210 * @return Zend_Date Provides fluid interface
3211 * @throws Zend_Date_Exception
3213 public function subArpa($date, $locale = null)
3215 return $this->_calcvalue('sub', $date, 'arpa', self
::RFC_822
, $locale);
3220 * Compares a RFC 822 compilant date with the date object.
3221 * ARPA messages are used in emails or HTTP Headers.
3222 * Only RFC 822 compilant date strings are accepted.
3223 * For example: Sat, 14 Feb 09 00:31:30 +0100
3224 * Returns if equal, earlier or later
3226 * @param string|integer|Zend_Date $date RFC 822 Date to sub
3227 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3228 * @return integer 0 = equal, 1 = later, -1 = earlier
3229 * @throws Zend_Date_Exception
3231 public function compareArpa($date, $locale = null)
3233 return $this->_calcvalue('cmp', $date, 'arpa', self
::RFC_822
, $locale);
3238 * Check if location is supported
3240 * @param $location array - locations array
3241 * @return $horizon float
3243 private function _checkLocation($location)
3245 if (!isset($location['longitude']) or !isset($location['latitude'])) {
3246 require_once 'Zend/Date/Exception.php';
3247 throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
3249 if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
3250 require_once 'Zend/Date/Exception.php';
3251 throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
3253 if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
3254 require_once 'Zend/Date/Exception.php';
3255 throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
3258 if (!isset($location['horizon'])){
3259 $location['horizon'] = 'effective';
3262 switch ($location['horizon']) {
3280 * Returns the time of sunrise for this date and a given location as new date object
3281 * For a list of cities and correct locations use the class Zend_Date_Cities
3283 * @param $location array - location of sunrise
3284 * ['horizon'] -> civil, nautic, astronomical, effective (default)
3285 * ['longitude'] -> longitude of location
3286 * ['latitude'] -> latitude of location
3288 * @throws Zend_Date_Exception
3290 public function getSunrise($location)
3292 $horizon = $this->_checkLocation($location);
3293 $result = clone $this;
3294 $result->set($this->calcSun($location, $horizon, true), self
::TIMESTAMP
);
3300 * Returns the time of sunset for this date and a given location as new date object
3301 * For a list of cities and correct locations use the class Zend_Date_Cities
3303 * @param $location array - location of sunset
3304 * ['horizon'] -> civil, nautic, astronomical, effective (default)
3305 * ['longitude'] -> longitude of location
3306 * ['latitude'] -> latitude of location
3308 * @throws Zend_Date_Exception
3310 public function getSunset($location)
3312 $horizon = $this->_checkLocation($location);
3313 $result = clone $this;
3314 $result->set($this->calcSun($location, $horizon, false), self
::TIMESTAMP
);
3320 * Returns an array with the sunset and sunrise dates for all horizon types
3321 * For a list of cities and correct locations use the class Zend_Date_Cities
3323 * @param $location array - location of suninfo
3324 * ['horizon'] -> civil, nautic, astronomical, effective (default)
3325 * ['longitude'] -> longitude of location
3326 * ['latitude'] -> latitude of location
3327 * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
3328 * @throws Zend_Date_Exception
3330 public function getSunInfo($location)
3333 for ($i = 0; $i < 4; ++
$i) {
3336 $location['horizon'] = 'effective';
3339 $location['horizon'] = 'civil';
3342 $location['horizon'] = 'nautic';
3345 $location['horizon'] = 'astronomic';
3348 $horizon = $this->_checkLocation($location);
3349 $result = clone $this;
3350 $result->set($this->calcSun($location, $horizon, true), self
::TIMESTAMP
);
3351 $suninfo['sunrise'][$location['horizon']] = $result;
3352 $result = clone $this;
3353 $result->set($this->calcSun($location, $horizon, false), self
::TIMESTAMP
);
3354 $suninfo['sunset'][$location['horizon']] = $result;
3361 * Check a given year for leap year.
3363 * @param integer|array|Zend_Date $year Year to check
3366 public static function checkLeapYear($year)
3368 if ($year instanceof Zend_Date
) {
3369 $year = (int) $year->toString(self
::YEAR
, 'iso');
3372 if (is_array($year)) {
3373 if (isset($year['year']) === true) {
3374 $year = $year['year'];
3376 require_once 'Zend/Date/Exception.php';
3377 throw new Zend_Date_Exception("no year given in array");
3381 if (!is_numeric($year)) {
3382 require_once 'Zend/Date/Exception.php';
3383 throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
3386 return (bool) parent
::isYearLeapYear($year);
3391 * Returns true, if the year is a leap year.
3395 public function isLeapYear()
3397 return self
::checkLeapYear($this);
3402 * Returns if the set date is todays date
3406 public function isToday()
3408 $today = $this->date('Ymd', $this->_getTime());
3409 $day = $this->date('Ymd', $this->getUnixTimestamp());
3410 return ($today == $day);
3415 * Returns if the set date is yesterdays date
3419 public function isYesterday()
3421 list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
3422 // adjusts for leap days and DST changes that are timezone specific
3423 $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
3424 $day = $this->date('Ymd', $this->getUnixTimestamp());
3425 return $day == $yesterday;
3430 * Returns if the set date is tomorrows date
3434 public function isTomorrow()
3436 list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
3437 // adjusts for leap days and DST changes that are timezone specific
3438 $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +
1, $year));
3439 $day = $this->date('Ymd', $this->getUnixTimestamp());
3440 return $day == $tomorrow;
3444 * Returns the actual date as new date object
3446 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3449 public static function now($locale = null)
3451 return new Zend_Date(time(), self
::TIMESTAMP
, $locale);
3455 * Calculate date details
3457 * @param string $calc Calculation to make
3458 * @param string|integer|array|Zend_Date $date Date or Part to calculate
3459 * @param string $part Datepart for Calculation
3460 * @param string|Zend_Locale $locale Locale for parsing input
3461 * @return integer|string new date
3462 * @throws Zend_Date_Exception
3464 private function _calcdetail($calc, $date, $type, $locale)
3467 if (self
::$_options['format_type'] == 'php') {
3468 self
::$_options['format_type'] = 'iso';
3474 $return = $this->set($date, $type, $locale);
3477 $return = $this->add($date, $type, $locale);
3480 $return = $this->sub($date, $type, $locale);
3483 $return = $this->compare($date, $type, $locale);
3488 self
::$_options['format_type'] = 'php';
3495 * Internal calculation, returns the requested date type
3497 * @param string $calc Calculation to make
3498 * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
3499 * @param string|Zend_Locale $locale Locale for parsing input
3500 * @return integer|Zend_Date new date
3501 * @throws Zend_Date_Exception
3503 private function _calcvalue($calc, $value, $type, $parameter, $locale)
3505 if ($value === null) {
3506 require_once 'Zend/Date/Exception.php';
3507 throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
3510 if ($locale === null) {
3511 $locale = $this->getLocale();
3514 if ($value instanceof Zend_Date
) {
3515 // extract value from object
3516 $value = $value->toString($parameter, 'iso', $locale);
3517 } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
3518 require_once 'Zend/Date/Exception.php';
3519 throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
3522 $return = $this->_calcdetail($calc, $value, $parameter, $locale);
3523 if ($calc != 'cmp') {
3531 * Returns only the year from the date object as new object.
3532 * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
3534 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3537 public function getYear($locale = null)
3539 if (self
::$_options['format_type'] == 'php') {
3542 $format = self
::YEAR
;
3545 return $this->copyPart($format, $locale);
3551 * If the year is between 0 and 69, 2000 will be set (2000-2069)
3552 * If the year if between 70 and 99, 1999 will be set (1970-1999)
3553 * 3 or 4 digit years are set as expected. If you need to set year 0-99
3554 * use set() instead.
3555 * Returned is the new date object
3557 * @param string|integer|array|Zend_Date $date Year to set
3558 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3559 * @return Zend_Date Provides fluid interface
3560 * @throws Zend_Date_Exception
3562 public function setYear($year, $locale = null)
3564 return $this->_calcvalue('set', $year, 'year', self
::YEAR
, $locale);
3569 * Adds the year to the existing date object
3570 * If the year is between 0 and 69, 2000 will be added (2000-2069)
3571 * If the year if between 70 and 99, 1999 will be added (1970-1999)
3572 * 3 or 4 digit years are added as expected. If you need to add years from 0-99
3573 * use add() instead.
3574 * Returned is the new date object
3576 * @param string|integer|array|Zend_Date $date Year to add
3577 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3578 * @return Zend_Date Provides fluid interface
3579 * @throws Zend_Date_Exception
3581 public function addYear($year, $locale = null)
3583 return $this->_calcvalue('add', $year, 'year', self
::YEAR
, $locale);
3588 * Subs the year from the existing date object
3589 * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
3590 * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
3591 * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
3592 * use sub() instead.
3593 * Returned is the new date object
3595 * @param string|integer|array|Zend_Date $date Year to sub
3596 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3597 * @return Zend_Date Provides fluid interface
3598 * @throws Zend_Date_Exception
3600 public function subYear($year, $locale = null)
3602 return $this->_calcvalue('sub', $year, 'year', self
::YEAR
, $locale);
3607 * Compares the year with the existing date object, ignoring other date parts.
3608 * For example: 10.03.2000 -> 15.02.2000 -> true
3609 * Returns if equal, earlier or later
3611 * @param string|integer|array|Zend_Date $year Year to compare
3612 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3613 * @return integer 0 = equal, 1 = later, -1 = earlier
3614 * @throws Zend_Date_Exception
3616 public function compareYear($year, $locale = null)
3618 return $this->_calcvalue('cmp', $year, 'year', self
::YEAR
, $locale);
3623 * Returns only the month from the date object as new object.
3624 * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
3626 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3629 public function getMonth($locale = null)
3631 if (self
::$_options['format_type'] == 'php') {
3634 $format = self
::MONTH
;
3637 return $this->copyPart($format, $locale);
3642 * Returns the calculated month
3644 * @param string $calc Calculation to make
3645 * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
3646 * @param string|Zend_Locale $locale Locale for parsing input
3647 * @return integer|Zend_Date new time
3648 * @throws Zend_Date_Exception
3650 private function _month($calc, $month, $locale)
3652 if ($month === null) {
3653 require_once 'Zend/Date/Exception.php';
3654 throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
3657 if ($locale === null) {
3658 $locale = $this->getLocale();
3661 if ($month instanceof Zend_Date
) {
3662 // extract month from object
3663 $found = $month->toString(self
::MONTH_SHORT
, 'iso', $locale);
3665 if (is_numeric($month)) {
3667 } else if (is_array($month)) {
3668 if (isset($month['month']) === true) {
3669 $month = $month['month'];
3671 require_once 'Zend/Date/Exception.php';
3672 throw new Zend_Date_Exception("no month given in array");
3675 $monthlist = Zend_Locale_Data
::getList($locale, 'month');
3676 $monthlist2 = Zend_Locale_Data
::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
3678 $monthlist = array_merge($monthlist, $monthlist2);
3681 foreach ($monthlist as $key => $value) {
3682 if (strtoupper($value) == strtoupper($month)) {
3683 $found = ($key %
12) +
1;
3689 foreach ($monthlist2 as $key => $value) {
3690 if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
3698 require_once 'Zend/Date/Exception.php';
3699 throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
3703 $return = $this->_calcdetail($calc, $found, self
::MONTH_SHORT
, $locale);
3704 if ($calc != 'cmp') {
3713 * The month can be a number or a string. Setting months lower then 0 and greater then 12
3714 * will result in adding or subtracting the relevant year. (12 months equal one year)
3715 * If a localized monthname is given it will be parsed with the default locale or the optional
3717 * Returned is the new date object
3719 * @param string|integer|array|Zend_Date $month Month to set
3720 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3721 * @return Zend_Date Provides fluid interface
3722 * @throws Zend_Date_Exception
3724 public function setMonth($month, $locale = null)
3726 return $this->_month('set', $month, $locale);
3731 * Adds months to the existing date object.
3732 * The month can be a number or a string. Adding months lower then 0 and greater then 12
3733 * will result in adding or subtracting the relevant year. (12 months equal one year)
3734 * If a localized monthname is given it will be parsed with the default locale or the optional
3736 * Returned is the new date object
3738 * @param string|integer|array|Zend_Date $month Month to add
3739 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3740 * @return Zend_Date Provides fluid interface
3741 * @throws Zend_Date_Exception
3743 public function addMonth($month, $locale = null)
3745 return $this->_month('add', $month, $locale);
3750 * Subtracts months from the existing date object.
3751 * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
3752 * will result in adding or subtracting the relevant year. (12 months equal one year)
3753 * If a localized monthname is given it will be parsed with the default locale or the optional
3755 * Returned is the new date object
3757 * @param string|integer|array|Zend_Date $month Month to sub
3758 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3759 * @return Zend_Date Provides fluid interface
3760 * @throws Zend_Date_Exception
3762 public function subMonth($month, $locale = null)
3764 return $this->_month('sub', $month, $locale);
3769 * Compares the month with the existing date object, ignoring other date parts.
3770 * For example: 10.03.2000 -> 15.03.1950 -> true
3771 * Returns if equal, earlier or later
3773 * @param string|integer|array|Zend_Date $month Month to compare
3774 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3775 * @return integer 0 = equal, 1 = later, -1 = earlier
3776 * @throws Zend_Date_Exception
3778 public function compareMonth($month, $locale = null)
3780 return $this->_month('cmp', $month, $locale);
3785 * Returns the day as new date object
3786 * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
3788 * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
3791 public function getDay($locale = null)
3793 return $this->copyPart(self
::DAY_SHORT
, $locale);
3798 * Returns the calculated day
3800 * @param $calc string Type of calculation to make
3801 * @param $day string|integer|Zend_Date Day to calculate, when null the actual day is calculated
3802 * @param $locale string|Zend_Locale Locale for parsing input
3803 * @return Zend_Date|integer
3805 private function _day($calc, $day, $locale)
3807 if ($day === null) {
3808 require_once 'Zend/Date/Exception.php';
3809 throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
3812 if ($locale === null) {
3813 $locale = $this->getLocale();
3816 if ($day instanceof Zend_Date
) {
3817 $day = $day->toString(self
::DAY_SHORT
, 'iso', $locale);
3820 if (is_numeric($day)) {
3821 $type = self
::DAY_SHORT
;
3822 } else if (is_array($day)) {
3823 if (isset($day['day']) === true) {
3825 $type = self
::WEEKDAY
;
3827 require_once 'Zend/Date/Exception.php';
3828 throw new Zend_Date_Exception("no day given in array");
3831 switch (iconv_strlen($day, 'UTF-8')) {
3833 $type = self
::WEEKDAY_NARROW
;
3836 $type = self
::WEEKDAY_NAME
;
3839 $type = self
::WEEKDAY_SHORT
;
3842 $type = self
::WEEKDAY
;
3846 $return = $this->_calcdetail($calc, $day, $type, $locale);
3847 if ($calc != 'cmp') {
3856 * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
3857 * will result in adding or subtracting the relevant month.
3858 * If a localized dayname is given it will be parsed with the default locale or the optional
3860 * Returned is the new date object
3861 * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
3863 * @param string|integer|array|Zend_Date $month Day to set
3864 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3865 * @return Zend_Date Provides fluid interface
3866 * @throws Zend_Date_Exception
3868 public function setDay($day, $locale = null)
3870 return $this->_day('set', $day, $locale);
3875 * Adds days to the existing date object.
3876 * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
3877 * will result in adding or subtracting the relevant month.
3878 * If a localized dayname is given it will be parsed with the default locale or the optional
3881 * @param string|integer|array|Zend_Date $month Day to add
3882 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3883 * @return Zend_Date Provides fluid interface
3884 * @throws Zend_Date_Exception
3886 public function addDay($day, $locale = null)
3888 return $this->_day('add', $day, $locale);
3893 * Subtracts days from the existing date object.
3894 * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
3895 * will result in adding or subtracting the relevant month.
3896 * If a localized dayname is given it will be parsed with the default locale or the optional
3899 * @param string|integer|array|Zend_Date $month Day to sub
3900 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3901 * @return Zend_Date Provides fluid interface
3902 * @throws Zend_Date_Exception
3904 public function subDay($day, $locale = null)
3906 return $this->_day('sub', $day, $locale);
3911 * Compares the day with the existing date object, ignoring other date parts.
3912 * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
3913 * Returns if equal, earlier or later
3915 * @param string|integer|array|Zend_Date $day Day to compare
3916 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
3917 * @return integer 0 = equal, 1 = later, -1 = earlier
3918 * @throws Zend_Date_Exception
3920 public function compareDay($day, $locale = null)
3922 return $this->_day('cmp', $day, $locale);
3927 * Returns the weekday as new date object
3928 * Weekday is always from 1-7
3929 * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
3931 * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
3934 public function getWeekday($locale = null)
3936 if (self
::$_options['format_type'] == 'php') {
3939 $format = self
::WEEKDAY
;
3942 return $this->copyPart($format, $locale);
3947 * Returns the calculated weekday
3949 * @param $calc string Type of calculation to make
3950 * @param $weekday string|integer|array|Zend_Date Weekday to calculate, when null the actual weekday is calculated
3951 * @param $locale string|Zend_Locale Locale for parsing input
3952 * @return Zend_Date|integer
3953 * @throws Zend_Date_Exception
3955 private function _weekday($calc, $weekday, $locale)
3957 if ($weekday === null) {
3958 require_once 'Zend/Date/Exception.php';
3959 throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
3962 if ($locale === null) {
3963 $locale = $this->getLocale();
3966 if ($weekday instanceof Zend_Date
) {
3967 $weekday = $weekday->toString(self
::WEEKDAY_8601
, 'iso', $locale);
3970 if (is_numeric($weekday)) {
3971 $type = self
::WEEKDAY_8601
;
3972 } else if (is_array($weekday)) {
3973 if (isset($weekday['weekday']) === true) {
3974 $weekday = $weekday['weekday'];
3975 $type = self
::WEEKDAY
;
3977 require_once 'Zend/Date/Exception.php';
3978 throw new Zend_Date_Exception("no weekday given in array");
3981 switch(iconv_strlen($weekday, 'UTF-8')) {
3983 $type = self
::WEEKDAY_NARROW
;
3986 $type = self
::WEEKDAY_NAME
;
3989 $type = self
::WEEKDAY_SHORT
;
3992 $type = self
::WEEKDAY
;
3996 $return = $this->_calcdetail($calc, $weekday, $type, $locale);
3997 if ($calc != 'cmp') {
4005 * Sets a new weekday
4006 * The weekday can be a number or a string. If a localized weekday name is given,
4007 * then it will be parsed as a date in $locale (defaults to the same locale as $this).
4008 * Returned is the new date object.
4009 * Example: setWeekday(3); will set the wednesday of this week as day.
4011 * @param string|integer|array|Zend_Date $month Weekday to set
4012 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4013 * @return Zend_Date Provides fluid interface
4014 * @throws Zend_Date_Exception
4016 public function setWeekday($weekday, $locale = null)
4018 return $this->_weekday('set', $weekday, $locale);
4023 * Adds weekdays to the existing date object.
4024 * The weekday can be a number or a string.
4025 * If a localized dayname is given it will be parsed with the default locale or the optional
4027 * Returned is the new date object
4028 * Example: addWeekday(3); will add the difference of days from the begining of the month until
4031 * @param string|integer|array|Zend_Date $month Weekday to add
4032 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4033 * @return Zend_Date Provides fluid interface
4034 * @throws Zend_Date_Exception
4036 public function addWeekday($weekday, $locale = null)
4038 return $this->_weekday('add', $weekday, $locale);
4043 * Subtracts weekdays from the existing date object.
4044 * The weekday can be a number or a string.
4045 * If a localized dayname is given it will be parsed with the default locale or the optional
4047 * Returned is the new date object
4048 * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
4051 * @param string|integer|array|Zend_Date $month Weekday to sub
4052 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4053 * @return Zend_Date Provides fluid interface
4054 * @throws Zend_Date_Exception
4056 public function subWeekday($weekday, $locale = null)
4058 return $this->_weekday('sub', $weekday, $locale);
4063 * Compares the weekday with the existing date object, ignoring other date parts.
4064 * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
4065 * Returns if equal, earlier or later
4067 * @param string|integer|array|Zend_Date $weekday Weekday to compare
4068 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4069 * @return integer 0 = equal, 1 = later, -1 = earlier
4070 * @throws Zend_Date_Exception
4072 public function compareWeekday($weekday, $locale = null)
4074 return $this->_weekday('cmp', $weekday, $locale);
4079 * Returns the day of year as new date object
4080 * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
4082 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4085 public function getDayOfYear($locale = null)
4087 if (self
::$_options['format_type'] == 'php') {
4090 $format = self
::DAY_OF_YEAR
;
4093 return $this->copyPart($format, $locale);
4098 * Sets a new day of year
4099 * The day of year is always a number.
4100 * Returned is the new date object
4101 * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
4103 * @param string|integer|array|Zend_Date $day Day of Year to set
4104 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4105 * @return Zend_Date Provides fluid interface
4106 * @throws Zend_Date_Exception
4108 public function setDayOfYear($day, $locale = null)
4110 return $this->_calcvalue('set', $day, 'day of year', self
::DAY_OF_YEAR
, $locale);
4115 * Adds a day of year to the existing date object.
4116 * The day of year is always a number.
4117 * Returned is the new date object
4118 * Example: addDayOfYear(10); will add 10 days to the existing date object.
4120 * @param string|integer|array|Zend_Date $day Day of Year to add
4121 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4122 * @return Zend_Date Provides fluid interface
4123 * @throws Zend_Date_Exception
4125 public function addDayOfYear($day, $locale = null)
4127 return $this->_calcvalue('add', $day, 'day of year', self
::DAY_OF_YEAR
, $locale);
4132 * Subtracts a day of year from the existing date object.
4133 * The day of year is always a number.
4134 * Returned is the new date object
4135 * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
4137 * @param string|integer|array|Zend_Date $day Day of Year to sub
4138 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4139 * @return Zend_Date Provides fluid interface
4140 * @throws Zend_Date_Exception
4142 public function subDayOfYear($day, $locale = null)
4144 return $this->_calcvalue('sub', $day, 'day of year', self
::DAY_OF_YEAR
, $locale);
4149 * Compares the day of year with the existing date object.
4150 * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
4151 * Returns if equal, earlier or later
4153 * @param string|integer|array|Zend_Date $day Day of Year to compare
4154 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4155 * @return integer 0 = equal, 1 = later, -1 = earlier
4156 * @throws Zend_Date_Exception
4158 public function compareDayOfYear($day, $locale = null)
4160 return $this->_calcvalue('cmp', $day, 'day of year', self
::DAY_OF_YEAR
, $locale);
4165 * Returns the hour as new date object
4166 * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
4168 * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
4171 public function getHour($locale = null)
4173 return $this->copyPart(self
::HOUR
, $locale);
4179 * The hour is always a number.
4180 * Returned is the new date object
4181 * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
4183 * @param string|integer|array|Zend_Date $hour Hour to set
4184 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4185 * @return Zend_Date Provides fluid interface
4186 * @throws Zend_Date_Exception
4188 public function setHour($hour, $locale = null)
4190 return $this->_calcvalue('set', $hour, 'hour', self
::HOUR_SHORT
, $locale);
4195 * Adds hours to the existing date object.
4196 * The hour is always a number.
4197 * Returned is the new date object
4198 * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
4200 * @param string|integer|array|Zend_Date $hour Hour to add
4201 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4202 * @return Zend_Date Provides fluid interface
4203 * @throws Zend_Date_Exception
4205 public function addHour($hour, $locale = null)
4207 return $this->_calcvalue('add', $hour, 'hour', self
::HOUR_SHORT
, $locale);
4212 * Subtracts hours from the existing date object.
4213 * The hour is always a number.
4214 * Returned is the new date object
4215 * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
4217 * @param string|integer|array|Zend_Date $hour Hour to sub
4218 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4219 * @return Zend_Date Provides fluid interface
4220 * @throws Zend_Date_Exception
4222 public function subHour($hour, $locale = null)
4224 return $this->_calcvalue('sub', $hour, 'hour', self
::HOUR_SHORT
, $locale);
4229 * Compares the hour with the existing date object.
4230 * For example: 10:30:25 -> compareHour(10) -> 0
4231 * Returns if equal, earlier or later
4233 * @param string|integer|array|Zend_Date $hour Hour to compare
4234 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4235 * @return integer 0 = equal, 1 = later, -1 = earlier
4236 * @throws Zend_Date_Exception
4238 public function compareHour($hour, $locale = null)
4240 return $this->_calcvalue('cmp', $hour, 'hour', self
::HOUR_SHORT
, $locale);
4245 * Returns the minute as new date object
4246 * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
4248 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4251 public function getMinute($locale = null)
4253 if (self
::$_options['format_type'] == 'php') {
4256 $format = self
::MINUTE
;
4259 return $this->copyPart($format, $locale);
4265 * The minute is always a number.
4266 * Returned is the new date object
4267 * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
4269 * @param string|integer|array|Zend_Date $minute Minute to set
4270 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4271 * @return Zend_Date Provides fluid interface
4272 * @throws Zend_Date_Exception
4274 public function setMinute($minute, $locale = null)
4276 return $this->_calcvalue('set', $minute, 'minute', self
::MINUTE_SHORT
, $locale);
4281 * Adds minutes to the existing date object.
4282 * The minute is always a number.
4283 * Returned is the new date object
4284 * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
4286 * @param string|integer|array|Zend_Date $minute Minute to add
4287 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4288 * @return Zend_Date Provides fluid interface
4289 * @throws Zend_Date_Exception
4291 public function addMinute($minute, $locale = null)
4293 return $this->_calcvalue('add', $minute, 'minute', self
::MINUTE_SHORT
, $locale);
4298 * Subtracts minutes from the existing date object.
4299 * The minute is always a number.
4300 * Returned is the new date object
4301 * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
4303 * @param string|integer|array|Zend_Date $minute Minute to sub
4304 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4305 * @return Zend_Date Provides fluid interface
4306 * @throws Zend_Date_Exception
4308 public function subMinute($minute, $locale = null)
4310 return $this->_calcvalue('sub', $minute, 'minute', self
::MINUTE_SHORT
, $locale);
4315 * Compares the minute with the existing date object.
4316 * For example: 10:30:25 -> compareMinute(30) -> 0
4317 * Returns if equal, earlier or later
4319 * @param string|integer|array|Zend_Date $minute Hour to compare
4320 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4321 * @return integer 0 = equal, 1 = later, -1 = earlier
4322 * @throws Zend_Date_Exception
4324 public function compareMinute($minute, $locale = null)
4326 return $this->_calcvalue('cmp', $minute, 'minute', self
::MINUTE_SHORT
, $locale);
4331 * Returns the second as new date object
4332 * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
4334 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4337 public function getSecond($locale = null)
4339 if (self
::$_options['format_type'] == 'php') {
4342 $format = self
::SECOND
;
4345 return $this->copyPart($format, $locale);
4350 * Sets new seconds to the existing date object.
4351 * The second is always a number.
4352 * Returned is the new date object
4353 * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
4355 * @param string|integer|array|Zend_Date $second Second to set
4356 * @param string|Zend_Locale $locale (Optional) Locale for parsing input
4357 * @return Zend_Date Provides fluid interface
4358 * @throws Zend_Date_Exception
4360 public function setSecond($second, $locale = null)
4362 return $this->_calcvalue('set', $second, 'second', self
::SECOND_SHORT
, $locale);
4367 * Adds seconds to the existing date object.
4368 * The second is always a number.
4369 * Returned is the new date object
4370 * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
4372 * @param string|integer|array|Zend_Date $second Second to add
4373 * @param string|Zend_Locale $locale (Optional) Locale for parsing input
4374 * @return Zend_Date Provides fluid interface
4375 * @throws Zend_Date_Exception
4377 public function addSecond($second, $locale = null)
4379 return $this->_calcvalue('add', $second, 'second', self
::SECOND_SHORT
, $locale);
4384 * Subtracts seconds from the existing date object.
4385 * The second is always a number.
4386 * Returned is the new date object
4387 * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
4389 * @param string|integer|array|Zend_Date $second Second to sub
4390 * @param string|Zend_Locale $locale (Optional) Locale for parsing input
4391 * @return Zend_Date Provides fluid interface
4392 * @throws Zend_Date_Exception
4394 public function subSecond($second, $locale = null)
4396 return $this->_calcvalue('sub', $second, 'second', self
::SECOND_SHORT
, $locale);
4401 * Compares the second with the existing date object.
4402 * For example: 10:30:25 -> compareSecond(25) -> 0
4403 * Returns if equal, earlier or later
4405 * @param string|integer|array|Zend_Date $second Second to compare
4406 * @param string|Zend_Locale $locale (Optional) Locale for parsing input
4407 * @return integer 0 = equal, 1 = later, -1 = earlier
4408 * @throws Zend_Date_Exception
4410 public function compareSecond($second, $locale = null)
4412 return $this->_calcvalue('cmp', $second, 'second', self
::SECOND_SHORT
, $locale);
4417 * Returns the precision for fractional seconds
4421 public function getFractionalPrecision()
4423 return $this->_precision
;
4428 * Sets a new precision for fractional seconds
4430 * @param integer $precision Precision for the fractional datepart 3 = milliseconds
4431 * @throws Zend_Date_Exception
4432 * @return Zend_Date Provides fluid interface
4434 public function setFractionalPrecision($precision)
4436 if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
4437 require_once 'Zend/Date/Exception.php';
4438 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4441 $this->_precision
= (int) $precision;
4442 if ($this->_precision
< strlen($this->_fractional
)) {
4443 $this->_fractional
= substr($this->_fractional
, 0, $this->_precision
);
4445 $this->_fractional
= str_pad($this->_fractional
, $this->_precision
, '0', STR_PAD_RIGHT
);
4453 * Returns the milliseconds of the date object
4457 public function getMilliSecond()
4459 return $this->_fractional
;
4464 * Sets new milliseconds for the date object
4465 * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
4467 * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
4468 * @param integer $precision (Optional) Fraction precision of the given milliseconds
4469 * @return Zend_Date Provides fluid interface
4471 public function setMilliSecond($milli = null, $precision = null)
4473 if ($milli === null) {
4474 list($milli, $time) = explode(" ", microtime());
4475 $milli = intval($milli);
4477 } else if (!is_numeric($milli)) {
4478 require_once 'Zend/Date/Exception.php';
4479 throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
4482 if ($precision === null) {
4483 $precision = $this->_precision
;
4486 if (!is_int($precision) ||
$precision < 1 ||
$precision > 9) {
4487 require_once 'Zend/Date/Exception.php';
4488 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4491 $this->_fractional
= 0;
4492 $this->addMilliSecond($milli, $precision);
4498 * Adds milliseconds to the date object
4500 * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
4501 * @param integer $precision (Optional) Fractional precision for the given milliseconds
4502 * @return Zend_Date Provides fluid interface
4504 public function addMilliSecond($milli = null, $precision = null)
4506 if ($milli === null) {
4507 list($milli, $time) = explode(" ", microtime());
4508 $milli = intval($milli);
4509 } else if (!is_numeric($milli)) {
4510 require_once 'Zend/Date/Exception.php';
4511 throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
4514 if ($precision === null) {
4515 $precision = strlen($milli);
4521 if (!is_int($precision) ||
$precision < 1 ||
$precision > 9) {
4522 require_once 'Zend/Date/Exception.php';
4523 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4526 $this->_fractional +
= $milli;
4528 // Add/sub milliseconds + add/sub seconds
4529 $max = pow(10, $this->_precision
);
4530 // Milli includes seconds
4531 if ($this->_fractional
>= $max) {
4532 while ($this->_fractional
>= $max) {
4533 $this->addSecond(1);
4534 $this->_fractional
-= $max;
4538 if ($this->_fractional
< 0) {
4539 while ($this->_fractional
< 0) {
4540 $this->subSecond(1);
4541 $this->_fractional +
= $max;
4545 if ($this->_precision
> strlen($this->_fractional
)) {
4546 $this->_fractional
= str_pad($this->_fractional
, $this->_precision
, '0', STR_PAD_LEFT
);
4554 * Subtracts a millisecond
4556 * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
4557 * @param integer $precision (Optional) Fractional precision for the given milliseconds
4558 * @return Zend_Date Provides fluid interface
4560 public function subMilliSecond($milli = null, $precision = null)
4562 $this->addMilliSecond(0 - $milli, $precision);
4567 * Compares only the millisecond part, returning the difference
4569 * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
4570 * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
4571 * @throws Zend_Date_Exception On invalid input
4572 * @return integer 0 = equal, 1 = later, -1 = earlier
4574 public function compareMilliSecond($milli = null, $precision = null)
4576 if ($milli === null) {
4577 list($milli, $time) = explode(" ", microtime());
4578 $milli = intval($milli);
4579 } else if (is_numeric($milli) === false) {
4580 require_once 'Zend/Date/Exception.php';
4581 throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
4584 if ($precision === null) {
4585 $precision = strlen($milli);
4586 } else if (!is_int($precision) ||
$precision < 1 ||
$precision > 9) {
4587 require_once 'Zend/Date/Exception.php';
4588 throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
4591 if ($precision === 0) {
4592 require_once 'Zend/Date/Exception.php';
4593 throw new Zend_Date_Exception('precision is 0');
4596 if ($precision != $this->_precision
) {
4597 if ($precision > $this->_precision
) {
4598 $diff = $precision - $this->_precision
;
4599 $milli = (int) ($milli / (10 * $diff));
4601 $diff = $this->_precision
- $precision;
4602 $milli = (int) ($milli * (10 * $diff));
4606 $comp = $this->_fractional
- $milli;
4609 } else if ($comp > 0) {
4616 * Returns the week as new date object using monday as begining of the week
4617 * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
4619 * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
4622 public function getWeek($locale = null)
4624 if (self
::$_options['format_type'] == 'php') {
4627 $format = self
::WEEK
;
4630 return $this->copyPart($format, $locale);
4634 * Sets a new week. The week is always a number. The day of week is not changed.
4635 * Returned is the new date object
4636 * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
4638 * @param string|integer|array|Zend_Date $week Week to set
4639 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4640 * @return Zend_Date Provides fluid interface
4641 * @throws Zend_Date_Exception
4643 public function setWeek($week, $locale = null)
4645 return $this->_calcvalue('set', $week, 'week', self
::WEEK
, $locale);
4649 * Adds a week. The week is always a number. The day of week is not changed.
4650 * Returned is the new date object
4651 * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
4653 * @param string|integer|array|Zend_Date $week Week to add
4654 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4655 * @return Zend_Date Provides fluid interface
4656 * @throws Zend_Date_Exception
4658 public function addWeek($week, $locale = null)
4660 return $this->_calcvalue('add', $week, 'week', self
::WEEK
, $locale);
4664 * Subtracts a week. The week is always a number. The day of week is not changed.
4665 * Returned is the new date object
4666 * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
4668 * @param string|integer|array|Zend_Date $week Week to sub
4669 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4670 * @return Zend_Date Provides fluid interface
4671 * @throws Zend_Date_Exception
4673 public function subWeek($week, $locale = null)
4675 return $this->_calcvalue('sub', $week, 'week', self
::WEEK
, $locale);
4679 * Compares only the week part, returning the difference
4680 * Returned is the new date object
4681 * Returns if equal, earlier or later
4682 * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
4684 * @param string|integer|array|Zend_Date $week Week to compare
4685 * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
4686 * @return integer 0 = equal, 1 = later, -1 = earlier
4688 public function compareWeek($week, $locale = null)
4690 return $this->_calcvalue('cmp', $week, 'week', self
::WEEK
, $locale);
4694 * Sets a new standard locale for the date object.
4695 * This locale will be used for all functions
4696 * Returned is the really set locale.
4697 * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
4698 * 'xx_YY' will be set to 'root' because 'xx' does not exist
4700 * @param string|Zend_Locale $locale (Optional) Locale for parsing input
4701 * @throws Zend_Date_Exception When the given locale does not exist
4702 * @return Zend_Date Provides fluent interface
4704 public function setLocale($locale = null)
4707 $this->_locale
= Zend_Locale
::findLocale($locale);
4708 } catch (Zend_Locale_Exception
$e) {
4709 require_once 'Zend/Date/Exception.php';
4710 throw new Zend_Date_Exception($e->getMessage(), 0, $e);
4717 * Returns the actual set locale
4721 public function getLocale()
4723 return $this->_locale
;
4727 * Checks if the given date is a real date or datepart.
4728 * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
4729 * But the check will only be done for the expected dateparts which are given by format.
4730 * If no format is given the standard dateformat for the actual locale is used.
4731 * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
4733 * @param string|array|Zend_Date $date Date to parse for correctness
4734 * @param string $format (Optional) Format for parsing the date string
4735 * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
4736 * @return boolean True when all date parts are correct
4738 public static function isDate($date, $format = null, $locale = null)
4740 if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date
) &&
4745 if (($format !== null) && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
4746 && (Zend_Locale
::isLocale($format, null, false))) {
4751 $locale = Zend_Locale
::findLocale($locale);
4753 if ($format === null) {
4754 $format = Zend_Locale_Format
::getDateFormat($locale);
4755 } else if ((self
::$_options['format_type'] == 'php') && !defined($format)) {
4756 $format = Zend_Locale_Format
::convertPhpToIsoFormat($format);
4759 $format = self
::_getLocalizedToken($format, $locale);
4760 if (!is_array($date)) {
4762 $parsed = Zend_Locale_Format
::getDate($date, array('locale' => $locale,
4763 'date_format' => $format, 'format_type' => 'iso',
4764 'fix_date' => false));
4765 } catch (Zend_Locale_Exception
$e) {
4766 // Date can not be parsed
4773 if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
4774 (!isset($parsed['year']))) {
4775 // Year expected but not found
4779 if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
4780 // Month expected but not found
4784 if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
4785 // Day expected but not found
4789 if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
4790 (!isset($parsed['hour']))) {
4791 // Hour expected but not found
4795 if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
4796 // Minute expected but not found
4800 if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
4801 // Second expected but not found
4805 // Set not given dateparts
4806 if (isset($parsed['hour']) === false) {
4807 $parsed['hour'] = 12;
4810 if (isset($parsed['minute']) === false) {
4811 $parsed['minute'] = 0;
4814 if (isset($parsed['second']) === false) {
4815 $parsed['second'] = 0;
4818 if (isset($parsed['month']) === false) {
4819 $parsed['month'] = 1;
4822 if (isset($parsed['day']) === false) {
4826 if (isset($parsed['year']) === false) {
4827 $parsed['year'] = 1970;
4830 if (self
::isYearLeapYear($parsed['year'])) {
4831 $parsed['year'] = 1972;
4833 $parsed['year'] = 1971;
4836 $date = new self($parsed, null, $locale);
4837 $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
4838 $parsed['month'], $parsed['day'], $parsed['year']);
4840 if ($parsed['year'] != $date->date('Y', $timestamp)) {
4841 // Given year differs from parsed year
4845 if ($parsed['month'] != $date->date('n', $timestamp)) {
4846 // Given month differs from parsed month
4850 if ($parsed['day'] != $date->date('j', $timestamp)) {
4851 // Given day differs from parsed day
4855 if ($parsed['hour'] != $date->date('G', $timestamp)) {
4856 // Given hour differs from parsed hour
4860 if ($parsed['minute'] != $date->date('i', $timestamp)) {
4861 // Given minute differs from parsed minute
4865 if ($parsed['second'] != $date->date('s', $timestamp)) {
4866 // Given second differs from parsed second
4874 * Returns the ISO Token for all localized constants
4876 * @param string $token Token to normalize
4877 * @param string $locale Locale to search
4880 protected static function _getLocalizedToken($token, $locale)
4883 case self
::ISO_8601
:
4884 return "yyyy-MM-ddThh:mm:ss";
4886 case self
::RFC_2822
:
4887 return "EEE, dd MMM yyyy HH:mm:ss";
4890 return Zend_Locale_Data
::getContent($locale, 'date');
4892 case self
::DATE_FULL
:
4893 return Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'full'));
4895 case self
::DATE_LONG
:
4896 return Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'long'));
4898 case self
::DATE_MEDIUM
:
4899 return Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'medium'));
4901 case self
::DATE_SHORT
:
4902 return Zend_Locale_Data
::getContent($locale, 'date', array('gregorian', 'short'));
4905 return Zend_Locale_Data
::getContent($locale, 'time');
4907 case self
::TIME_FULL
:
4908 return Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'full'));
4910 case self
::TIME_LONG
:
4911 return Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'long'));
4913 case self
::TIME_MEDIUM
:
4914 return Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'medium'));
4916 case self
::TIME_SHORT
:
4917 return Zend_Locale_Data
::getContent($locale, 'time', array('gregorian', 'short'));
4919 case self
::DATETIME
:
4920 return Zend_Locale_Data
::getContent($locale, 'datetime');
4922 case self
::DATETIME_FULL
:
4923 return Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'full'));
4925 case self
::DATETIME_LONG
:
4926 return Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'long'));
4928 case self
::DATETIME_MEDIUM
:
4929 return Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'medium'));
4931 case self
::DATETIME_SHORT
:
4932 return Zend_Locale_Data
::getContent($locale, 'datetime', array('gregorian', 'short'));
4935 case self
::RFC_3339
:
4937 return "yyyy-MM-DD HH:mm:ss";
4940 case self
::RFC_850
:
4941 return "EEEE, dd-MM-yyyy HH:mm:ss";
4943 case self
::RFC_822
:
4944 case self
::RFC_1036
:
4945 case self
::RFC_1123
:
4947 return "EEE, dd MM yyyy HH:mm:ss";