PageRenderTime 168ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/zend/Zend/Date.php

https://bitbucket.org/ngmares/moodle
PHP | 4953 lines | 3175 code | 503 blank | 1275 comment | 643 complexity | 5eda070fe35ef2b5eab930ae17162e23 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  16. * @package Zend_Date
  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
  19. * @version $Id$
  20. */
  21. /**
  22. * Include needed Date classes
  23. */
  24. require_once 'Zend/Date/DateObject.php';
  25. require_once 'Zend/Locale.php';
  26. require_once 'Zend/Locale/Format.php';
  27. require_once 'Zend/Locale/Math.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Date
  31. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Date extends Zend_Date_DateObject
  35. {
  36. private $_locale = null;
  37. // Fractional second variables
  38. private $_fractional = 0;
  39. private $_precision = 3;
  40. private static $_options = array(
  41. 'format_type' => 'iso', // format for date strings 'iso' or 'php'
  42. 'fix_dst' => true, // fix dst on summer/winter time change
  43. 'extend_month' => false, // false - addMonth like SQL, true like excel
  44. 'cache' => null, // cache to set
  45. 'timesync' => null // timesync server to set
  46. );
  47. // Class wide Date Constants
  48. const DAY = 'dd';
  49. const DAY_SHORT = 'd';
  50. const DAY_SUFFIX = 'SS';
  51. const DAY_OF_YEAR = 'D';
  52. const WEEKDAY = 'EEEE';
  53. const WEEKDAY_SHORT = 'EEE';
  54. const WEEKDAY_NARROW = 'E';
  55. const WEEKDAY_NAME = 'EE';
  56. const WEEKDAY_8601 = 'eee';
  57. const WEEKDAY_DIGIT = 'e';
  58. const WEEK = 'ww';
  59. const MONTH = 'MM';
  60. const MONTH_SHORT = 'M';
  61. const MONTH_DAYS = 'ddd';
  62. const MONTH_NAME = 'MMMM';
  63. const MONTH_NAME_SHORT = 'MMM';
  64. const MONTH_NAME_NARROW = 'MMMMM';
  65. const YEAR = 'y';
  66. const YEAR_SHORT = 'yy';
  67. const YEAR_8601 = 'Y';
  68. const YEAR_SHORT_8601 = 'YY';
  69. const LEAPYEAR = 'l';
  70. const MERIDIEM = 'a';
  71. const SWATCH = 'B';
  72. const HOUR = 'HH';
  73. const HOUR_SHORT = 'H';
  74. const HOUR_AM = 'hh';
  75. const HOUR_SHORT_AM = 'h';
  76. const MINUTE = 'mm';
  77. const MINUTE_SHORT = 'm';
  78. const SECOND = 'ss';
  79. const SECOND_SHORT = 's';
  80. const MILLISECOND = 'S';
  81. const TIMEZONE_NAME = 'zzzz';
  82. const DAYLIGHT = 'I';
  83. const GMT_DIFF = 'Z';
  84. const GMT_DIFF_SEP = 'ZZZZ';
  85. const TIMEZONE = 'z';
  86. const TIMEZONE_SECS = 'X';
  87. const ISO_8601 = 'c';
  88. const RFC_2822 = 'r';
  89. const TIMESTAMP = 'U';
  90. const ERA = 'G';
  91. const ERA_NAME = 'GGGG';
  92. const ERA_NARROW = 'GGGGG';
  93. const DATES = 'F';
  94. const DATE_FULL = 'FFFFF';
  95. const DATE_LONG = 'FFFF';
  96. const DATE_MEDIUM = 'FFF';
  97. const DATE_SHORT = 'FF';
  98. const TIMES = 'WW';
  99. const TIME_FULL = 'TTTTT';
  100. const TIME_LONG = 'TTTT';
  101. const TIME_MEDIUM = 'TTT';
  102. const TIME_SHORT = 'TT';
  103. const DATETIME = 'K';
  104. const DATETIME_FULL = 'KKKKK';
  105. const DATETIME_LONG = 'KKKK';
  106. const DATETIME_MEDIUM = 'KKK';
  107. const DATETIME_SHORT = 'KK';
  108. const ATOM = 'OOO';
  109. const COOKIE = 'CCC';
  110. const RFC_822 = 'R';
  111. const RFC_850 = 'RR';
  112. const RFC_1036 = 'RRR';
  113. const RFC_1123 = 'RRRR';
  114. const RFC_3339 = 'RRRRR';
  115. const RSS = 'SSS';
  116. const W3C = 'WWW';
  117. /**
  118. * Generates the standard date object, could be a unix timestamp, localized date,
  119. * string, integer, array and so on. Also parts of dates or time are supported
  120. * Always set the default timezone: http://php.net/date_default_timezone_set
  121. * For example, in your bootstrap: date_default_timezone_set('America/Los_Angeles');
  122. * For detailed instructions please look in the docu.
  123. *
  124. * @param string|integer|Zend_Date|array $date OPTIONAL Date value or value of date part to set
  125. * ,depending on $part. If null the actual time is set
  126. * @param string $part OPTIONAL Defines the input format of $date
  127. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  128. * @return Zend_Date
  129. * @throws Zend_Date_Exception
  130. */
  131. public function __construct($date = null, $part = null, $locale = null)
  132. {
  133. if (is_object($date) and !($date instanceof Zend_TimeSync_Protocol) and
  134. !($date instanceof Zend_Date)) {
  135. if ($locale instanceof Zend_Locale) {
  136. $locale = $date;
  137. $date = null;
  138. $part = null;
  139. } else {
  140. $date = (string) $date;
  141. }
  142. }
  143. if (($date !== null) and !is_array($date) and !($date instanceof Zend_TimeSync_Protocol) and
  144. !($date instanceof Zend_Date) and !defined($date) and Zend_Locale::isLocale($date, true, false)) {
  145. $locale = $date;
  146. $date = null;
  147. $part = null;
  148. } else if (($part !== null) and !defined($part) and Zend_Locale::isLocale($part, true, false)) {
  149. $locale = $part;
  150. $part = null;
  151. }
  152. $this->setLocale($locale);
  153. if (is_string($date) && ($part === null) && (strlen($date) <= 5)) {
  154. $part = $date;
  155. $date = null;
  156. }
  157. if ($date === null) {
  158. if ($part === null) {
  159. $date = time();
  160. } else if ($part !== self::TIMESTAMP) {
  161. $date = self::now($locale);
  162. $date = $date->get($part);
  163. }
  164. }
  165. if ($date instanceof Zend_TimeSync_Protocol) {
  166. $date = $date->getInfo();
  167. $date = $this->_getTime($date['offset']);
  168. $part = null;
  169. } else if (parent::$_defaultOffset != 0) {
  170. $date = $this->_getTime(parent::$_defaultOffset);
  171. }
  172. // set the timezone and offset for $this
  173. $zone = @date_default_timezone_get();
  174. $this->setTimezone($zone);
  175. // try to get timezone from date-string
  176. if (!is_int($date)) {
  177. $zone = $this->getTimezoneFromString($date);
  178. $this->setTimezone($zone);
  179. }
  180. // set datepart
  181. if (($part !== null && $part !== self::TIMESTAMP) or (!is_numeric($date))) {
  182. // switch off dst handling for value setting
  183. $this->setUnixTimestamp($this->getGmtOffset());
  184. $this->set($date, $part, $this->_locale);
  185. // DST fix
  186. if (is_array($date) === true) {
  187. if (!isset($date['hour'])) {
  188. $date['hour'] = 0;
  189. }
  190. $hour = $this->toString('H', 'iso', true);
  191. $hour = $date['hour'] - $hour;
  192. switch ($hour) {
  193. case 1 :
  194. case -23 :
  195. $this->addTimestamp(3600);
  196. break;
  197. case -1 :
  198. case 23 :
  199. $this->subTimestamp(3600);
  200. break;
  201. case 2 :
  202. case -22 :
  203. $this->addTimestamp(7200);
  204. break;
  205. case -2 :
  206. case 22 :
  207. $this->subTimestamp(7200);
  208. break;
  209. }
  210. }
  211. } else {
  212. $this->setUnixTimestamp($date);
  213. }
  214. }
  215. /**
  216. * Sets class wide options, if no option was given, the actual set options will be returned
  217. *
  218. * @param array $options Options to set
  219. * @throws Zend_Date_Exception
  220. * @return Options array if no option was given
  221. */
  222. public static function setOptions(array $options = array())
  223. {
  224. if (empty($options)) {
  225. return self::$_options;
  226. }
  227. foreach ($options as $name => $value) {
  228. $name = strtolower($name);
  229. if (array_key_exists($name, self::$_options)) {
  230. switch($name) {
  231. case 'format_type' :
  232. if ((strtolower($value) != 'php') && (strtolower($value) != 'iso')) {
  233. require_once 'Zend/Date/Exception.php';
  234. throw new Zend_Date_Exception("Unknown format type ($value) for dates, only 'iso' and 'php' supported", 0, null, $value);
  235. }
  236. break;
  237. case 'fix_dst' :
  238. if (!is_bool($value)) {
  239. require_once 'Zend/Date/Exception.php';
  240. throw new Zend_Date_Exception("'fix_dst' has to be boolean", 0, null, $value);
  241. }
  242. break;
  243. case 'extend_month' :
  244. if (!is_bool($value)) {
  245. require_once 'Zend/Date/Exception.php';
  246. throw new Zend_Date_Exception("'extend_month' has to be boolean", 0, null, $value);
  247. }
  248. break;
  249. case 'cache' :
  250. if ($value === null) {
  251. parent::$_cache = null;
  252. } else {
  253. if (!$value instanceof Zend_Cache_Core) {
  254. require_once 'Zend/Date/Exception.php';
  255. throw new Zend_Date_Exception("Instance of Zend_Cache expected");
  256. }
  257. parent::$_cache = $value;
  258. Zend_Locale_Data::setCache($value);
  259. }
  260. break;
  261. case 'timesync' :
  262. if ($value === null) {
  263. parent::$_defaultOffset = 0;
  264. } else {
  265. if (!$value instanceof Zend_TimeSync_Protocol) {
  266. require_once 'Zend/Date/Exception.php';
  267. throw new Zend_Date_Exception("Instance of Zend_TimeSync expected");
  268. }
  269. $date = $value->getInfo();
  270. parent::$_defaultOffset = $date['offset'];
  271. }
  272. break;
  273. }
  274. self::$_options[$name] = $value;
  275. }
  276. else {
  277. require_once 'Zend/Date/Exception.php';
  278. throw new Zend_Date_Exception("Unknown option: $name = $value");
  279. }
  280. }
  281. }
  282. /**
  283. * Returns this object's internal UNIX timestamp (equivalent to Zend_Date::TIMESTAMP).
  284. * If the timestamp is too large for integers, then the return value will be a string.
  285. * This function does not return the timestamp as an object.
  286. * Use clone() or copyPart() instead.
  287. *
  288. * @return integer|string UNIX timestamp
  289. */
  290. public function getTimestamp()
  291. {
  292. return $this->getUnixTimestamp();
  293. }
  294. /**
  295. * Returns the calculated timestamp
  296. * HINT: timestamps are always GMT
  297. *
  298. * @param string $calc Type of calculation to make
  299. * @param string|integer|array|Zend_Date $stamp Timestamp to calculate, when null the actual timestamp is calculated
  300. * @return Zend_Date|integer
  301. * @throws Zend_Date_Exception
  302. */
  303. private function _timestamp($calc, $stamp)
  304. {
  305. if ($stamp instanceof Zend_Date) {
  306. // extract timestamp from object
  307. $stamp = $stamp->getTimestamp();
  308. }
  309. if (is_array($stamp)) {
  310. if (isset($stamp['timestamp']) === true) {
  311. $stamp = $stamp['timestamp'];
  312. } else {
  313. require_once 'Zend/Date/Exception.php';
  314. throw new Zend_Date_Exception('no timestamp given in array');
  315. }
  316. }
  317. if ($calc === 'set') {
  318. $return = $this->setUnixTimestamp($stamp);
  319. } else {
  320. $return = $this->_calcdetail($calc, $stamp, self::TIMESTAMP, null);
  321. }
  322. if ($calc != 'cmp') {
  323. return $this;
  324. }
  325. return $return;
  326. }
  327. /**
  328. * Sets a new timestamp
  329. *
  330. * @param integer|string|array|Zend_Date $timestamp Timestamp to set
  331. * @return Zend_Date Provides fluid interface
  332. * @throws Zend_Date_Exception
  333. */
  334. public function setTimestamp($timestamp)
  335. {
  336. return $this->_timestamp('set', $timestamp);
  337. }
  338. /**
  339. * Adds a timestamp
  340. *
  341. * @param integer|string|array|Zend_Date $timestamp Timestamp to add
  342. * @return Zend_Date Provides fluid interface
  343. * @throws Zend_Date_Exception
  344. */
  345. public function addTimestamp($timestamp)
  346. {
  347. return $this->_timestamp('add', $timestamp);
  348. }
  349. /**
  350. * Subtracts a timestamp
  351. *
  352. * @param integer|string|array|Zend_Date $timestamp Timestamp to sub
  353. * @return Zend_Date Provides fluid interface
  354. * @throws Zend_Date_Exception
  355. */
  356. public function subTimestamp($timestamp)
  357. {
  358. return $this->_timestamp('sub', $timestamp);
  359. }
  360. /**
  361. * Compares two timestamps, returning the difference as integer
  362. *
  363. * @param integer|string|array|Zend_Date $timestamp Timestamp to compare
  364. * @return integer 0 = equal, 1 = later, -1 = earlier
  365. * @throws Zend_Date_Exception
  366. */
  367. public function compareTimestamp($timestamp)
  368. {
  369. return $this->_timestamp('cmp', $timestamp);
  370. }
  371. /**
  372. * Returns a string representation of the object
  373. * Supported format tokens are:
  374. * G - era, y - year, Y - ISO year, M - month, w - week of year, D - day of year, d - day of month
  375. * E - day of week, e - number of weekday (1-7), h - hour 1-12, H - hour 0-23, m - minute, s - second
  376. * A - milliseconds of day, z - timezone, Z - timezone offset, S - fractional second, a - period of day
  377. *
  378. * Additionally format tokens but non ISO conform are:
  379. * SS - day suffix, eee - php number of weekday(0-6), ddd - number of days per month
  380. * l - Leap year, B - swatch internet time, I - daylight saving time, X - timezone offset in seconds
  381. * r - RFC2822 format, U - unix timestamp
  382. *
  383. * Not supported ISO tokens are
  384. * u - extended year, Q - quarter, q - quarter, L - stand alone month, W - week of month
  385. * F - day of week of month, g - modified julian, c - stand alone weekday, k - hour 0-11, K - hour 1-24
  386. * v - wall zone
  387. *
  388. * @param string $format OPTIONAL Rule for formatting output. If null the default date format is used
  389. * @param string $type OPTIONAL Type for the format string which overrides the standard setting
  390. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  391. * @return string
  392. */
  393. public function toString($format = null, $type = null, $locale = null)
  394. {
  395. if (is_object($format)) {
  396. if ($format instanceof Zend_Locale) {
  397. $locale = $format;
  398. $format = null;
  399. } else {
  400. $format = (string) $format;
  401. }
  402. }
  403. if (is_object($type)) {
  404. if ($type instanceof Zend_Locale) {
  405. $locale = $type;
  406. $type = null;
  407. } else {
  408. $type = (string) $type;
  409. }
  410. }
  411. if (($format !== null) && !defined($format)
  412. && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
  413. && Zend_Locale::isLocale($format, null, false)) {
  414. $locale = $format;
  415. $format = null;
  416. }
  417. if (($type !== null) and ($type != 'php') and ($type != 'iso') and
  418. Zend_Locale::isLocale($type, null, false)) {
  419. $locale = $type;
  420. $type = null;
  421. }
  422. if ($locale === null) {
  423. $locale = $this->getLocale();
  424. }
  425. if ($format === null) {
  426. $format = Zend_Locale_Format::getDateFormat($locale) . ' ' . Zend_Locale_Format::getTimeFormat($locale);
  427. } else if (((self::$_options['format_type'] == 'php') && ($type === null)) or ($type == 'php')) {
  428. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  429. }
  430. return $this->date($this->_toToken($format, $locale), $this->getUnixTimestamp(), false);
  431. }
  432. /**
  433. * Returns a string representation of the date which is equal with the timestamp
  434. *
  435. * @return string
  436. */
  437. public function __toString()
  438. {
  439. return $this->toString(null, $this->_locale);
  440. }
  441. /**
  442. * Returns a integer representation of the object
  443. * But returns false when the given part is no value f.e. Month-Name
  444. *
  445. * @param string|integer|Zend_Date $part OPTIONAL Defines the date or datepart to return as integer
  446. * @return integer|false
  447. */
  448. public function toValue($part = null)
  449. {
  450. $result = $this->get($part);
  451. if (is_numeric($result)) {
  452. return intval("$result");
  453. } else {
  454. return false;
  455. }
  456. }
  457. /**
  458. * Returns an array representation of the object
  459. *
  460. * @return array
  461. */
  462. public function toArray()
  463. {
  464. return array('day' => $this->toString(self::DAY_SHORT, 'iso'),
  465. 'month' => $this->toString(self::MONTH_SHORT, 'iso'),
  466. 'year' => $this->toString(self::YEAR, 'iso'),
  467. 'hour' => $this->toString(self::HOUR_SHORT, 'iso'),
  468. 'minute' => $this->toString(self::MINUTE_SHORT, 'iso'),
  469. 'second' => $this->toString(self::SECOND_SHORT, 'iso'),
  470. 'timezone' => $this->toString(self::TIMEZONE, 'iso'),
  471. 'timestamp' => $this->toString(self::TIMESTAMP, 'iso'),
  472. 'weekday' => $this->toString(self::WEEKDAY_8601, 'iso'),
  473. 'dayofyear' => $this->toString(self::DAY_OF_YEAR, 'iso'),
  474. 'week' => $this->toString(self::WEEK, 'iso'),
  475. 'gmtsecs' => $this->toString(self::TIMEZONE_SECS, 'iso'));
  476. }
  477. /**
  478. * Returns a representation of a date or datepart
  479. * This could be for example a localized monthname, the time without date,
  480. * the era or only the fractional seconds. There are about 50 different supported date parts.
  481. * For a complete list of supported datepart values look into the docu
  482. *
  483. * @param string $part OPTIONAL Part of the date to return, if null the timestamp is returned
  484. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  485. * @return string date or datepart
  486. */
  487. public function get($part = null, $locale = null)
  488. {
  489. if ($locale === null) {
  490. $locale = $this->getLocale();
  491. }
  492. if (($part !== null) && !defined($part)
  493. && ($part != 'ee') && ($part != 'ss') && ($part != 'GG') && ($part != 'MM') && ($part != 'EE') && ($part != 'TT')
  494. && Zend_Locale::isLocale($part, null, false)) {
  495. $locale = $part;
  496. $part = null;
  497. }
  498. if ($part === null) {
  499. $part = self::TIMESTAMP;
  500. } else if (self::$_options['format_type'] == 'php') {
  501. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  502. }
  503. return $this->date($this->_toToken($part, $locale), $this->getUnixTimestamp(), false);
  504. }
  505. /**
  506. * Internal method to apply tokens
  507. *
  508. * @param string $part
  509. * @param string $locale
  510. * @return string
  511. */
  512. private function _toToken($part, $locale) {
  513. // get format tokens
  514. $comment = false;
  515. $format = '';
  516. $orig = '';
  517. for ($i = 0; $i < strlen($part); ++$i) {
  518. if ($part[$i] == "'") {
  519. $comment = $comment ? false : true;
  520. if (isset($part[$i+1]) && ($part[$i+1] == "'")) {
  521. $comment = $comment ? false : true;
  522. $format .= "\\'";
  523. ++$i;
  524. }
  525. $orig = '';
  526. continue;
  527. }
  528. if ($comment) {
  529. $format .= '\\' . $part[$i];
  530. $orig = '';
  531. } else {
  532. $orig .= $part[$i];
  533. if (!isset($part[$i+1]) || (isset($orig[0]) && ($orig[0] != $part[$i+1]))) {
  534. $format .= $this->_parseIsoToDate($orig, $locale);
  535. $orig = '';
  536. }
  537. }
  538. }
  539. return $format;
  540. }
  541. /**
  542. * Internal parsing method
  543. *
  544. * @param string $token
  545. * @param string $locale
  546. * @return string
  547. */
  548. private function _parseIsoToDate($token, $locale) {
  549. switch($token) {
  550. case self::DAY :
  551. return 'd';
  552. break;
  553. case self::WEEKDAY_SHORT :
  554. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  555. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday));
  556. return $this->_toComment(iconv_substr($day, 0, 3, 'UTF-8'));
  557. break;
  558. case self::DAY_SHORT :
  559. return 'j';
  560. break;
  561. case self::WEEKDAY :
  562. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  563. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'wide', $weekday)));
  564. break;
  565. case self::WEEKDAY_8601 :
  566. return 'N';
  567. break;
  568. case 'ee' :
  569. return $this->_toComment(str_pad($this->date('N', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  570. break;
  571. case self::DAY_SUFFIX :
  572. return 'S';
  573. break;
  574. case self::WEEKDAY_DIGIT :
  575. return 'w';
  576. break;
  577. case self::DAY_OF_YEAR :
  578. return 'z';
  579. break;
  580. case 'DDD' :
  581. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 3, '0', STR_PAD_LEFT));
  582. break;
  583. case 'DD' :
  584. return $this->_toComment(str_pad($this->date('z', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  585. break;
  586. case self::WEEKDAY_NARROW :
  587. case 'EEEEE' :
  588. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  589. $day = Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday));
  590. return $this->_toComment(iconv_substr($day, 0, 1, 'UTF-8'));
  591. break;
  592. case self::WEEKDAY_NAME :
  593. $weekday = strtolower($this->date('D', $this->getUnixTimestamp(), false));
  594. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'day', array('gregorian', 'format', 'abbreviated', $weekday)));
  595. break;
  596. case 'w' :
  597. $week = $this->date('W', $this->getUnixTimestamp(), false);
  598. return $this->_toComment(($week[0] == '0') ? $week[1] : $week);
  599. break;
  600. case self::WEEK :
  601. return 'W';
  602. break;
  603. case self::MONTH_NAME :
  604. $month = $this->date('n', $this->getUnixTimestamp(), false);
  605. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'wide', $month)));
  606. break;
  607. case self::MONTH :
  608. return 'm';
  609. break;
  610. case self::MONTH_NAME_SHORT :
  611. $month = $this->date('n', $this->getUnixTimestamp(), false);
  612. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month)));
  613. break;
  614. case self::MONTH_SHORT :
  615. return 'n';
  616. break;
  617. case self::MONTH_DAYS :
  618. return 't';
  619. break;
  620. case self::MONTH_NAME_NARROW :
  621. $month = $this->date('n', $this->getUnixTimestamp(), false);
  622. $mon = Zend_Locale_Data::getContent($locale, 'month', array('gregorian', 'format', 'abbreviated', $month));
  623. return $this->_toComment(iconv_substr($mon, 0, 1, 'UTF-8'));
  624. break;
  625. case self::LEAPYEAR :
  626. return 'L';
  627. break;
  628. case self::YEAR_8601 :
  629. return 'o';
  630. break;
  631. case self::YEAR :
  632. return 'Y';
  633. break;
  634. case self::YEAR_SHORT :
  635. return 'y';
  636. break;
  637. case self::YEAR_SHORT_8601 :
  638. return $this->_toComment(substr($this->date('o', $this->getUnixTimestamp(), false), -2, 2));
  639. break;
  640. case self::MERIDIEM :
  641. $am = $this->date('a', $this->getUnixTimestamp(), false);
  642. if ($am == 'am') {
  643. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'am'));
  644. }
  645. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'pm'));
  646. break;
  647. case self::SWATCH :
  648. return 'B';
  649. break;
  650. case self::HOUR_SHORT_AM :
  651. return 'g';
  652. break;
  653. case self::HOUR_SHORT :
  654. return 'G';
  655. break;
  656. case self::HOUR_AM :
  657. return 'h';
  658. break;
  659. case self::HOUR :
  660. return 'H';
  661. break;
  662. case self::MINUTE :
  663. return $this->_toComment(str_pad($this->date('i', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  664. break;
  665. case self::SECOND :
  666. return $this->_toComment(str_pad($this->date('s', $this->getUnixTimestamp(), false), 2, '0', STR_PAD_LEFT));
  667. break;
  668. case self::MINUTE_SHORT :
  669. return 'i';
  670. break;
  671. case self::SECOND_SHORT :
  672. return 's';
  673. break;
  674. case self::MILLISECOND :
  675. return $this->_toComment($this->getMilliSecond());
  676. break;
  677. case self::TIMEZONE_NAME :
  678. case 'vvvv' :
  679. return 'e';
  680. break;
  681. case self::DAYLIGHT :
  682. return 'I';
  683. break;
  684. case self::GMT_DIFF :
  685. case 'ZZ' :
  686. case 'ZZZ' :
  687. return 'O';
  688. break;
  689. case self::GMT_DIFF_SEP :
  690. return 'P';
  691. break;
  692. case self::TIMEZONE :
  693. case 'v' :
  694. case 'zz' :
  695. case 'zzz' :
  696. return 'T';
  697. break;
  698. case self::TIMEZONE_SECS :
  699. return 'Z';
  700. break;
  701. case self::ISO_8601 :
  702. return 'c';
  703. break;
  704. case self::RFC_2822 :
  705. return 'r';
  706. break;
  707. case self::TIMESTAMP :
  708. return 'U';
  709. break;
  710. case self::ERA :
  711. case 'GG' :
  712. case 'GGG' :
  713. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  714. if ($year < 0) {
  715. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')));
  716. }
  717. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')));
  718. break;
  719. case self::ERA_NARROW :
  720. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  721. if ($year < 0) {
  722. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '0')), 0, 1, 'UTF-8')) . '.';
  723. }
  724. return $this->_toComment(iconv_substr(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Abbr', '1')), 0, 1, 'UTF-8')) . '.';
  725. break;
  726. case self::ERA_NAME :
  727. $year = $this->date('Y', $this->getUnixTimestamp(), false);
  728. if ($year < 0) {
  729. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '0')));
  730. }
  731. return $this->_toComment(Zend_Locale_Data::getContent($locale, 'era', array('gregorian', 'Names', '1')));
  732. break;
  733. case self::DATES :
  734. return $this->_toToken(Zend_Locale_Format::getDateFormat($locale), $locale);
  735. break;
  736. case self::DATE_FULL :
  737. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full')), $locale);
  738. break;
  739. case self::DATE_LONG :
  740. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long')), $locale);
  741. break;
  742. case self::DATE_MEDIUM :
  743. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium')), $locale);
  744. break;
  745. case self::DATE_SHORT :
  746. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short')), $locale);
  747. break;
  748. case self::TIMES :
  749. return $this->_toToken(Zend_Locale_Format::getTimeFormat($locale), $locale);
  750. break;
  751. case self::TIME_FULL :
  752. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'full'), $locale);
  753. break;
  754. case self::TIME_LONG :
  755. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'long'), $locale);
  756. break;
  757. case self::TIME_MEDIUM :
  758. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'medium'), $locale);
  759. break;
  760. case self::TIME_SHORT :
  761. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'time', 'short'), $locale);
  762. break;
  763. case self::DATETIME :
  764. return $this->_toToken(Zend_Locale_Format::getDateTimeFormat($locale), $locale);
  765. break;
  766. case self::DATETIME_FULL :
  767. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full')), $locale);
  768. break;
  769. case self::DATETIME_LONG :
  770. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long')), $locale);
  771. break;
  772. case self::DATETIME_MEDIUM :
  773. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium')), $locale);
  774. break;
  775. case self::DATETIME_SHORT :
  776. return $this->_toToken(Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short')), $locale);
  777. break;
  778. case self::ATOM :
  779. return 'Y\-m\-d\TH\:i\:sP';
  780. break;
  781. case self::COOKIE :
  782. return 'l\, d\-M\-y H\:i\:s e';
  783. break;
  784. case self::RFC_822 :
  785. return 'D\, d M y H\:i\:s O';
  786. break;
  787. case self::RFC_850 :
  788. return 'l\, d\-M\-y H\:i\:s e';
  789. break;
  790. case self::RFC_1036 :
  791. return 'D\, d M y H\:i\:s O';
  792. break;
  793. case self::RFC_1123 :
  794. return 'D\, d M Y H\:i\:s O';
  795. break;
  796. case self::RFC_3339 :
  797. return 'Y\-m\-d\TH\:i\:sP';
  798. break;
  799. case self::RSS :
  800. return 'D\, d M Y H\:i\:s O';
  801. break;
  802. case self::W3C :
  803. return 'Y\-m\-d\TH\:i\:sP';
  804. break;
  805. }
  806. if ($token == '') {
  807. return '';
  808. }
  809. switch ($token[0]) {
  810. case 'y' :
  811. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  812. return 'Y';
  813. }
  814. $length = iconv_strlen($token, 'UTF-8');
  815. return $this->_toComment(str_pad($this->date('Y', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  816. break;
  817. case 'Y' :
  818. if ((strlen($token) == 4) && (abs($this->getUnixTimestamp()) <= 0x7FFFFFFF)) {
  819. return 'o';
  820. }
  821. $length = iconv_strlen($token, 'UTF-8');
  822. return $this->_toComment(str_pad($this->date('o', $this->getUnixTimestamp(), false), $length, '0', STR_PAD_LEFT));
  823. break;
  824. case 'A' :
  825. $length = iconv_strlen($token, 'UTF-8');
  826. $result = substr($this->getMilliSecond(), 0, 3);
  827. $result += $this->date('s', $this->getUnixTimestamp(), false) * 1000;
  828. $result += $this->date('i', $this->getUnixTimestamp(), false) * 60000;
  829. $result += $this->date('H', $this->getUnixTimestamp(), false) * 3600000;
  830. return $this->_toComment(str_pad($result, $length, '0', STR_PAD_LEFT));
  831. break;
  832. }
  833. return $this->_toComment($token);
  834. }
  835. /**
  836. * Private function to make a comment of a token
  837. *
  838. * @param string $token
  839. * @return string
  840. */
  841. private function _toComment($token)
  842. {
  843. $token = str_split($token);
  844. $result = '';
  845. foreach ($token as $tok) {
  846. $result .= '\\' . $tok;
  847. }
  848. return $result;
  849. }
  850. /**
  851. * Return digit from standard names (english)
  852. * Faster implementation than locale aware searching
  853. *
  854. * @param string $name
  855. * @return integer Number of this month
  856. * @throws Zend_Date_Exception
  857. */
  858. private function _getDigitFromName($name)
  859. {
  860. switch($name) {
  861. case "Jan":
  862. return 1;
  863. case "Feb":
  864. return 2;
  865. case "Mar":
  866. return 3;
  867. case "Apr":
  868. return 4;
  869. case "May":
  870. return 5;
  871. case "Jun":
  872. return 6;
  873. case "Jul":
  874. return 7;
  875. case "Aug":
  876. return 8;
  877. case "Sep":
  878. return 9;
  879. case "Oct":
  880. return 10;
  881. case "Nov":
  882. return 11;
  883. case "Dec":
  884. return 12;
  885. default:
  886. require_once 'Zend/Date/Exception.php';
  887. throw new Zend_Date_Exception('Month ($name) is not a known month');
  888. }
  889. }
  890. /**
  891. * Counts the exact year number
  892. * < 70 - 2000 added, >70 < 100 - 1900, others just returned
  893. *
  894. * @param integer $value year number
  895. * @return integer Number of year
  896. */
  897. public static function getFullYear($value)
  898. {
  899. if ($value >= 0) {
  900. if ($value < 70) {
  901. $value += 2000;
  902. } else if ($value < 100) {
  903. $value += 1900;
  904. }
  905. }
  906. return $value;
  907. }
  908. /**
  909. * Sets the given date as new date or a given datepart as new datepart returning the new datepart
  910. * This could be for example a localized dayname, the date without time,
  911. * the month or only the seconds. There are about 50 different supported date parts.
  912. * For a complete list of supported datepart values look into the docu
  913. *
  914. * @param string|integer|array|Zend_Date $date Date or datepart to set
  915. * @param string $part OPTIONAL Part of the date to set, if null the timestamp is set
  916. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  917. * @return Zend_Date Provides fluid interface
  918. * @throws Zend_Date_Exception
  919. */
  920. public function set($date, $part = null, $locale = null)
  921. {
  922. if (self::$_options['format_type'] == 'php') {
  923. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  924. }
  925. $zone = $this->getTimezoneFromString($date);
  926. $this->setTimezone($zone);
  927. $this->_calculate('set', $date, $part, $locale);
  928. return $this;
  929. }
  930. /**
  931. * Adds a date or datepart to the existing date, by extracting $part from $date,
  932. * and modifying this object by adding that part. The $part is then extracted from
  933. * this object and returned as an integer or numeric string (for large values, or $part's
  934. * corresponding to pre-defined formatted date strings).
  935. * This could be for example a ISO 8601 date, the hour the monthname or only the minute.
  936. * There are about 50 different supported date parts.
  937. * For a complete list of supported datepart values look into the docu.
  938. *
  939. * @param string|integer|array|Zend_Date $date Date or datepart to add
  940. * @param string $part OPTIONAL Part of the date to add, if null the timestamp is added
  941. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  942. * @return Zend_Date Provides fluid interface
  943. * @throws Zend_Date_Exception
  944. */
  945. public function add($date, $part = self::TIMESTAMP, $locale = null)
  946. {
  947. if (self::$_options['format_type'] == 'php') {
  948. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  949. }
  950. $this->_calculate('add', $date, $part, $locale);
  951. return $this;
  952. }
  953. /**
  954. * Subtracts a date from another date.
  955. * This could be for example a RFC2822 date, the time,
  956. * the year or only the timestamp. There are about 50 different supported date parts.
  957. * For a complete list of supported datepart values look into the docu
  958. * Be aware: Adding -2 Months is not equal to Subtracting 2 Months !!!
  959. *
  960. * @param string|integer|array|Zend_Date $date Date or datepart to subtract
  961. * @param string $part OPTIONAL Part of the date to sub, if null the timestamp is subtracted
  962. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  963. * @return Zend_Date Provides fluid interface
  964. * @throws Zend_Date_Exception
  965. */
  966. public function sub($date, $part = self::TIMESTAMP, $locale = null)
  967. {
  968. if (self::$_options['format_type'] == 'php') {
  969. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  970. }
  971. $this->_calculate('sub', $date, $part, $locale);
  972. return $this;
  973. }
  974. /**
  975. * Compares a date or datepart with the existing one.
  976. * Returns -1 if earlier, 0 if equal and 1 if later.
  977. *
  978. * @param string|integer|array|Zend_Date $date Date or datepart to compare with the date object
  979. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is subtracted
  980. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  981. * @return integer 0 = equal, 1 = later, -1 = earlier
  982. * @throws Zend_Date_Exception
  983. */
  984. public function compare($date, $part = self::TIMESTAMP, $locale = null)
  985. {
  986. if (self::$_options['format_type'] == 'php') {
  987. $part = Zend_Locale_Format::convertPhpToIsoFormat($part);
  988. }
  989. $compare = $this->_calculate('cmp', $date, $part, $locale);
  990. if ($compare > 0) {
  991. return 1;
  992. } else if ($compare < 0) {
  993. return -1;
  994. }
  995. return 0;
  996. }
  997. /**
  998. * Returns a new instance of Zend_Date with the selected part copied.
  999. * To make an exact copy, use PHP's clone keyword.
  1000. * For a complete list of supported date part values look into the docu.
  1001. * If a date part is copied, all other date parts are set to standard values.
  1002. * For example: If only YEAR is copied, the returned date object is equal to
  1003. * 01-01-YEAR 00:00:00 (01-01-1970 00:00:00 is equal to timestamp 0)
  1004. * If only HOUR is copied, the returned date object is equal to
  1005. * 01-01-1970 HOUR:00:00 (so $this contains a timestamp equal to a timestamp of 0 plus HOUR).
  1006. *
  1007. * @param string $part Part of the date to compare, if null the timestamp is subtracted
  1008. * @param string|Zend_Locale $locale OPTIONAL New object's locale. No adjustments to timezone are made.
  1009. * @return Zend_Date New clone with requested part
  1010. */
  1011. public function copyPart($part, $locale = null)
  1012. {
  1013. $clone = clone $this; // copy all instance variables
  1014. $clone->setUnixTimestamp(0); // except the timestamp
  1015. if ($locale != null) {
  1016. $clone->setLocale($locale); // set an other locale if selected
  1017. }
  1018. $clone->set($this, $part);
  1019. return $clone;
  1020. }
  1021. /**
  1022. * Internal function, returns the offset of a given timezone
  1023. *
  1024. * @param string $zone
  1025. * @return integer
  1026. */
  1027. public function getTimezoneFromString($zone)
  1028. {
  1029. if (is_array($zone)) {
  1030. return $this->getTimezone();
  1031. }
  1032. if ($zone instanceof Zend_Date) {
  1033. return $zone->getTimezone();
  1034. }
  1035. $match = array();
  1036. preg_match('/\dZ$/', $zone, $match);
  1037. if (!empty($match)) {
  1038. return "Etc/UTC";
  1039. }
  1040. preg_match('/([+-]\d{2}):{0,1}\d{2}/', $zone, $match);
  1041. if (!empty($match) and ($match[count($match) - 1] <= 12) and ($match[count($match) - 1] >= -12)) {
  1042. $zone = "Etc/GMT";
  1043. $zone .= ($match[count($match) - 1] < 0) ? "+" : "-";
  1044. $zone .= (int) abs($match[count($match) - 1]);
  1045. return $zone;
  1046. }
  1047. preg_match('/([[:alpha:]\/]{3,30})(?!.*([[:alpha:]\/]{3,30}))/', $zone, $match);
  1048. try {
  1049. if (!empty($match) and (!is_int($match[count($match) - 1]))) {
  1050. $oldzone = $this->getTimezone();
  1051. $this->setTimezone($match[count($match) - 1]);
  1052. $result = $this->getTimezone();
  1053. $this->setTimezone($oldzone);
  1054. if ($result !== $oldzone) {
  1055. return $match[count($match) - 1];
  1056. }
  1057. }
  1058. } catch (Exception $e) {
  1059. // fall through
  1060. }
  1061. return $this->getTimezone();
  1062. }
  1063. /**
  1064. * Calculates the date or object
  1065. *
  1066. * @param string $calc Calculation to make
  1067. * @param string|integer $date Date for calculation
  1068. * @param string|integer $comp Second date for calculation
  1069. * @param boolean|integer $dst Use dst correction if option is set
  1070. * @return integer|string|Zend_Date new timestamp or Zend_Date depending on calculation
  1071. */
  1072. private function _assign($calc, $date, $comp = 0, $dst = false)
  1073. {
  1074. switch ($calc) {
  1075. case 'set' :
  1076. if (!empty($comp)) {
  1077. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $comp));
  1078. }
  1079. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1080. $value = $this->getUnixTimestamp();
  1081. break;
  1082. case 'add' :
  1083. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$add, $this->getUnixTimestamp(), $date));
  1084. $value = $this->getUnixTimestamp();
  1085. break;
  1086. case 'sub' :
  1087. $this->setUnixTimestamp(call_user_func(Zend_Locale_Math::$sub, $this->getUnixTimestamp(), $date));
  1088. $value = $this->getUnixTimestamp();
  1089. break;
  1090. default :
  1091. // cmp - compare
  1092. return call_user_func(Zend_Locale_Math::$comp, $comp, $date);
  1093. break;
  1094. }
  1095. // dst-correction if 'fix_dst' = true and dst !== false but only for non UTC and non GMT
  1096. if ((self::$_options['fix_dst'] === true) and ($dst !== false) and ($this->_dst === true)) {
  1097. $hour = $this->toString(self::HOUR, 'iso');
  1098. if ($hour != $dst) {
  1099. if (($dst == ($hour + 1)) or ($dst == ($hour - 23))) {
  1100. $value += 3600;
  1101. } else if (($dst == ($hour - 1)) or ($dst == ($hour + 23))) {
  1102. $value -= 3600;
  1103. }
  1104. $this->setUnixTimestamp($value);
  1105. }
  1106. }
  1107. return $this->getUnixTimestamp();
  1108. }
  1109. /**
  1110. * Calculates the date or object
  1111. *
  1112. * @param string $calc Calculation to make, one of: 'add'|'sub'|'cmp'|'copy'|'set'
  1113. * @param string|integer|array|Zend_Date $date Date or datepart to calculate with
  1114. * @param string $part Part of the date to calculate, if null the timestamp is used
  1115. * @param string|Zend_Locale $locale Locale for parsing input
  1116. * @return integer|string|Zend_Date new timestamp
  1117. * @throws Zend_Date_Exception
  1118. */
  1119. private function _calculate($calc, $date, $part, $locale)
  1120. {
  1121. if ($date === null) {
  1122. require_once 'Zend/Date/Exception.php';
  1123. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  1124. }
  1125. if (($part !== null) && (strlen($part) !== 2) && (Zend_Locale::isLocale($part, null, false))) {
  1126. $locale = $part;
  1127. $part = null;
  1128. }
  1129. if ($locale === null) {
  1130. $locale = $this->getLocale();
  1131. }
  1132. $locale = (string) $locale;
  1133. // Create date parts
  1134. $year = $this->toString(self::YEAR, 'iso');
  1135. $month = $this->toString(self::MONTH_SHORT, 'iso');
  1136. $day = $this->toString(self::DAY_SHORT, 'iso');
  1137. $hour = $this->toString(self::HOUR_SHORT, 'iso');
  1138. $minute = $this->toString(self::MINUTE_SHORT, 'iso');
  1139. $second = $this->toString(self::SECOND_SHORT, 'iso');
  1140. // If object extract value
  1141. if ($date instanceof Zend_Date) {
  1142. $date = $date->toString($part, 'iso', $locale);
  1143. }
  1144. if (is_array($date) === true) {
  1145. if (empty($part) === false) {
  1146. switch($part) {
  1147. // Fall through
  1148. case self::DAY:
  1149. case self::DAY_SHORT:
  1150. if (isset($date['day']) === true) {
  1151. $date = $date['day'];
  1152. }
  1153. break;
  1154. // Fall through
  1155. case self::WEEKDAY_SHORT:
  1156. case self::WEEKDAY:
  1157. case self::WEEKDAY_8601:
  1158. case self::WEEKDAY_DIGIT:
  1159. case self::WEEKDAY_NARROW:
  1160. case self::WEEKDAY_NAME:
  1161. if (isset($date['weekday']) === true) {
  1162. $date = $date['weekday'];
  1163. $part = self::WEEKDAY_DIGIT;
  1164. }
  1165. break;
  1166. case self::DAY_OF_YEAR:
  1167. if (isset($date['day_of_year']) === true) {
  1168. $date = $date['day_of_year'];
  1169. }
  1170. break;
  1171. // Fall through
  1172. case self::MONTH:
  1173. case self::MONTH_SHORT:
  1174. case self::MONTH_NAME:
  1175. case self::MONTH_NAME_SHORT:
  1176. case self::MONTH_NAME_NARROW:
  1177. if (isset($date['month']) === true) {
  1178. $date = $date['month'];
  1179. }
  1180. break;
  1181. // Fall through
  1182. case self::YEAR:
  1183. case self::YEAR_SHORT:
  1184. case self::YEAR_8601:
  1185. case self::YEAR_SHORT_8601:
  1186. if (isset($date['year']) === true) {
  1187. $date = $date['year'];
  1188. }
  1189. break;
  1190. // Fall through
  1191. case self::HOUR:
  1192. case self::HOUR_AM:
  1193. case self::HOUR_SHORT:
  1194. case self::HOUR_SHORT_AM:
  1195. if (isset($date['hour']) === true) {
  1196. $date = $date['hour'];
  1197. }
  1198. break;
  1199. // Fall through
  1200. case self::MINUTE:
  1201. case self::MINUTE_SHORT:
  1202. if (isset($date['minute']) === true) {
  1203. $date = $date['minute'];
  1204. }
  1205. break;
  1206. // Fall through
  1207. case self::SECOND:
  1208. case self::SECOND_SHORT:
  1209. if (isset($date['second']) === true) {
  1210. $date = $date['second'];
  1211. }
  1212. break;
  1213. // Fall through
  1214. case self::TIMEZONE:
  1215. case self::TIMEZONE_NAME:
  1216. if (isset($date['timezone']) === true) {
  1217. $date = $date['timezone'];
  1218. }
  1219. break;
  1220. case self::TIMESTAMP:
  1221. if (isset($date['timestamp']) === true) {
  1222. $date = $date['timestamp'];
  1223. }
  1224. break;
  1225. case self::WEEK:
  1226. if (isset($date['week']) === true) {
  1227. $date = $date['week'];
  1228. }
  1229. break;
  1230. case self::TIMEZONE_SECS:
  1231. if (isset($date['gmtsecs']) === true) {
  1232. $date = $date['gmtsecs'];
  1233. }
  1234. break;
  1235. default:
  1236. require_once 'Zend/Date/Exception.php';
  1237. throw new Zend_Date_Exception("datepart for part ($part) not found in array");
  1238. break;
  1239. }
  1240. } else {
  1241. $hours = 0;
  1242. if (isset($date['hour']) === true) {
  1243. $hours = $date['hour'];
  1244. }
  1245. $minutes = 0;
  1246. if (isset($date['minute']) === true) {
  1247. $minutes = $date['minute'];
  1248. }
  1249. $seconds = 0;
  1250. if (isset($date['second']) === true) {
  1251. $seconds = $date['second'];
  1252. }
  1253. $months = 0;
  1254. if (isset($date['month']) === true) {
  1255. $months = $date['month'];
  1256. }
  1257. $days = 0;
  1258. if (isset($date['day']) === true) {
  1259. $days = $date['day'];
  1260. }
  1261. $years = 0;
  1262. if (isset($date['year']) === true) {
  1263. $years = $date['year'];
  1264. }
  1265. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, $months, $days, $years, true),
  1266. $this->mktime($hour, $minute, $second, $month, $day, $year, true), $hour);
  1267. }
  1268. }
  1269. // $date as object, part of foreign date as own date
  1270. switch($part) {
  1271. // day formats
  1272. case self::DAY:
  1273. if (is_numeric($date)) {
  1274. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1275. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1276. }
  1277. require_once 'Zend/Date/Exception.php';
  1278. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
  1279. break;
  1280. case self::WEEKDAY_SHORT:
  1281. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1282. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1283. $cnt = 0;
  1284. foreach ($daylist as $key => $value) {
  1285. if (strtoupper(iconv_substr($value, 0, 3, 'UTF-8')) == strtoupper($date)) {
  1286. $found = $cnt;
  1287. break;
  1288. }
  1289. ++$cnt;
  1290. }
  1291. // Weekday found
  1292. if ($cnt < 7) {
  1293. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1294. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1295. }
  1296. // Weekday not found
  1297. require_once 'Zend/Date/Exception.php';
  1298. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1299. break;
  1300. case self::DAY_SHORT:
  1301. if (is_numeric($date)) {
  1302. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1303. $this->mktime(0, 0, 0, 1, 1 + intval($day), 1970, true), $hour);
  1304. }
  1305. require_once 'Zend/Date/Exception.php';
  1306. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
  1307. break;
  1308. case self::WEEKDAY:
  1309. $daylist = Zend_Locale_Data::getList($locale, 'day');
  1310. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1311. $cnt = 0;
  1312. foreach ($daylist as $key => $value) {
  1313. if (strtoupper($value) == strtoupper($date)) {
  1314. $found = $cnt;
  1315. break;
  1316. }
  1317. ++$cnt;
  1318. }
  1319. // Weekday found
  1320. if ($cnt < 7) {
  1321. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1322. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1323. }
  1324. // Weekday not found
  1325. require_once 'Zend/Date/Exception.php';
  1326. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1327. break;
  1328. case self::WEEKDAY_8601:
  1329. $weekday = (int) $this->toString(self::WEEKDAY_8601, 'iso', $locale);
  1330. if ((intval($date) > 0) and (intval($date) < 8)) {
  1331. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + intval($date), 1970, true),
  1332. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1333. }
  1334. // Weekday not found
  1335. require_once 'Zend/Date/Exception.php';
  1336. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1337. break;
  1338. case self::DAY_SUFFIX:
  1339. require_once 'Zend/Date/Exception.php';
  1340. throw new Zend_Date_Exception('day suffix not supported', 0, null, $date);
  1341. break;
  1342. case self::WEEKDAY_DIGIT:
  1343. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1344. if (is_numeric($date) and (intval($date) >= 0) and (intval($date) < 7)) {
  1345. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $date, 1970, true),
  1346. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1347. }
  1348. // Weekday not found
  1349. require_once 'Zend/Date/Exception.php';
  1350. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1351. break;
  1352. case self::DAY_OF_YEAR:
  1353. if (is_numeric($date)) {
  1354. if (($calc == 'add') || ($calc == 'sub')) {
  1355. $year = 1970;
  1356. ++$date;
  1357. ++$day;
  1358. }
  1359. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, $date, $year, true),
  1360. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1361. }
  1362. require_once 'Zend/Date/Exception.php';
  1363. throw new Zend_Date_Exception("invalid date ($date) operand, day expected", 0, null, $date);
  1364. break;
  1365. case self::WEEKDAY_NARROW:
  1366. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1367. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1368. $cnt = 0;
  1369. foreach ($daylist as $key => $value) {
  1370. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($date)) {
  1371. $found = $cnt;
  1372. break;
  1373. }
  1374. ++$cnt;
  1375. }
  1376. // Weekday found
  1377. if ($cnt < 7) {
  1378. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1379. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1380. }
  1381. // Weekday not found
  1382. require_once 'Zend/Date/Exception.php';
  1383. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1384. break;
  1385. case self::WEEKDAY_NAME:
  1386. $daylist = Zend_Locale_Data::getList($locale, 'day', array('gregorian', 'format', 'abbreviated'));
  1387. $weekday = (int) $this->toString(self::WEEKDAY_DIGIT, 'iso', $locale);
  1388. $cnt = 0;
  1389. foreach ($daylist as $key => $value) {
  1390. if (strtoupper($value) == strtoupper($date)) {
  1391. $found = $cnt;
  1392. break;
  1393. }
  1394. ++$cnt;
  1395. }
  1396. // Weekday found
  1397. if ($cnt < 7) {
  1398. return $this->_assign($calc, $this->mktime(0, 0, 0, 1, 1 + $found, 1970, true),
  1399. $this->mktime(0, 0, 0, 1, 1 + $weekday, 1970, true), $hour);
  1400. }
  1401. // Weekday not found
  1402. require_once 'Zend/Date/Exception.php';
  1403. throw new Zend_Date_Exception("invalid date ($date) operand, weekday expected", 0, null, $date);
  1404. break;
  1405. // week formats
  1406. case self::WEEK:
  1407. if (is_numeric($date)) {
  1408. $week = (int) $this->toString(self::WEEK, 'iso', $locale);
  1409. return $this->_assign($calc, parent::mktime(0, 0, 0, 1, 1 + ($date * 7), 1970, true),
  1410. parent::mktime(0, 0, 0, 1, 1 + ($week * 7), 1970, true), $hour);
  1411. }
  1412. require_once 'Zend/Date/Exception.php';
  1413. throw new Zend_Date_Exception("invalid date ($date) operand, week expected", 0, null, $date);
  1414. break;
  1415. // month formats
  1416. case self::MONTH_NAME:
  1417. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  1418. $cnt = 0;
  1419. foreach ($monthlist as $key => $value) {
  1420. if (strtoupper($value) == strtoupper($date)) {
  1421. $found = $key;
  1422. break;
  1423. }
  1424. ++$cnt;
  1425. }
  1426. $date = array_search($date, $monthlist);
  1427. // Monthname found
  1428. if ($cnt < 12) {
  1429. $fixday = 0;
  1430. if ($calc == 'add') {
  1431. $date += $found;
  1432. $calc = 'set';
  1433. if (self::$_options['extend_month'] == false) {
  1434. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1435. if ($parts['mday'] != $day) {
  1436. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1437. }
  1438. }
  1439. } else if ($calc == 'sub') {
  1440. $date = $month - $found;
  1441. $calc = 'set';
  1442. if (self::$_options['extend_month'] == false) {
  1443. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1444. if ($parts['mday'] != $day) {
  1445. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1446. }
  1447. }
  1448. }
  1449. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1450. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1451. }
  1452. // Monthname not found
  1453. require_once 'Zend/Date/Exception.php';
  1454. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1455. break;
  1456. case self::MONTH:
  1457. if (is_numeric($date)) {
  1458. $fixday = 0;
  1459. if ($calc == 'add') {
  1460. $date += $month;
  1461. $calc = 'set';
  1462. if (self::$_options['extend_month'] == false) {
  1463. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1464. if ($parts['mday'] != $day) {
  1465. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1466. }
  1467. }
  1468. } else if ($calc == 'sub') {
  1469. $date = $month - $date;
  1470. $calc = 'set';
  1471. if (self::$_options['extend_month'] == false) {
  1472. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1473. if ($parts['mday'] != $day) {
  1474. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1475. }
  1476. }
  1477. }
  1478. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1479. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1480. }
  1481. require_once 'Zend/Date/Exception.php';
  1482. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1483. break;
  1484. case self::MONTH_NAME_SHORT:
  1485. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  1486. $cnt = 0;
  1487. foreach ($monthlist as $key => $value) {
  1488. if (strtoupper($value) == strtoupper($date)) {
  1489. $found = $key;
  1490. break;
  1491. }
  1492. ++$cnt;
  1493. }
  1494. $date = array_search($date, $monthlist);
  1495. // Monthname found
  1496. if ($cnt < 12) {
  1497. $fixday = 0;
  1498. if ($calc == 'add') {
  1499. $date += $found;
  1500. $calc = 'set';
  1501. if (self::$_options['extend_month'] === false) {
  1502. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1503. if ($parts['mday'] != $day) {
  1504. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1505. }
  1506. }
  1507. } else if ($calc == 'sub') {
  1508. $date = $month - $found;
  1509. $calc = 'set';
  1510. if (self::$_options['extend_month'] === false) {
  1511. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1512. if ($parts['mday'] != $day) {
  1513. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1514. }
  1515. }
  1516. }
  1517. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1518. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1519. }
  1520. // Monthname not found
  1521. require_once 'Zend/Date/Exception.php';
  1522. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1523. break;
  1524. case self::MONTH_SHORT:
  1525. if (is_numeric($date) === true) {
  1526. $fixday = 0;
  1527. if ($calc === 'add') {
  1528. $date += $month;
  1529. $calc = 'set';
  1530. if (self::$_options['extend_month'] === false) {
  1531. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1532. if ($parts['mday'] != $day) {
  1533. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1534. }
  1535. }
  1536. } else if ($calc === 'sub') {
  1537. $date = $month - $date;
  1538. $calc = 'set';
  1539. if (self::$_options['extend_month'] === false) {
  1540. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1541. if ($parts['mday'] != $day) {
  1542. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1543. }
  1544. }
  1545. }
  1546. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1547. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1548. }
  1549. require_once 'Zend/Date/Exception.php';
  1550. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1551. break;
  1552. case self::MONTH_DAYS:
  1553. require_once 'Zend/Date/Exception.php';
  1554. throw new Zend_Date_Exception('month days not supported', 0, null, $date);
  1555. break;
  1556. case self::MONTH_NAME_NARROW:
  1557. $monthlist = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'stand-alone', 'narrow'));
  1558. $cnt = 0;
  1559. foreach ($monthlist as $key => $value) {
  1560. if (strtoupper($value) === strtoupper($date)) {
  1561. $found = $key;
  1562. break;
  1563. }
  1564. ++$cnt;
  1565. }
  1566. $date = array_search($date, $monthlist);
  1567. // Monthname found
  1568. if ($cnt < 12) {
  1569. $fixday = 0;
  1570. if ($calc === 'add') {
  1571. $date += $found;
  1572. $calc = 'set';
  1573. if (self::$_options['extend_month'] === false) {
  1574. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1575. if ($parts['mday'] != $day) {
  1576. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1577. }
  1578. }
  1579. } else if ($calc === 'sub') {
  1580. $date = $month - $found;
  1581. $calc = 'set';
  1582. if (self::$_options['extend_month'] === false) {
  1583. $parts = $this->getDateParts($this->mktime($hour, $minute, $second, $date, $day, $year, false));
  1584. if ($parts['mday'] != $day) {
  1585. $fixday = ($parts['mday'] < $day) ? -$parts['mday'] : ($parts['mday'] - $day);
  1586. }
  1587. }
  1588. }
  1589. return $this->_assign($calc, $this->mktime(0, 0, 0, $date, $day + $fixday, $year, true),
  1590. $this->mktime(0, 0, 0, $month, $day, $year, true), $hour);
  1591. }
  1592. // Monthname not found
  1593. require_once 'Zend/Date/Exception.php';
  1594. throw new Zend_Date_Exception("invalid date ($date) operand, month expected", 0, null, $date);
  1595. break;
  1596. // year formats
  1597. case self::LEAPYEAR:
  1598. require_once 'Zend/Date/Exception.php';
  1599. throw new Zend_Date_Exception('leap year not supported', 0, null, $date);
  1600. break;
  1601. case self::YEAR_8601:
  1602. if (is_numeric($date)) {
  1603. if ($calc === 'add') {
  1604. $date += $year;
  1605. $calc = 'set';
  1606. } else if ($calc === 'sub') {
  1607. $date = $year - $date;
  1608. $calc = 'set';
  1609. }
  1610. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1611. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1612. }
  1613. require_once 'Zend/Date/Exception.php';
  1614. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1615. break;
  1616. case self::YEAR:
  1617. if (is_numeric($date)) {
  1618. if ($calc === 'add') {
  1619. $date += $year;
  1620. $calc = 'set';
  1621. } else if ($calc === 'sub') {
  1622. $date = $year - $date;
  1623. $calc = 'set';
  1624. }
  1625. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, intval($date), true),
  1626. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1627. }
  1628. require_once 'Zend/Date/Exception.php';
  1629. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1630. break;
  1631. case self::YEAR_SHORT:
  1632. if (is_numeric($date)) {
  1633. $date = intval($date);
  1634. if (($calc == 'set') || ($calc == 'cmp')) {
  1635. $date = self::getFullYear($date);
  1636. }
  1637. if ($calc === 'add') {
  1638. $date += $year;
  1639. $calc = 'set';
  1640. } else if ($calc === 'sub') {
  1641. $date = $year - $date;
  1642. $calc = 'set';
  1643. }
  1644. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1645. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1646. }
  1647. require_once 'Zend/Date/Exception.php';
  1648. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1649. break;
  1650. case self::YEAR_SHORT_8601:
  1651. if (is_numeric($date)) {
  1652. $date = intval($date);
  1653. if (($calc === 'set') || ($calc === 'cmp')) {
  1654. $date = self::getFullYear($date);
  1655. }
  1656. if ($calc === 'add') {
  1657. $date += $year;
  1658. $calc = 'set';
  1659. } else if ($calc === 'sub') {
  1660. $date = $year - $date;
  1661. $calc = 'set';
  1662. }
  1663. return $this->_assign($calc, $this->mktime(0, 0, 0, $month, $day, $date, true),
  1664. $this->mktime(0, 0, 0, $month, $day, $year, true), false);
  1665. }
  1666. require_once 'Zend/Date/Exception.php';
  1667. throw new Zend_Date_Exception("invalid date ($date) operand, year expected", 0, null, $date);
  1668. break;
  1669. // time formats
  1670. case self::MERIDIEM:
  1671. require_once 'Zend/Date/Exception.php';
  1672. throw new Zend_Date_Exception('meridiem not supported', 0, null, $date);
  1673. break;
  1674. case self::SWATCH:
  1675. if (is_numeric($date)) {
  1676. $rest = intval($date);
  1677. $hours = floor($rest * 24 / 1000);
  1678. $rest = $rest - ($hours * 1000 / 24);
  1679. $minutes = floor($rest * 1440 / 1000);
  1680. $rest = $rest - ($minutes * 1000 / 1440);
  1681. $seconds = floor($rest * 86400 / 1000);
  1682. return $this->_assign($calc, $this->mktime($hours, $minutes, $seconds, 1, 1, 1970, true),
  1683. $this->mktime($hour, $minute, $second, 1, 1, 1970, true), false);
  1684. }
  1685. require_once 'Zend/Date/Exception.php';
  1686. throw new Zend_Date_Exception("invalid date ($date) operand, swatchstamp expected", 0, null, $date);
  1687. break;
  1688. case self::HOUR_SHORT_AM:
  1689. if (is_numeric($date)) {
  1690. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1691. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1692. }
  1693. require_once 'Zend/Date/Exception.php';
  1694. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1695. break;
  1696. case self::HOUR_SHORT:
  1697. if (is_numeric($date)) {
  1698. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1699. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1700. }
  1701. require_once 'Zend/Date/Exception.php';
  1702. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1703. break;
  1704. case self::HOUR_AM:
  1705. if (is_numeric($date)) {
  1706. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1707. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1708. }
  1709. require_once 'Zend/Date/Exception.php';
  1710. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1711. break;
  1712. case self::HOUR:
  1713. if (is_numeric($date)) {
  1714. return $this->_assign($calc, $this->mktime(intval($date), 0, 0, 1, 1, 1970, true),
  1715. $this->mktime($hour, 0, 0, 1, 1, 1970, true), false);
  1716. }
  1717. require_once 'Zend/Date/Exception.php';
  1718. throw new Zend_Date_Exception("invalid date ($date) operand, hour expected", 0, null, $date);
  1719. break;
  1720. case self::MINUTE:
  1721. if (is_numeric($date)) {
  1722. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1723. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1724. }
  1725. require_once 'Zend/Date/Exception.php';
  1726. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
  1727. break;
  1728. case self::SECOND:
  1729. if (is_numeric($date)) {
  1730. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1731. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1732. }
  1733. require_once 'Zend/Date/Exception.php';
  1734. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
  1735. break;
  1736. case self::MILLISECOND:
  1737. if (is_numeric($date)) {
  1738. switch($calc) {
  1739. case 'set' :
  1740. return $this->setMillisecond($date);
  1741. break;
  1742. case 'add' :
  1743. return $this->addMillisecond($date);
  1744. break;
  1745. case 'sub' :
  1746. return $this->subMillisecond($date);
  1747. break;
  1748. }
  1749. return $this->compareMillisecond($date);
  1750. }
  1751. require_once 'Zend/Date/Exception.php';
  1752. throw new Zend_Date_Exception("invalid date ($date) operand, milliseconds expected", 0, null, $date);
  1753. break;
  1754. case self::MINUTE_SHORT:
  1755. if (is_numeric($date)) {
  1756. return $this->_assign($calc, $this->mktime(0, intval($date), 0, 1, 1, 1970, true),
  1757. $this->mktime(0, $minute, 0, 1, 1, 1970, true), false);
  1758. }
  1759. require_once 'Zend/Date/Exception.php';
  1760. throw new Zend_Date_Exception("invalid date ($date) operand, minute expected", 0, null, $date);
  1761. break;
  1762. case self::SECOND_SHORT:
  1763. if (is_numeric($date)) {
  1764. return $this->_assign($calc, $this->mktime(0, 0, intval($date), 1, 1, 1970, true),
  1765. $this->mktime(0, 0, $second, 1, 1, 1970, true), false);
  1766. }
  1767. require_once 'Zend/Date/Exception.php';
  1768. throw new Zend_Date_Exception("invalid date ($date) operand, second expected", 0, null, $date);
  1769. break;
  1770. // timezone formats
  1771. // break intentionally omitted
  1772. case self::TIMEZONE_NAME:
  1773. case self::TIMEZONE:
  1774. case self::TIMEZONE_SECS:
  1775. require_once 'Zend/Date/Exception.php';
  1776. throw new Zend_Date_Exception('timezone not supported', 0, null, $date);
  1777. break;
  1778. case self::DAYLIGHT:
  1779. require_once 'Zend/Date/Exception.php';
  1780. throw new Zend_Date_Exception('daylight not supported', 0, null, $date);
  1781. break;
  1782. case self::GMT_DIFF:
  1783. case self::GMT_DIFF_SEP:
  1784. require_once 'Zend/Date/Exception.php';
  1785. throw new Zend_Date_Exception('gmtdiff not supported', 0, null, $date);
  1786. break;
  1787. // date strings
  1788. case self::ISO_8601:
  1789. // (-)YYYY-MM-dd
  1790. preg_match('/^(-{0,1}\d{4})-(\d{2})-(\d{2})/', $date, $datematch);
  1791. // (-)YY-MM-dd
  1792. if (empty($datematch)) {
  1793. preg_match('/^(-{0,1}\d{2})-(\d{2})-(\d{2})/', $date, $datematch);
  1794. }
  1795. // (-)YYYYMMdd
  1796. if (empty($datematch)) {
  1797. preg_match('/^(-{0,1}\d{4})(\d{2})(\d{2})/', $date, $datematch);
  1798. }
  1799. // (-)YYMMdd
  1800. if (empty($datematch)) {
  1801. preg_match('/^(-{0,1}\d{2})(\d{2})(\d{2})/', $date, $datematch);
  1802. }
  1803. $tmpdate = $date;
  1804. if (!empty($datematch)) {
  1805. $dateMatchCharCount = iconv_strlen($datematch[0], 'UTF-8');
  1806. $tmpdate = iconv_substr($date,
  1807. $dateMatchCharCount,
  1808. iconv_strlen($date, 'UTF-8') - $dateMatchCharCount,
  1809. 'UTF-8');
  1810. }
  1811. // (T)hh:mm:ss
  1812. preg_match('/[T,\s]{0,1}(\d{2}):(\d{2}):(\d{2})/', $tmpdate, $timematch);
  1813. if (empty($timematch)) {
  1814. preg_match('/[T,\s]{0,1}(\d{2})(\d{2})(\d{2})/', $tmpdate, $timematch);
  1815. }
  1816. if (empty($datematch) and empty($timematch)) {
  1817. require_once 'Zend/Date/Exception.php';
  1818. throw new Zend_Date_Exception("unsupported ISO8601 format ($date)", 0, null, $date);
  1819. }
  1820. if (!empty($timematch)) {
  1821. $timeMatchCharCount = iconv_strlen($timematch[0], 'UTF-8');
  1822. $tmpdate = iconv_substr($tmpdate,
  1823. $timeMatchCharCount,
  1824. iconv_strlen($tmpdate, 'UTF-8') - $timeMatchCharCount,
  1825. 'UTF-8');
  1826. }
  1827. if (empty($datematch)) {
  1828. $datematch[1] = 1970;
  1829. $datematch[2] = 1;
  1830. $datematch[3] = 1;
  1831. } else if (iconv_strlen($datematch[1], 'UTF-8') == 2) {
  1832. $datematch[1] = self::getFullYear($datematch[1]);
  1833. }
  1834. if (empty($timematch)) {
  1835. $timematch[1] = 0;
  1836. $timematch[2] = 0;
  1837. $timematch[3] = 0;
  1838. }
  1839. if (($calc == 'set') || ($calc == 'cmp')) {
  1840. --$datematch[2];
  1841. --$month;
  1842. --$datematch[3];
  1843. --$day;
  1844. $datematch[1] -= 1970;
  1845. $year -= 1970;
  1846. }
  1847. return $this->_assign($calc, $this->mktime($timematch[1], $timematch[2], $timematch[3], 1 + $datematch[2], 1 + $datematch[3], 1970 + $datematch[1], false),
  1848. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1849. break;
  1850. case self::RFC_2822:
  1851. $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);
  1852. if (!$result) {
  1853. require_once 'Zend/Date/Exception.php';
  1854. throw new Zend_Date_Exception("no RFC 2822 format ($date)", 0, null, $date);
  1855. }
  1856. $months = $this->_getDigitFromName($match[2]);
  1857. if (($calc == 'set') || ($calc == 'cmp')) {
  1858. --$months;
  1859. --$month;
  1860. --$match[1];
  1861. --$day;
  1862. $match[3] -= 1970;
  1863. $year -= 1970;
  1864. }
  1865. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  1866. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  1867. break;
  1868. case self::TIMESTAMP:
  1869. if (is_numeric($date)) {
  1870. return $this->_assign($calc, $date, $this->getUnixTimestamp());
  1871. }
  1872. require_once 'Zend/Date/Exception.php';
  1873. throw new Zend_Date_Exception("invalid date ($date) operand, timestamp expected", 0, null, $date);
  1874. break;
  1875. // additional formats
  1876. // break intentionally omitted
  1877. case self::ERA:
  1878. case self::ERA_NAME:
  1879. require_once 'Zend/Date/Exception.php';
  1880. throw new Zend_Date_Exception('era not supported', 0, null, $date);
  1881. break;
  1882. case self::DATES:
  1883. try {
  1884. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1885. if (($calc == 'set') || ($calc == 'cmp')) {
  1886. --$parsed['month'];
  1887. --$month;
  1888. --$parsed['day'];
  1889. --$day;
  1890. $parsed['year'] -= 1970;
  1891. $year -= 1970;
  1892. }
  1893. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1894. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1895. } catch (Zend_Locale_Exception $e) {
  1896. require_once 'Zend/Date/Exception.php';
  1897. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1898. }
  1899. break;
  1900. case self::DATE_FULL:
  1901. try {
  1902. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  1903. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1904. if (($calc == 'set') || ($calc == 'cmp')) {
  1905. --$parsed['month'];
  1906. --$month;
  1907. --$parsed['day'];
  1908. --$day;
  1909. $parsed['year'] -= 1970;
  1910. $year -= 1970;
  1911. }
  1912. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1913. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1914. } catch (Zend_Locale_Exception $e) {
  1915. require_once 'Zend/Date/Exception.php';
  1916. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1917. }
  1918. break;
  1919. case self::DATE_LONG:
  1920. try {
  1921. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  1922. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1923. if (($calc == 'set') || ($calc == 'cmp')){
  1924. --$parsed['month'];
  1925. --$month;
  1926. --$parsed['day'];
  1927. --$day;
  1928. $parsed['year'] -= 1970;
  1929. $year -= 1970;
  1930. }
  1931. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1932. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1933. } catch (Zend_Locale_Exception $e) {
  1934. require_once 'Zend/Date/Exception.php';
  1935. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1936. }
  1937. break;
  1938. case self::DATE_MEDIUM:
  1939. try {
  1940. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  1941. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1942. if (($calc == 'set') || ($calc == 'cmp')) {
  1943. --$parsed['month'];
  1944. --$month;
  1945. --$parsed['day'];
  1946. --$day;
  1947. $parsed['year'] -= 1970;
  1948. $year -= 1970;
  1949. }
  1950. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1951. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1952. } catch (Zend_Locale_Exception $e) {
  1953. require_once 'Zend/Date/Exception.php';
  1954. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1955. }
  1956. break;
  1957. case self::DATE_SHORT:
  1958. try {
  1959. $format = Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  1960. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1961. $parsed['year'] = self::getFullYear($parsed['year']);
  1962. if (($calc == 'set') || ($calc == 'cmp')) {
  1963. --$parsed['month'];
  1964. --$month;
  1965. --$parsed['day'];
  1966. --$day;
  1967. $parsed['year'] -= 1970;
  1968. $year -= 1970;
  1969. }
  1970. return $this->_assign($calc, $this->mktime(0, 0, 0, 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  1971. $this->mktime(0, 0, 0, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  1972. } catch (Zend_Locale_Exception $e) {
  1973. require_once 'Zend/Date/Exception.php';
  1974. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1975. }
  1976. break;
  1977. case self::TIMES:
  1978. try {
  1979. if ($calc != 'set') {
  1980. $month = 1;
  1981. $day = 1;
  1982. $year = 1970;
  1983. }
  1984. $parsed = Zend_Locale_Format::getTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  1985. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  1986. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  1987. } catch (Zend_Locale_Exception $e) {
  1988. require_once 'Zend/Date/Exception.php';
  1989. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  1990. }
  1991. break;
  1992. case self::TIME_FULL:
  1993. try {
  1994. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  1995. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  1996. if ($calc != 'set') {
  1997. $month = 1;
  1998. $day = 1;
  1999. $year = 1970;
  2000. }
  2001. if (!isset($parsed['second'])) {
  2002. $parsed['second'] = 0;
  2003. }
  2004. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2005. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2006. } catch (Zend_Locale_Exception $e) {
  2007. require_once 'Zend/Date/Exception.php';
  2008. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2009. }
  2010. break;
  2011. case self::TIME_LONG:
  2012. try {
  2013. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  2014. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2015. if ($calc != 'set') {
  2016. $month = 1;
  2017. $day = 1;
  2018. $year = 1970;
  2019. }
  2020. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2021. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2022. } catch (Zend_Locale_Exception $e) {
  2023. require_once 'Zend/Date/Exception.php';
  2024. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2025. }
  2026. break;
  2027. case self::TIME_MEDIUM:
  2028. try {
  2029. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  2030. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2031. if ($calc != 'set') {
  2032. $month = 1;
  2033. $day = 1;
  2034. $year = 1970;
  2035. }
  2036. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2037. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2038. } catch (Zend_Locale_Exception $e) {
  2039. require_once 'Zend/Date/Exception.php';
  2040. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2041. }
  2042. break;
  2043. case self::TIME_SHORT:
  2044. try {
  2045. $format = Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  2046. $parsed = Zend_Locale_Format::getTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2047. if ($calc != 'set') {
  2048. $month = 1;
  2049. $day = 1;
  2050. $year = 1970;
  2051. }
  2052. if (!isset($parsed['second'])) {
  2053. $parsed['second'] = 0;
  2054. }
  2055. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], $month, $day, $year, true),
  2056. $this->mktime($hour, $minute, $second, $month, $day, $year, true), false);
  2057. } catch (Zend_Locale_Exception $e) {
  2058. require_once 'Zend/Date/Exception.php';
  2059. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2060. }
  2061. break;
  2062. case self::DATETIME:
  2063. try {
  2064. $parsed = Zend_Locale_Format::getDateTime($date, array('locale' => $locale, 'format_type' => 'iso', 'fix_date' => true));
  2065. if (($calc == 'set') || ($calc == 'cmp')) {
  2066. --$parsed['month'];
  2067. --$month;
  2068. --$parsed['day'];
  2069. --$day;
  2070. $parsed['year'] -= 1970;
  2071. $year -= 1970;
  2072. }
  2073. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2074. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2075. } catch (Zend_Locale_Exception $e) {
  2076. require_once 'Zend/Date/Exception.php';
  2077. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2078. }
  2079. break;
  2080. case self::DATETIME_FULL:
  2081. try {
  2082. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  2083. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2084. if (($calc == 'set') || ($calc == 'cmp')) {
  2085. --$parsed['month'];
  2086. --$month;
  2087. --$parsed['day'];
  2088. --$day;
  2089. $parsed['year'] -= 1970;
  2090. $year -= 1970;
  2091. }
  2092. if (!isset($parsed['second'])) {
  2093. $parsed['second'] = 0;
  2094. }
  2095. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2096. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2097. } catch (Zend_Locale_Exception $e) {
  2098. require_once 'Zend/Date/Exception.php';
  2099. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2100. }
  2101. break;
  2102. case self::DATETIME_LONG:
  2103. try {
  2104. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  2105. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2106. if (($calc == 'set') || ($calc == 'cmp')){
  2107. --$parsed['month'];
  2108. --$month;
  2109. --$parsed['day'];
  2110. --$day;
  2111. $parsed['year'] -= 1970;
  2112. $year -= 1970;
  2113. }
  2114. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2115. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2116. } catch (Zend_Locale_Exception $e) {
  2117. require_once 'Zend/Date/Exception.php';
  2118. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2119. }
  2120. break;
  2121. case self::DATETIME_MEDIUM:
  2122. try {
  2123. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  2124. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2125. if (($calc == 'set') || ($calc == 'cmp')) {
  2126. --$parsed['month'];
  2127. --$month;
  2128. --$parsed['day'];
  2129. --$day;
  2130. $parsed['year'] -= 1970;
  2131. $year -= 1970;
  2132. }
  2133. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2134. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2135. } catch (Zend_Locale_Exception $e) {
  2136. require_once 'Zend/Date/Exception.php';
  2137. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2138. }
  2139. break;
  2140. case self::DATETIME_SHORT:
  2141. try {
  2142. $format = Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  2143. $parsed = Zend_Locale_Format::getDateTime($date, array('date_format' => $format, 'format_type' => 'iso', 'locale' => $locale));
  2144. $parsed['year'] = self::getFullYear($parsed['year']);
  2145. if (($calc == 'set') || ($calc == 'cmp')) {
  2146. --$parsed['month'];
  2147. --$month;
  2148. --$parsed['day'];
  2149. --$day;
  2150. $parsed['year'] -= 1970;
  2151. $year -= 1970;
  2152. }
  2153. if (!isset($parsed['second'])) {
  2154. $parsed['second'] = 0;
  2155. }
  2156. return $this->_assign($calc, $this->mktime($parsed['hour'], $parsed['minute'], $parsed['second'], 1 + $parsed['month'], 1 + $parsed['day'], 1970 + $parsed['year'], true),
  2157. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), $hour);
  2158. } catch (Zend_Locale_Exception $e) {
  2159. require_once 'Zend/Date/Exception.php';
  2160. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2161. }
  2162. break;
  2163. // ATOM and RFC_3339 are identical
  2164. case self::ATOM:
  2165. case self::RFC_3339:
  2166. $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);
  2167. if (!$result) {
  2168. require_once 'Zend/Date/Exception.php';
  2169. throw new Zend_Date_Exception("invalid date ($date) operand, ATOM format expected", 0, null, $date);
  2170. }
  2171. if (($calc == 'set') || ($calc == 'cmp')) {
  2172. --$match[2];
  2173. --$month;
  2174. --$match[3];
  2175. --$day;
  2176. $match[1] -= 1970;
  2177. $year -= 1970;
  2178. }
  2179. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2180. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2181. break;
  2182. case self::COOKIE:
  2183. $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);
  2184. if (!$result) {
  2185. require_once 'Zend/Date/Exception.php';
  2186. throw new Zend_Date_Exception("invalid date ($date) operand, COOKIE format expected", 0, null, $date);
  2187. }
  2188. $matchStartPos = iconv_strpos($match[0], ' ', 0, 'UTF-8') + 1;
  2189. $match[0] = iconv_substr($match[0],
  2190. $matchStartPos,
  2191. iconv_strlen($match[0], 'UTF-8') - $matchStartPos,
  2192. 'UTF-8');
  2193. $months = $this->_getDigitFromName($match[2]);
  2194. $match[3] = self::getFullYear($match[3]);
  2195. if (($calc == 'set') || ($calc == 'cmp')) {
  2196. --$months;
  2197. --$month;
  2198. --$match[1];
  2199. --$day;
  2200. $match[3] -= 1970;
  2201. $year -= 1970;
  2202. }
  2203. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2204. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2205. break;
  2206. case self::RFC_822:
  2207. case self::RFC_1036:
  2208. // new RFC 822 format, identical to RFC 1036 standard
  2209. $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);
  2210. if (!$result) {
  2211. require_once 'Zend/Date/Exception.php';
  2212. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 822 date format expected", 0, null, $date);
  2213. }
  2214. $months = $this->_getDigitFromName($match[2]);
  2215. $match[3] = self::getFullYear($match[3]);
  2216. if (($calc == 'set') || ($calc == 'cmp')) {
  2217. --$months;
  2218. --$month;
  2219. --$match[1];
  2220. --$day;
  2221. $match[3] -= 1970;
  2222. $year -= 1970;
  2223. }
  2224. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], false),
  2225. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, false), false);
  2226. break;
  2227. case self::RFC_850:
  2228. $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);
  2229. if (!$result) {
  2230. require_once 'Zend/Date/Exception.php';
  2231. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 850 date format expected", 0, null, $date);
  2232. }
  2233. $months = $this->_getDigitFromName($match[2]);
  2234. $match[3] = self::getFullYear($match[3]);
  2235. if (($calc == 'set') || ($calc == 'cmp')) {
  2236. --$months;
  2237. --$month;
  2238. --$match[1];
  2239. --$day;
  2240. $match[3] -= 1970;
  2241. $year -= 1970;
  2242. }
  2243. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2244. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2245. break;
  2246. case self::RFC_1123:
  2247. $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);
  2248. if (!$result) {
  2249. require_once 'Zend/Date/Exception.php';
  2250. throw new Zend_Date_Exception("invalid date ($date) operand, RFC 1123 date format expected", 0, null, $date);
  2251. }
  2252. $months = $this->_getDigitFromName($match[2]);
  2253. if (($calc == 'set') || ($calc == 'cmp')) {
  2254. --$months;
  2255. --$month;
  2256. --$match[1];
  2257. --$day;
  2258. $match[3] -= 1970;
  2259. $year -= 1970;
  2260. }
  2261. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2262. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2263. break;
  2264. case self::RSS:
  2265. $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);
  2266. if (!$result) {
  2267. require_once 'Zend/Date/Exception.php';
  2268. throw new Zend_Date_Exception("invalid date ($date) operand, RSS date format expected", 0, null, $date);
  2269. }
  2270. $months = $this->_getDigitFromName($match[2]);
  2271. $match[3] = self::getFullYear($match[3]);
  2272. if (($calc == 'set') || ($calc == 'cmp')) {
  2273. --$months;
  2274. --$month;
  2275. --$match[1];
  2276. --$day;
  2277. $match[3] -= 1970;
  2278. $year -= 1970;
  2279. }
  2280. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $months, 1 + $match[1], 1970 + $match[3], true),
  2281. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2282. break;
  2283. case self::W3C:
  2284. $result = preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})[+-]{1}\d{2}:\d{2}$/', $date, $match);
  2285. if (!$result) {
  2286. require_once 'Zend/Date/Exception.php';
  2287. throw new Zend_Date_Exception("invalid date ($date) operand, W3C date format expected", 0, null, $date);
  2288. }
  2289. if (($calc == 'set') || ($calc == 'cmp')) {
  2290. --$match[2];
  2291. --$month;
  2292. --$match[3];
  2293. --$day;
  2294. $match[1] -= 1970;
  2295. $year -= 1970;
  2296. }
  2297. return $this->_assign($calc, $this->mktime($match[4], $match[5], $match[6], 1 + $match[2], 1 + $match[3], 1970 + $match[1], true),
  2298. $this->mktime($hour, $minute, $second, 1 + $month, 1 + $day, 1970 + $year, true), false);
  2299. break;
  2300. default:
  2301. if (!is_numeric($date) || !empty($part)) {
  2302. try {
  2303. if (empty($part)) {
  2304. $part = Zend_Locale_Format::getDateFormat($locale) . " ";
  2305. $part .= Zend_Locale_Format::getTimeFormat($locale);
  2306. }
  2307. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $part, 'locale' => $locale, 'fix_date' => true, 'format_type' => 'iso'));
  2308. if ((strpos(strtoupper($part), 'YY') !== false) and (strpos(strtoupper($part), 'YYYY') === false)) {
  2309. $parsed['year'] = self::getFullYear($parsed['year']);
  2310. }
  2311. if (($calc == 'set') || ($calc == 'cmp')) {
  2312. if (isset($parsed['month'])) {
  2313. --$parsed['month'];
  2314. } else {
  2315. $parsed['month'] = 0;
  2316. }
  2317. if (isset($parsed['day'])) {
  2318. --$parsed['day'];
  2319. } else {
  2320. $parsed['day'] = 0;
  2321. }
  2322. if (isset($parsed['year'])) {
  2323. $parsed['year'] -= 1970;
  2324. } else {
  2325. $parsed['year'] = 0;
  2326. }
  2327. }
  2328. return $this->_assign($calc, $this->mktime(
  2329. isset($parsed['hour']) ? $parsed['hour'] : 0,
  2330. isset($parsed['minute']) ? $parsed['minute'] : 0,
  2331. isset($parsed['second']) ? $parsed['second'] : 0,
  2332. isset($parsed['month']) ? (1 + $parsed['month']) : 1,
  2333. isset($parsed['day']) ? (1 + $parsed['day']) : 1,
  2334. isset($parsed['year']) ? (1970 + $parsed['year']) : 1970,
  2335. false), $this->getUnixTimestamp(), false);
  2336. } catch (Zend_Locale_Exception $e) {
  2337. if (!is_numeric($date)) {
  2338. require_once 'Zend/Date/Exception.php';
  2339. throw new Zend_Date_Exception($e->getMessage(), 0, $e, $date);
  2340. }
  2341. }
  2342. }
  2343. return $this->_assign($calc, $date, $this->getUnixTimestamp(), false);
  2344. break;
  2345. }
  2346. }
  2347. /**
  2348. * Returns true when both date objects or date parts are equal.
  2349. * For example:
  2350. * 15.May.2000 <-> 15.June.2000 Equals only for Day or Year... all other will return false
  2351. *
  2352. * @param string|integer|array|Zend_Date $date Date or datepart to equal with
  2353. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2354. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2355. * @return boolean
  2356. * @throws Zend_Date_Exception
  2357. */
  2358. public function equals($date, $part = self::TIMESTAMP, $locale = null)
  2359. {
  2360. $result = $this->compare($date, $part, $locale);
  2361. if ($result == 0) {
  2362. return true;
  2363. }
  2364. return false;
  2365. }
  2366. /**
  2367. * Returns if the given date or datepart is earlier
  2368. * For example:
  2369. * 15.May.2000 <-> 13.June.1999 will return true for day, year and date, but not for month
  2370. *
  2371. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2372. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2373. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2374. * @return boolean
  2375. * @throws Zend_Date_Exception
  2376. */
  2377. public function isEarlier($date, $part = null, $locale = null)
  2378. {
  2379. $result = $this->compare($date, $part, $locale);
  2380. if ($result == -1) {
  2381. return true;
  2382. }
  2383. return false;
  2384. }
  2385. /**
  2386. * Returns if the given date or datepart is later
  2387. * For example:
  2388. * 15.May.2000 <-> 13.June.1999 will return true for month but false for day, year and date
  2389. * Returns if the given date is later
  2390. *
  2391. * @param string|integer|array|Zend_Date $date Date or datepart to compare with
  2392. * @param string $part OPTIONAL Part of the date to compare, if null the timestamp is used
  2393. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2394. * @return boolean
  2395. * @throws Zend_Date_Exception
  2396. */
  2397. public function isLater($date, $part = null, $locale = null)
  2398. {
  2399. $result = $this->compare($date, $part, $locale);
  2400. if ($result == 1) {
  2401. return true;
  2402. }
  2403. return false;
  2404. }
  2405. /**
  2406. * Returns only the time of the date as new Zend_Date object
  2407. * For example:
  2408. * 15.May.2000 10:11:23 will return a dateobject equal to 01.Jan.1970 10:11:23
  2409. *
  2410. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2411. * @return Zend_Date
  2412. */
  2413. public function getTime($locale = null)
  2414. {
  2415. if (self::$_options['format_type'] == 'php') {
  2416. $format = 'H:i:s';
  2417. } else {
  2418. $format = self::TIME_MEDIUM;
  2419. }
  2420. return $this->copyPart($format, $locale);
  2421. }
  2422. /**
  2423. * Returns the calculated time
  2424. *
  2425. * @param string $calc Calculation to make
  2426. * @param string|integer|array|Zend_Date $time Time to calculate with, if null the actual time is taken
  2427. * @param string $format Timeformat for parsing input
  2428. * @param string|Zend_Locale $locale Locale for parsing input
  2429. * @return integer|Zend_Date new time
  2430. * @throws Zend_Date_Exception
  2431. */
  2432. private function _time($calc, $time, $format, $locale)
  2433. {
  2434. if ($time === null) {
  2435. require_once 'Zend/Date/Exception.php';
  2436. throw new Zend_Date_Exception('parameter $time must be set, null is not allowed');
  2437. }
  2438. if ($time instanceof Zend_Date) {
  2439. // extract time from object
  2440. $time = $time->toString('HH:mm:ss', 'iso');
  2441. } else {
  2442. if (is_array($time)) {
  2443. if ((isset($time['hour']) === true) or (isset($time['minute']) === true) or
  2444. (isset($time['second']) === true)) {
  2445. $parsed = $time;
  2446. } else {
  2447. require_once 'Zend/Date/Exception.php';
  2448. throw new Zend_Date_Exception("no hour, minute or second given in array");
  2449. }
  2450. } else {
  2451. if (self::$_options['format_type'] == 'php') {
  2452. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2453. }
  2454. try {
  2455. if ($locale === null) {
  2456. $locale = $this->getLocale();
  2457. }
  2458. $parsed = Zend_Locale_Format::getTime($time, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2459. } catch (Zend_Locale_Exception $e) {
  2460. require_once 'Zend/Date/Exception.php';
  2461. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2462. }
  2463. }
  2464. if (!array_key_exists('hour', $parsed)) {
  2465. $parsed['hour'] = 0;
  2466. }
  2467. if (!array_key_exists('minute', $parsed)) {
  2468. $parsed['minute'] = 0;
  2469. }
  2470. if (!array_key_exists('second', $parsed)) {
  2471. $parsed['second'] = 0;
  2472. }
  2473. $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
  2474. $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
  2475. $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
  2476. }
  2477. $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
  2478. if ($calc != 'cmp') {
  2479. return $this;
  2480. }
  2481. return $return;
  2482. }
  2483. /**
  2484. * Sets a new time for the date object. Format defines how to parse the time string.
  2485. * Also a complete date can be given, but only the time is used for setting.
  2486. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2487. * Returned is the new date object and the existing date is left as it was before
  2488. *
  2489. * @param string|integer|array|Zend_Date $time Time to set
  2490. * @param string $format OPTIONAL Timeformat for parsing input
  2491. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2492. * @return Zend_Date Provides fluid interface
  2493. * @throws Zend_Date_Exception
  2494. */
  2495. public function setTime($time, $format = null, $locale = null)
  2496. {
  2497. return $this->_time('set', $time, $format, $locale);
  2498. }
  2499. /**
  2500. * Adds a time to the existing date. Format defines how to parse the time string.
  2501. * If only parts are given the other parts are set to 0.
  2502. * If no format is given, the standardformat of this locale is used.
  2503. * For example: HH:mm:ss -> 10 -> +10 hours
  2504. *
  2505. * @param string|integer|array|Zend_Date $time Time to add
  2506. * @param string $format OPTIONAL Timeformat for parsing input
  2507. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2508. * @return Zend_Date Provides fluid interface
  2509. * @throws Zend_Date_Exception
  2510. */
  2511. public function addTime($time, $format = null, $locale = null)
  2512. {
  2513. return $this->_time('add', $time, $format, $locale);
  2514. }
  2515. /**
  2516. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2517. * If only parts are given the other parts are set to 0.
  2518. * If no format is given, the standardformat of this locale is used.
  2519. * For example: HH:mm:ss -> 10 -> -10 hours
  2520. *
  2521. * @param string|integer|array|Zend_Date $time Time to sub
  2522. * @param string $format OPTIONAL Timeformat for parsing input
  2523. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2524. * @return Zend_Date Provides fluid inteface
  2525. * @throws Zend_Date_Exception
  2526. */
  2527. public function subTime($time, $format = null, $locale = null)
  2528. {
  2529. return $this->_time('sub', $time, $format, $locale);
  2530. }
  2531. /**
  2532. * Compares the time from the existing date. Format defines how to parse the time string.
  2533. * If only parts are given the other parts are set to default.
  2534. * If no format us given, the standardformat of this locale is used.
  2535. * For example: HH:mm:ss -> 10 -> 10 hours
  2536. *
  2537. * @param string|integer|array|Zend_Date $time Time to compare
  2538. * @param string $format OPTIONAL Timeformat for parsing input
  2539. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2540. * @return integer 0 = equal, 1 = later, -1 = earlier
  2541. * @throws Zend_Date_Exception
  2542. */
  2543. public function compareTime($time, $format = null, $locale = null)
  2544. {
  2545. return $this->_time('cmp', $time, $format, $locale);
  2546. }
  2547. /**
  2548. * Returns a clone of $this, with the time part set to 00:00:00.
  2549. *
  2550. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2551. * @return Zend_Date
  2552. */
  2553. public function getDate($locale = null)
  2554. {
  2555. $orig = self::$_options['format_type'];
  2556. if (self::$_options['format_type'] == 'php') {
  2557. self::$_options['format_type'] = 'iso';
  2558. }
  2559. $date = $this->copyPart(self::DATE_MEDIUM, $locale);
  2560. $date->addTimestamp($this->getGmtOffset());
  2561. self::$_options['format_type'] = $orig;
  2562. return $date;
  2563. }
  2564. /**
  2565. * Returns the calculated date
  2566. *
  2567. * @param string $calc Calculation to make
  2568. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2569. * @param string $format Date format for parsing
  2570. * @param string|Zend_Locale $locale Locale for parsing input
  2571. * @return integer|Zend_Date new date
  2572. * @throws Zend_Date_Exception
  2573. */
  2574. private function _date($calc, $date, $format, $locale)
  2575. {
  2576. if ($date === null) {
  2577. require_once 'Zend/Date/Exception.php';
  2578. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2579. }
  2580. if ($date instanceof Zend_Date) {
  2581. // extract date from object
  2582. $date = $date->toString('d.M.y', 'iso');
  2583. } else {
  2584. if (is_array($date)) {
  2585. if ((isset($date['year']) === true) or (isset($date['month']) === true) or
  2586. (isset($date['day']) === true)) {
  2587. $parsed = $date;
  2588. } else {
  2589. require_once 'Zend/Date/Exception.php';
  2590. throw new Zend_Date_Exception("no day,month or year given in array");
  2591. }
  2592. } else {
  2593. if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  2594. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2595. }
  2596. try {
  2597. if ($locale === null) {
  2598. $locale = $this->getLocale();
  2599. }
  2600. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2601. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2602. $parsed['year'] = self::getFullYear($parsed['year']);
  2603. }
  2604. } catch (Zend_Locale_Exception $e) {
  2605. require_once 'Zend/Date/Exception.php';
  2606. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2607. }
  2608. }
  2609. if (!array_key_exists('day', $parsed)) {
  2610. $parsed['day'] = 1;
  2611. }
  2612. if (!array_key_exists('month', $parsed)) {
  2613. $parsed['month'] = 1;
  2614. }
  2615. if (!array_key_exists('year', $parsed)) {
  2616. $parsed['year'] = 0;
  2617. }
  2618. $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
  2619. }
  2620. $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
  2621. if ($calc != 'cmp') {
  2622. return $this;
  2623. }
  2624. return $return;
  2625. }
  2626. /**
  2627. * Sets a new date for the date object. Format defines how to parse the date string.
  2628. * Also a complete date with time can be given, but only the date is used for setting.
  2629. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2630. * Returned is the new date object and the existing time is left as it was before
  2631. *
  2632. * @param string|integer|array|Zend_Date $date Date to set
  2633. * @param string $format OPTIONAL Date format for parsing
  2634. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2635. * @return Zend_Date Provides fluid interface
  2636. * @throws Zend_Date_Exception
  2637. */
  2638. public function setDate($date, $format = null, $locale = null)
  2639. {
  2640. return $this->_date('set', $date, $format, $locale);
  2641. }
  2642. /**
  2643. * Adds a date to the existing date object. Format defines how to parse the date string.
  2644. * If only parts are given the other parts are set to 0.
  2645. * If no format is given, the standardformat of this locale is used.
  2646. * For example: MM.dd.YYYY -> 10 -> +10 months
  2647. *
  2648. * @param string|integer|array|Zend_Date $date Date to add
  2649. * @param string $format OPTIONAL Date format for parsing input
  2650. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2651. * @return Zend_Date Provides fluid interface
  2652. * @throws Zend_Date_Exception
  2653. */
  2654. public function addDate($date, $format = null, $locale = null)
  2655. {
  2656. return $this->_date('add', $date, $format, $locale);
  2657. }
  2658. /**
  2659. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2660. * If only parts are given the other parts are set to 0.
  2661. * If no format is given, the standardformat of this locale is used.
  2662. * For example: MM.dd.YYYY -> 10 -> -10 months
  2663. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2664. *
  2665. * @param string|integer|array|Zend_Date $date Date to sub
  2666. * @param string $format OPTIONAL Date format for parsing input
  2667. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2668. * @return Zend_Date Provides fluid interface
  2669. * @throws Zend_Date_Exception
  2670. */
  2671. public function subDate($date, $format = null, $locale = null)
  2672. {
  2673. return $this->_date('sub', $date, $format, $locale);
  2674. }
  2675. /**
  2676. * Compares the date from the existing date object, ignoring the time.
  2677. * Format defines how to parse the date string.
  2678. * If only parts are given the other parts are set to 0.
  2679. * If no format is given, the standardformat of this locale is used.
  2680. * For example: 10.01.2000 => 10.02.1999 -> false
  2681. *
  2682. * @param string|integer|array|Zend_Date $date Date to compare
  2683. * @param string $format OPTIONAL Date format for parsing input
  2684. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2685. * @return integer 0 = equal, 1 = later, -1 = earlier
  2686. * @throws Zend_Date_Exception
  2687. */
  2688. public function compareDate($date, $format = null, $locale = null)
  2689. {
  2690. return $this->_date('cmp', $date, $format, $locale);
  2691. }
  2692. /**
  2693. * Returns the full ISO 8601 date from the date object.
  2694. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2695. * (ISO 8601 defines several formats) use toString() instead.
  2696. * This function does not return the ISO date as object. Use copy() instead.
  2697. *
  2698. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2699. * @return string
  2700. */
  2701. public function getIso($locale = null)
  2702. {
  2703. return $this->toString(self::ISO_8601, 'iso', $locale);
  2704. }
  2705. /**
  2706. * Sets a new date for the date object. Not given parts are set to default.
  2707. * Only supported ISO 8601 formats are accepted.
  2708. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2709. * Returned is the new date object
  2710. *
  2711. * @param string|integer|Zend_Date $date ISO Date to set
  2712. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2713. * @return Zend_Date Provides fluid interface
  2714. * @throws Zend_Date_Exception
  2715. */
  2716. public function setIso($date, $locale = null)
  2717. {
  2718. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2719. }
  2720. /**
  2721. * Adds a ISO date to the date object. Not given parts are set to default.
  2722. * Only supported ISO 8601 formats are accepted.
  2723. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2724. * Returned is the new date object
  2725. *
  2726. * @param string|integer|Zend_Date $date ISO Date to add
  2727. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2728. * @return Zend_Date Provides fluid interface
  2729. * @throws Zend_Date_Exception
  2730. */
  2731. public function addIso($date, $locale = null)
  2732. {
  2733. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2734. }
  2735. /**
  2736. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2737. * Only supported ISO 8601 formats are accepted.
  2738. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2739. * Returned is the new date object
  2740. *
  2741. * @param string|integer|Zend_Date $date ISO Date to sub
  2742. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2743. * @return Zend_Date Provides fluid interface
  2744. * @throws Zend_Date_Exception
  2745. */
  2746. public function subIso($date, $locale = null)
  2747. {
  2748. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2749. }
  2750. /**
  2751. * Compares a ISO date with the date object. Not given parts are set to default.
  2752. * Only supported ISO 8601 formats are accepted.
  2753. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2754. * Returns if equal, earlier or later
  2755. *
  2756. * @param string|integer|Zend_Date $date ISO Date to sub
  2757. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2758. * @return integer 0 = equal, 1 = later, -1 = earlier
  2759. * @throws Zend_Date_Exception
  2760. */
  2761. public function compareIso($date, $locale = null)
  2762. {
  2763. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2764. }
  2765. /**
  2766. * Returns a RFC 822 compilant datestring from the date object.
  2767. * This function does not return the RFC date as object. Use copy() instead.
  2768. *
  2769. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2770. * @return string
  2771. */
  2772. public function getArpa($locale = null)
  2773. {
  2774. if (self::$_options['format_type'] == 'php') {
  2775. $format = 'D\, d M y H\:i\:s O';
  2776. } else {
  2777. $format = self::RFC_822;
  2778. }
  2779. return $this->toString($format, 'iso', $locale);
  2780. }
  2781. /**
  2782. * Sets a RFC 822 date as new date for the date object.
  2783. * Only RFC 822 compilant date strings are accepted.
  2784. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2785. * Returned is the new date object
  2786. *
  2787. * @param string|integer|Zend_Date $date RFC 822 to set
  2788. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2789. * @return Zend_Date Provides fluid interface
  2790. * @throws Zend_Date_Exception
  2791. */
  2792. public function setArpa($date, $locale = null)
  2793. {
  2794. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2795. }
  2796. /**
  2797. * Adds a RFC 822 date to the date object.
  2798. * ARPA messages are used in emails or HTTP Headers.
  2799. * Only RFC 822 compilant date strings are accepted.
  2800. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2801. * Returned is the new date object
  2802. *
  2803. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2804. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2805. * @return Zend_Date Provides fluid interface
  2806. * @throws Zend_Date_Exception
  2807. */
  2808. public function addArpa($date, $locale = null)
  2809. {
  2810. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2811. }
  2812. /**
  2813. * Subtracts a RFC 822 date from the date object.
  2814. * ARPA messages are used in emails or HTTP Headers.
  2815. * Only RFC 822 compilant date strings are accepted.
  2816. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2817. * Returned is the new date object
  2818. *
  2819. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2820. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2821. * @return Zend_Date Provides fluid interface
  2822. * @throws Zend_Date_Exception
  2823. */
  2824. public function subArpa($date, $locale = null)
  2825. {
  2826. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2827. }
  2828. /**
  2829. * Compares a RFC 822 compilant date with the date object.
  2830. * ARPA messages are used in emails or HTTP Headers.
  2831. * Only RFC 822 compilant date strings are accepted.
  2832. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2833. * Returns if equal, earlier or later
  2834. *
  2835. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2836. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2837. * @return integer 0 = equal, 1 = later, -1 = earlier
  2838. * @throws Zend_Date_Exception
  2839. */
  2840. public function compareArpa($date, $locale = null)
  2841. {
  2842. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2843. }
  2844. /**
  2845. * Check if location is supported
  2846. *
  2847. * @param $location array - locations array
  2848. * @return $horizon float
  2849. */
  2850. private function _checkLocation($location)
  2851. {
  2852. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2853. require_once 'Zend/Date/Exception.php';
  2854. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
  2855. }
  2856. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2857. require_once 'Zend/Date/Exception.php';
  2858. throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
  2859. }
  2860. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2861. require_once 'Zend/Date/Exception.php';
  2862. throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
  2863. }
  2864. if (!isset($location['horizon'])){
  2865. $location['horizon'] = 'effective';
  2866. }
  2867. switch ($location['horizon']) {
  2868. case 'civil' :
  2869. return -0.104528;
  2870. break;
  2871. case 'nautic' :
  2872. return -0.207912;
  2873. break;
  2874. case 'astronomic' :
  2875. return -0.309017;
  2876. break;
  2877. default :
  2878. return -0.0145439;
  2879. break;
  2880. }
  2881. }
  2882. /**
  2883. * Returns the time of sunrise for this date and a given location as new date object
  2884. * For a list of cities and correct locations use the class Zend_Date_Cities
  2885. *
  2886. * @param $location array - location of sunrise
  2887. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2888. * ['longitude'] -> longitude of location
  2889. * ['latitude'] -> latitude of location
  2890. * @return Zend_Date
  2891. * @throws Zend_Date_Exception
  2892. */
  2893. public function getSunrise($location)
  2894. {
  2895. $horizon = $this->_checkLocation($location);
  2896. $result = clone $this;
  2897. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2898. return $result;
  2899. }
  2900. /**
  2901. * Returns the time of sunset for this date and a given location as new date object
  2902. * For a list of cities and correct locations use the class Zend_Date_Cities
  2903. *
  2904. * @param $location array - location of sunset
  2905. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2906. * ['longitude'] -> longitude of location
  2907. * ['latitude'] -> latitude of location
  2908. * @return Zend_Date
  2909. * @throws Zend_Date_Exception
  2910. */
  2911. public function getSunset($location)
  2912. {
  2913. $horizon = $this->_checkLocation($location);
  2914. $result = clone $this;
  2915. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2916. return $result;
  2917. }
  2918. /**
  2919. * Returns an array with the sunset and sunrise dates for all horizon types
  2920. * For a list of cities and correct locations use the class Zend_Date_Cities
  2921. *
  2922. * @param $location array - location of suninfo
  2923. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2924. * ['longitude'] -> longitude of location
  2925. * ['latitude'] -> latitude of location
  2926. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2927. * @throws Zend_Date_Exception
  2928. */
  2929. public function getSunInfo($location)
  2930. {
  2931. $suninfo = array();
  2932. for ($i = 0; $i < 4; ++$i) {
  2933. switch ($i) {
  2934. case 0 :
  2935. $location['horizon'] = 'effective';
  2936. break;
  2937. case 1 :
  2938. $location['horizon'] = 'civil';
  2939. break;
  2940. case 2 :
  2941. $location['horizon'] = 'nautic';
  2942. break;
  2943. case 3 :
  2944. $location['horizon'] = 'astronomic';
  2945. break;
  2946. }
  2947. $horizon = $this->_checkLocation($location);
  2948. $result = clone $this;
  2949. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2950. $suninfo['sunrise'][$location['horizon']] = $result;
  2951. $result = clone $this;
  2952. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2953. $suninfo['sunset'][$location['horizon']] = $result;
  2954. }
  2955. return $suninfo;
  2956. }
  2957. /**
  2958. * Check a given year for leap year.
  2959. *
  2960. * @param integer|array|Zend_Date $year Year to check
  2961. * @return boolean
  2962. */
  2963. public static function checkLeapYear($year)
  2964. {
  2965. if ($year instanceof Zend_Date) {
  2966. $year = (int) $year->toString(self::YEAR, 'iso');
  2967. }
  2968. if (is_array($year)) {
  2969. if (isset($year['year']) === true) {
  2970. $year = $year['year'];
  2971. } else {
  2972. require_once 'Zend/Date/Exception.php';
  2973. throw new Zend_Date_Exception("no year given in array");
  2974. }
  2975. }
  2976. if (!is_numeric($year)) {
  2977. require_once 'Zend/Date/Exception.php';
  2978. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
  2979. }
  2980. return (bool) parent::isYearLeapYear($year);
  2981. }
  2982. /**
  2983. * Returns true, if the year is a leap year.
  2984. *
  2985. * @return boolean
  2986. */
  2987. public function isLeapYear()
  2988. {
  2989. return self::checkLeapYear($this);
  2990. }
  2991. /**
  2992. * Returns if the set date is todays date
  2993. *
  2994. * @return boolean
  2995. */
  2996. public function isToday()
  2997. {
  2998. $today = $this->date('Ymd', $this->_getTime());
  2999. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3000. return ($today == $day);
  3001. }
  3002. /**
  3003. * Returns if the set date is yesterdays date
  3004. *
  3005. * @return boolean
  3006. */
  3007. public function isYesterday()
  3008. {
  3009. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3010. // adjusts for leap days and DST changes that are timezone specific
  3011. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  3012. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3013. return $day == $yesterday;
  3014. }
  3015. /**
  3016. * Returns if the set date is tomorrows date
  3017. *
  3018. * @return boolean
  3019. */
  3020. public function isTomorrow()
  3021. {
  3022. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3023. // adjusts for leap days and DST changes that are timezone specific
  3024. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  3025. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3026. return $day == $tomorrow;
  3027. }
  3028. /**
  3029. * Returns the actual date as new date object
  3030. *
  3031. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3032. * @return Zend_Date
  3033. */
  3034. public static function now($locale = null)
  3035. {
  3036. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  3037. }
  3038. /**
  3039. * Calculate date details
  3040. *
  3041. * @param string $calc Calculation to make
  3042. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  3043. * @param string $part Datepart for Calculation
  3044. * @param string|Zend_Locale $locale Locale for parsing input
  3045. * @return integer|string new date
  3046. * @throws Zend_Date_Exception
  3047. */
  3048. private function _calcdetail($calc, $date, $type, $locale)
  3049. {
  3050. $old = false;
  3051. if (self::$_options['format_type'] == 'php') {
  3052. self::$_options['format_type'] = 'iso';
  3053. $old = true;
  3054. }
  3055. switch($calc) {
  3056. case 'set' :
  3057. $return = $this->set($date, $type, $locale);
  3058. break;
  3059. case 'add' :
  3060. $return = $this->add($date, $type, $locale);
  3061. break;
  3062. case 'sub' :
  3063. $return = $this->sub($date, $type, $locale);
  3064. break;
  3065. default :
  3066. $return = $this->compare($date, $type, $locale);
  3067. break;
  3068. }
  3069. if ($old) {
  3070. self::$_options['format_type'] = 'php';
  3071. }
  3072. return $return;
  3073. }
  3074. /**
  3075. * Internal calculation, returns the requested date type
  3076. *
  3077. * @param string $calc Calculation to make
  3078. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  3079. * @param string|Zend_Locale $locale Locale for parsing input
  3080. * @return integer|Zend_Date new date
  3081. * @throws Zend_Date_Exception
  3082. */
  3083. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  3084. {
  3085. if ($value === null) {
  3086. require_once 'Zend/Date/Exception.php';
  3087. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  3088. }
  3089. if ($locale === null) {
  3090. $locale = $this->getLocale();
  3091. }
  3092. if ($value instanceof Zend_Date) {
  3093. // extract value from object
  3094. $value = $value->toString($parameter, 'iso', $locale);
  3095. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  3096. require_once 'Zend/Date/Exception.php';
  3097. throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
  3098. }
  3099. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  3100. if ($calc != 'cmp') {
  3101. return $this;
  3102. }
  3103. return $return;
  3104. }
  3105. /**
  3106. * Returns only the year from the date object as new object.
  3107. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  3108. *
  3109. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3110. * @return Zend_Date
  3111. */
  3112. public function getYear($locale = null)
  3113. {
  3114. if (self::$_options['format_type'] == 'php') {
  3115. $format = 'Y';
  3116. } else {
  3117. $format = self::YEAR;
  3118. }
  3119. return $this->copyPart($format, $locale);
  3120. }
  3121. /**
  3122. * Sets a new year
  3123. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  3124. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  3125. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  3126. * use set() instead.
  3127. * Returned is the new date object
  3128. *
  3129. * @param string|integer|array|Zend_Date $date Year to set
  3130. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3131. * @return Zend_Date Provides fluid interface
  3132. * @throws Zend_Date_Exception
  3133. */
  3134. public function setYear($year, $locale = null)
  3135. {
  3136. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  3137. }
  3138. /**
  3139. * Adds the year to the existing date object
  3140. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  3141. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  3142. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  3143. * use add() instead.
  3144. * Returned is the new date object
  3145. *
  3146. * @param string|integer|array|Zend_Date $date Year to add
  3147. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3148. * @return Zend_Date Provides fluid interface
  3149. * @throws Zend_Date_Exception
  3150. */
  3151. public function addYear($year, $locale = null)
  3152. {
  3153. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  3154. }
  3155. /**
  3156. * Subs the year from the existing date object
  3157. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  3158. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  3159. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  3160. * use sub() instead.
  3161. * Returned is the new date object
  3162. *
  3163. * @param string|integer|array|Zend_Date $date Year to sub
  3164. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3165. * @return Zend_Date Provides fluid interface
  3166. * @throws Zend_Date_Exception
  3167. */
  3168. public function subYear($year, $locale = null)
  3169. {
  3170. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  3171. }
  3172. /**
  3173. * Compares the year with the existing date object, ignoring other date parts.
  3174. * For example: 10.03.2000 -> 15.02.2000 -> true
  3175. * Returns if equal, earlier or later
  3176. *
  3177. * @param string|integer|array|Zend_Date $year Year to compare
  3178. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3179. * @return integer 0 = equal, 1 = later, -1 = earlier
  3180. * @throws Zend_Date_Exception
  3181. */
  3182. public function compareYear($year, $locale = null)
  3183. {
  3184. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  3185. }
  3186. /**
  3187. * Returns only the month from the date object as new object.
  3188. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  3189. *
  3190. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3191. * @return Zend_Date
  3192. */
  3193. public function getMonth($locale = null)
  3194. {
  3195. if (self::$_options['format_type'] == 'php') {
  3196. $format = 'm';
  3197. } else {
  3198. $format = self::MONTH;
  3199. }
  3200. return $this->copyPart($format, $locale);
  3201. }
  3202. /**
  3203. * Returns the calculated month
  3204. *
  3205. * @param string $calc Calculation to make
  3206. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  3207. * @param string|Zend_Locale $locale Locale for parsing input
  3208. * @return integer|Zend_Date new time
  3209. * @throws Zend_Date_Exception
  3210. */
  3211. private function _month($calc, $month, $locale)
  3212. {
  3213. if ($month === null) {
  3214. require_once 'Zend/Date/Exception.php';
  3215. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  3216. }
  3217. if ($locale === null) {
  3218. $locale = $this->getLocale();
  3219. }
  3220. if ($month instanceof Zend_Date) {
  3221. // extract month from object
  3222. $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
  3223. } else {
  3224. if (is_numeric($month)) {
  3225. $found = $month;
  3226. } else if (is_array($month)) {
  3227. if (isset($month['month']) === true) {
  3228. $month = $month['month'];
  3229. } else {
  3230. require_once 'Zend/Date/Exception.php';
  3231. throw new Zend_Date_Exception("no month given in array");
  3232. }
  3233. } else {
  3234. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  3235. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  3236. $monthlist = array_merge($monthlist, $monthlist2);
  3237. $found = 0;
  3238. $cnt = 0;
  3239. foreach ($monthlist as $key => $value) {
  3240. if (strtoupper($value) == strtoupper($month)) {
  3241. $found = ($key % 12) + 1;
  3242. break;
  3243. }
  3244. ++$cnt;
  3245. }
  3246. if ($found == 0) {
  3247. foreach ($monthlist2 as $key => $value) {
  3248. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
  3249. $found = $key + 1;
  3250. break;
  3251. }
  3252. ++$cnt;
  3253. }
  3254. }
  3255. if ($found == 0) {
  3256. require_once 'Zend/Date/Exception.php';
  3257. throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
  3258. }
  3259. }
  3260. }
  3261. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3262. if ($calc != 'cmp') {
  3263. return $this;
  3264. }
  3265. return $return;
  3266. }
  3267. /**
  3268. * Sets a new month
  3269. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3270. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3271. * If a localized monthname is given it will be parsed with the default locale or the optional
  3272. * set locale.
  3273. * Returned is the new date object
  3274. *
  3275. * @param string|integer|array|Zend_Date $month Month to set
  3276. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3277. * @return Zend_Date Provides fluid interface
  3278. * @throws Zend_Date_Exception
  3279. */
  3280. public function setMonth($month, $locale = null)
  3281. {
  3282. return $this->_month('set', $month, $locale);
  3283. }
  3284. /**
  3285. * Adds months to the existing date object.
  3286. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3287. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3288. * If a localized monthname is given it will be parsed with the default locale or the optional
  3289. * set locale.
  3290. * Returned is the new date object
  3291. *
  3292. * @param string|integer|array|Zend_Date $month Month to add
  3293. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3294. * @return Zend_Date Provides fluid interface
  3295. * @throws Zend_Date_Exception
  3296. */
  3297. public function addMonth($month, $locale = null)
  3298. {
  3299. return $this->_month('add', $month, $locale);
  3300. }
  3301. /**
  3302. * Subtracts months from the existing date object.
  3303. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3304. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3305. * If a localized monthname is given it will be parsed with the default locale or the optional
  3306. * set locale.
  3307. * Returned is the new date object
  3308. *
  3309. * @param string|integer|array|Zend_Date $month Month to sub
  3310. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3311. * @return Zend_Date Provides fluid interface
  3312. * @throws Zend_Date_Exception
  3313. */
  3314. public function subMonth($month, $locale = null)
  3315. {
  3316. return $this->_month('sub', $month, $locale);
  3317. }
  3318. /**
  3319. * Compares the month with the existing date object, ignoring other date parts.
  3320. * For example: 10.03.2000 -> 15.03.1950 -> true
  3321. * Returns if equal, earlier or later
  3322. *
  3323. * @param string|integer|array|Zend_Date $month Month to compare
  3324. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3325. * @return integer 0 = equal, 1 = later, -1 = earlier
  3326. * @throws Zend_Date_Exception
  3327. */
  3328. public function compareMonth($month, $locale = null)
  3329. {
  3330. return $this->_month('cmp', $month, $locale);
  3331. }
  3332. /**
  3333. * Returns the day as new date object
  3334. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3335. *
  3336. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3337. * @return Zend_Date
  3338. */
  3339. public function getDay($locale = null)
  3340. {
  3341. return $this->copyPart(self::DAY_SHORT, $locale);
  3342. }
  3343. /**
  3344. * Returns the calculated day
  3345. *
  3346. * @param $calc string Type of calculation to make
  3347. * @param $day string|integer|Zend_Date Day to calculate, when null the actual day is calculated
  3348. * @param $locale string|Zend_Locale Locale for parsing input
  3349. * @return Zend_Date|integer
  3350. */
  3351. private function _day($calc, $day, $locale)
  3352. {
  3353. if ($day === null) {
  3354. require_once 'Zend/Date/Exception.php';
  3355. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3356. }
  3357. if ($locale === null) {
  3358. $locale = $this->getLocale();
  3359. }
  3360. if ($day instanceof Zend_Date) {
  3361. $day = $day->toString(self::DAY_SHORT, 'iso', $locale);
  3362. }
  3363. if (is_numeric($day)) {
  3364. $type = self::DAY_SHORT;
  3365. } else if (is_array($day)) {
  3366. if (isset($day['day']) === true) {
  3367. $day = $day['day'];
  3368. $type = self::WEEKDAY;
  3369. } else {
  3370. require_once 'Zend/Date/Exception.php';
  3371. throw new Zend_Date_Exception("no day given in array");
  3372. }
  3373. } else {
  3374. switch (iconv_strlen($day, 'UTF-8')) {
  3375. case 1 :
  3376. $type = self::WEEKDAY_NARROW;
  3377. break;
  3378. case 2:
  3379. $type = self::WEEKDAY_NAME;
  3380. break;
  3381. case 3:
  3382. $type = self::WEEKDAY_SHORT;
  3383. break;
  3384. default:
  3385. $type = self::WEEKDAY;
  3386. break;
  3387. }
  3388. }
  3389. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3390. if ($calc != 'cmp') {
  3391. return $this;
  3392. }
  3393. return $return;
  3394. }
  3395. /**
  3396. * Sets a new day
  3397. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3398. * will result in adding or subtracting the relevant month.
  3399. * If a localized dayname is given it will be parsed with the default locale or the optional
  3400. * set locale.
  3401. * Returned is the new date object
  3402. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3403. *
  3404. * @param string|integer|array|Zend_Date $month Day to set
  3405. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3406. * @return Zend_Date Provides fluid interface
  3407. * @throws Zend_Date_Exception
  3408. */
  3409. public function setDay($day, $locale = null)
  3410. {
  3411. return $this->_day('set', $day, $locale);
  3412. }
  3413. /**
  3414. * Adds days to the existing date object.
  3415. * The day can be a number or a string. Adding days lower then 0 or greater than the number of this months days
  3416. * will result in adding or subtracting the relevant month.
  3417. * If a localized dayname is given it will be parsed with the default locale or the optional
  3418. * set locale.
  3419. *
  3420. * @param string|integer|array|Zend_Date $month Day to add
  3421. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3422. * @return Zend_Date Provides fluid interface
  3423. * @throws Zend_Date_Exception
  3424. */
  3425. public function addDay($day, $locale = null)
  3426. {
  3427. return $this->_day('add', $day, $locale);
  3428. }
  3429. /**
  3430. * Subtracts days from the existing date object.
  3431. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3432. * will result in adding or subtracting the relevant month.
  3433. * If a localized dayname is given it will be parsed with the default locale or the optional
  3434. * set locale.
  3435. *
  3436. * @param string|integer|array|Zend_Date $month Day to sub
  3437. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3438. * @return Zend_Date Provides fluid interface
  3439. * @throws Zend_Date_Exception
  3440. */
  3441. public function subDay($day, $locale = null)
  3442. {
  3443. return $this->_day('sub', $day, $locale);
  3444. }
  3445. /**
  3446. * Compares the day with the existing date object, ignoring other date parts.
  3447. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3448. * Returns if equal, earlier or later
  3449. *
  3450. * @param string|integer|array|Zend_Date $day Day to compare
  3451. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3452. * @return integer 0 = equal, 1 = later, -1 = earlier
  3453. * @throws Zend_Date_Exception
  3454. */
  3455. public function compareDay($day, $locale = null)
  3456. {
  3457. return $this->_day('cmp', $day, $locale);
  3458. }
  3459. /**
  3460. * Returns the weekday as new date object
  3461. * Weekday is always from 1-7
  3462. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3463. *
  3464. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3465. * @return Zend_Date
  3466. */
  3467. public function getWeekday($locale = null)
  3468. {
  3469. if (self::$_options['format_type'] == 'php') {
  3470. $format = 'l';
  3471. } else {
  3472. $format = self::WEEKDAY;
  3473. }
  3474. return $this->copyPart($format, $locale);
  3475. }
  3476. /**
  3477. * Returns the calculated weekday
  3478. *
  3479. * @param $calc string Type of calculation to make
  3480. * @param $weekday string|integer|array|Zend_Date Weekday to calculate, when null the actual weekday is calculated
  3481. * @param $locale string|Zend_Locale Locale for parsing input
  3482. * @return Zend_Date|integer
  3483. * @throws Zend_Date_Exception
  3484. */
  3485. private function _weekday($calc, $weekday, $locale)
  3486. {
  3487. if ($weekday === null) {
  3488. require_once 'Zend/Date/Exception.php';
  3489. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3490. }
  3491. if ($locale === null) {
  3492. $locale = $this->getLocale();
  3493. }
  3494. if ($weekday instanceof Zend_Date) {
  3495. $weekday = $weekday->toString(self::WEEKDAY_8601, 'iso', $locale);
  3496. }
  3497. if (is_numeric($weekday)) {
  3498. $type = self::WEEKDAY_8601;
  3499. } else if (is_array($weekday)) {
  3500. if (isset($weekday['weekday']) === true) {
  3501. $weekday = $weekday['weekday'];
  3502. $type = self::WEEKDAY;
  3503. } else {
  3504. require_once 'Zend/Date/Exception.php';
  3505. throw new Zend_Date_Exception("no weekday given in array");
  3506. }
  3507. } else {
  3508. switch(iconv_strlen($weekday, 'UTF-8')) {
  3509. case 1:
  3510. $type = self::WEEKDAY_NARROW;
  3511. break;
  3512. case 2:
  3513. $type = self::WEEKDAY_NAME;
  3514. break;
  3515. case 3:
  3516. $type = self::WEEKDAY_SHORT;
  3517. break;
  3518. default:
  3519. $type = self::WEEKDAY;
  3520. break;
  3521. }
  3522. }
  3523. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3524. if ($calc != 'cmp') {
  3525. return $this;
  3526. }
  3527. return $return;
  3528. }
  3529. /**
  3530. * Sets a new weekday
  3531. * The weekday can be a number or a string. If a localized weekday name is given,
  3532. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3533. * Returned is the new date object.
  3534. * Example: setWeekday(3); will set the wednesday of this week as day.
  3535. *
  3536. * @param string|integer|array|Zend_Date $month Weekday to set
  3537. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3538. * @return Zend_Date Provides fluid interface
  3539. * @throws Zend_Date_Exception
  3540. */
  3541. public function setWeekday($weekday, $locale = null)
  3542. {
  3543. return $this->_weekday('set', $weekday, $locale);
  3544. }
  3545. /**
  3546. * Adds weekdays to the existing date object.
  3547. * The weekday can be a number or a string.
  3548. * If a localized dayname is given it will be parsed with the default locale or the optional
  3549. * set locale.
  3550. * Returned is the new date object
  3551. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3552. * wednesday.
  3553. *
  3554. * @param string|integer|array|Zend_Date $month Weekday to add
  3555. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3556. * @return Zend_Date Provides fluid interface
  3557. * @throws Zend_Date_Exception
  3558. */
  3559. public function addWeekday($weekday, $locale = null)
  3560. {
  3561. return $this->_weekday('add', $weekday, $locale);
  3562. }
  3563. /**
  3564. * Subtracts weekdays from the existing date object.
  3565. * The weekday can be a number or a string.
  3566. * If a localized dayname is given it will be parsed with the default locale or the optional
  3567. * set locale.
  3568. * Returned is the new date object
  3569. * Example: subWeekday(3); will subtract the difference of days from the begining of the month until
  3570. * wednesday.
  3571. *
  3572. * @param string|integer|array|Zend_Date $month Weekday to sub
  3573. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3574. * @return Zend_Date Provides fluid interface
  3575. * @throws Zend_Date_Exception
  3576. */
  3577. public function subWeekday($weekday, $locale = null)
  3578. {
  3579. return $this->_weekday('sub', $weekday, $locale);
  3580. }
  3581. /**
  3582. * Compares the weekday with the existing date object, ignoring other date parts.
  3583. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3584. * Returns if equal, earlier or later
  3585. *
  3586. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3587. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3588. * @return integer 0 = equal, 1 = later, -1 = earlier
  3589. * @throws Zend_Date_Exception
  3590. */
  3591. public function compareWeekday($weekday, $locale = null)
  3592. {
  3593. return $this->_weekday('cmp', $weekday, $locale);
  3594. }
  3595. /**
  3596. * Returns the day of year as new date object
  3597. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3598. *
  3599. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3600. * @return Zend_Date
  3601. */
  3602. public function getDayOfYear($locale = null)
  3603. {
  3604. if (self::$_options['format_type'] == 'php') {
  3605. $format = 'D';
  3606. } else {
  3607. $format = self::DAY_OF_YEAR;
  3608. }
  3609. return $this->copyPart($format, $locale);
  3610. }
  3611. /**
  3612. * Sets a new day of year
  3613. * The day of year is always a number.
  3614. * Returned is the new date object
  3615. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3616. *
  3617. * @param string|integer|array|Zend_Date $day Day of Year to set
  3618. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3619. * @return Zend_Date Provides fluid interface
  3620. * @throws Zend_Date_Exception
  3621. */
  3622. public function setDayOfYear($day, $locale = null)
  3623. {
  3624. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3625. }
  3626. /**
  3627. * Adds a day of year to the existing date object.
  3628. * The day of year is always a number.
  3629. * Returned is the new date object
  3630. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3631. *
  3632. * @param string|integer|array|Zend_Date $day Day of Year to add
  3633. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3634. * @return Zend_Date Provides fluid interface
  3635. * @throws Zend_Date_Exception
  3636. */
  3637. public function addDayOfYear($day, $locale = null)
  3638. {
  3639. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3640. }
  3641. /**
  3642. * Subtracts a day of year from the existing date object.
  3643. * The day of year is always a number.
  3644. * Returned is the new date object
  3645. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3646. *
  3647. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3648. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3649. * @return Zend_Date Provides fluid interface
  3650. * @throws Zend_Date_Exception
  3651. */
  3652. public function subDayOfYear($day, $locale = null)
  3653. {
  3654. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3655. }
  3656. /**
  3657. * Compares the day of year with the existing date object.
  3658. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3659. * Returns if equal, earlier or later
  3660. *
  3661. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3662. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3663. * @return integer 0 = equal, 1 = later, -1 = earlier
  3664. * @throws Zend_Date_Exception
  3665. */
  3666. public function compareDayOfYear($day, $locale = null)
  3667. {
  3668. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3669. }
  3670. /**
  3671. * Returns the hour as new date object
  3672. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3673. *
  3674. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3675. * @return Zend_Date
  3676. */
  3677. public function getHour($locale = null)
  3678. {
  3679. return $this->copyPart(self::HOUR, $locale);
  3680. }
  3681. /**
  3682. * Sets a new hour
  3683. * The hour is always a number.
  3684. * Returned is the new date object
  3685. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3686. *
  3687. * @param string|integer|array|Zend_Date $hour Hour to set
  3688. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3689. * @return Zend_Date Provides fluid interface
  3690. * @throws Zend_Date_Exception
  3691. */
  3692. public function setHour($hour, $locale = null)
  3693. {
  3694. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3695. }
  3696. /**
  3697. * Adds hours to the existing date object.
  3698. * The hour is always a number.
  3699. * Returned is the new date object
  3700. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3701. *
  3702. * @param string|integer|array|Zend_Date $hour Hour to add
  3703. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3704. * @return Zend_Date Provides fluid interface
  3705. * @throws Zend_Date_Exception
  3706. */
  3707. public function addHour($hour, $locale = null)
  3708. {
  3709. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3710. }
  3711. /**
  3712. * Subtracts hours from the existing date object.
  3713. * The hour is always a number.
  3714. * Returned is the new date object
  3715. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3716. *
  3717. * @param string|integer|array|Zend_Date $hour Hour to sub
  3718. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3719. * @return Zend_Date Provides fluid interface
  3720. * @throws Zend_Date_Exception
  3721. */
  3722. public function subHour($hour, $locale = null)
  3723. {
  3724. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3725. }
  3726. /**
  3727. * Compares the hour with the existing date object.
  3728. * For example: 10:30:25 -> compareHour(10) -> 0
  3729. * Returns if equal, earlier or later
  3730. *
  3731. * @param string|integer|array|Zend_Date $hour Hour to compare
  3732. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3733. * @return integer 0 = equal, 1 = later, -1 = earlier
  3734. * @throws Zend_Date_Exception
  3735. */
  3736. public function compareHour($hour, $locale = null)
  3737. {
  3738. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3739. }
  3740. /**
  3741. * Returns the minute as new date object
  3742. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3743. *
  3744. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3745. * @return Zend_Date
  3746. */
  3747. public function getMinute($locale = null)
  3748. {
  3749. if (self::$_options['format_type'] == 'php') {
  3750. $format = 'i';
  3751. } else {
  3752. $format = self::MINUTE;
  3753. }
  3754. return $this->copyPart($format, $locale);
  3755. }
  3756. /**
  3757. * Sets a new minute
  3758. * The minute is always a number.
  3759. * Returned is the new date object
  3760. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3761. *
  3762. * @param string|integer|array|Zend_Date $minute Minute to set
  3763. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3764. * @return Zend_Date Provides fluid interface
  3765. * @throws Zend_Date_Exception
  3766. */
  3767. public function setMinute($minute, $locale = null)
  3768. {
  3769. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3770. }
  3771. /**
  3772. * Adds minutes to the existing date object.
  3773. * The minute is always a number.
  3774. * Returned is the new date object
  3775. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3776. *
  3777. * @param string|integer|array|Zend_Date $minute Minute to add
  3778. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3779. * @return Zend_Date Provides fluid interface
  3780. * @throws Zend_Date_Exception
  3781. */
  3782. public function addMinute($minute, $locale = null)
  3783. {
  3784. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3785. }
  3786. /**
  3787. * Subtracts minutes from the existing date object.
  3788. * The minute is always a number.
  3789. * Returned is the new date object
  3790. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3791. *
  3792. * @param string|integer|array|Zend_Date $minute Minute to sub
  3793. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3794. * @return Zend_Date Provides fluid interface
  3795. * @throws Zend_Date_Exception
  3796. */
  3797. public function subMinute($minute, $locale = null)
  3798. {
  3799. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3800. }
  3801. /**
  3802. * Compares the minute with the existing date object.
  3803. * For example: 10:30:25 -> compareMinute(30) -> 0
  3804. * Returns if equal, earlier or later
  3805. *
  3806. * @param string|integer|array|Zend_Date $minute Hour to compare
  3807. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3808. * @return integer 0 = equal, 1 = later, -1 = earlier
  3809. * @throws Zend_Date_Exception
  3810. */
  3811. public function compareMinute($minute, $locale = null)
  3812. {
  3813. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3814. }
  3815. /**
  3816. * Returns the second as new date object
  3817. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3818. *
  3819. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3820. * @return Zend_Date
  3821. */
  3822. public function getSecond($locale = null)
  3823. {
  3824. if (self::$_options['format_type'] == 'php') {
  3825. $format = 's';
  3826. } else {
  3827. $format = self::SECOND;
  3828. }
  3829. return $this->copyPart($format, $locale);
  3830. }
  3831. /**
  3832. * Sets new seconds to the existing date object.
  3833. * The second is always a number.
  3834. * Returned is the new date object
  3835. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3836. *
  3837. * @param string|integer|array|Zend_Date $second Second to set
  3838. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3839. * @return Zend_Date Provides fluid interface
  3840. * @throws Zend_Date_Exception
  3841. */
  3842. public function setSecond($second, $locale = null)
  3843. {
  3844. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3845. }
  3846. /**
  3847. * Adds seconds to the existing date object.
  3848. * The second is always a number.
  3849. * Returned is the new date object
  3850. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3851. *
  3852. * @param string|integer|array|Zend_Date $second Second to add
  3853. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3854. * @return Zend_Date Provides fluid interface
  3855. * @throws Zend_Date_Exception
  3856. */
  3857. public function addSecond($second, $locale = null)
  3858. {
  3859. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3860. }
  3861. /**
  3862. * Subtracts seconds from the existing date object.
  3863. * The second is always a number.
  3864. * Returned is the new date object
  3865. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3866. *
  3867. * @param string|integer|array|Zend_Date $second Second to sub
  3868. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3869. * @return Zend_Date Provides fluid interface
  3870. * @throws Zend_Date_Exception
  3871. */
  3872. public function subSecond($second, $locale = null)
  3873. {
  3874. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3875. }
  3876. /**
  3877. * Compares the second with the existing date object.
  3878. * For example: 10:30:25 -> compareSecond(25) -> 0
  3879. * Returns if equal, earlier or later
  3880. *
  3881. * @param string|integer|array|Zend_Date $second Second to compare
  3882. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3883. * @return integer 0 = equal, 1 = later, -1 = earlier
  3884. * @throws Zend_Date_Exception
  3885. */
  3886. public function compareSecond($second, $locale = null)
  3887. {
  3888. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3889. }
  3890. /**
  3891. * Returns the precision for fractional seconds
  3892. *
  3893. * @return integer
  3894. */
  3895. public function getFractionalPrecision()
  3896. {
  3897. return $this->_precision;
  3898. }
  3899. /**
  3900. * Sets a new precision for fractional seconds
  3901. *
  3902. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3903. * @throws Zend_Date_Exception
  3904. * @return Zend_Date Provides fluid interface
  3905. */
  3906. public function setFractionalPrecision($precision)
  3907. {
  3908. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3909. require_once 'Zend/Date/Exception.php';
  3910. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3911. }
  3912. $this->_precision = (int) $precision;
  3913. if ($this->_precision < strlen($this->_fractional)) {
  3914. $this->_fractional = substr($this->_fractional, 0, $this->_precision);
  3915. } else {
  3916. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_RIGHT);
  3917. }
  3918. return $this;
  3919. }
  3920. /**
  3921. * Returns the milliseconds of the date object
  3922. *
  3923. * @return string
  3924. */
  3925. public function getMilliSecond()
  3926. {
  3927. return $this->_fractional;
  3928. }
  3929. /**
  3930. * Sets new milliseconds for the date object
  3931. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3932. *
  3933. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3934. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3935. * @return Zend_Date Provides fluid interface
  3936. */
  3937. public function setMilliSecond($milli = null, $precision = null)
  3938. {
  3939. if ($milli === null) {
  3940. list($milli, $time) = explode(" ", microtime());
  3941. $milli = intval($milli);
  3942. $precision = 6;
  3943. } else if (!is_numeric($milli)) {
  3944. require_once 'Zend/Date/Exception.php';
  3945. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3946. }
  3947. if ($precision === null) {
  3948. $precision = $this->_precision;
  3949. }
  3950. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3951. require_once 'Zend/Date/Exception.php';
  3952. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3953. }
  3954. $this->_fractional = 0;
  3955. $this->addMilliSecond($milli, $precision);
  3956. return $this;
  3957. }
  3958. /**
  3959. * Adds milliseconds to the date object
  3960. *
  3961. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3962. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3963. * @return Zend_Date Provides fluid interface
  3964. */
  3965. public function addMilliSecond($milli = null, $precision = null)
  3966. {
  3967. if ($milli === null) {
  3968. list($milli, $time) = explode(" ", microtime());
  3969. $milli = intval($milli);
  3970. } else if (!is_numeric($milli)) {
  3971. require_once 'Zend/Date/Exception.php';
  3972. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3973. }
  3974. if ($precision === null) {
  3975. $precision = strlen($milli);
  3976. if ($milli < 0) {
  3977. --$precision;
  3978. }
  3979. }
  3980. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3981. require_once 'Zend/Date/Exception.php';
  3982. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3983. }
  3984. $this->_fractional += $milli;
  3985. // Add/sub milliseconds + add/sub seconds
  3986. $max = pow(10, $this->_precision);
  3987. // Milli includes seconds
  3988. if ($this->_fractional >= $max) {
  3989. while ($this->_fractional >= $max) {
  3990. $this->addSecond(1);
  3991. $this->_fractional -= $max;
  3992. }
  3993. }
  3994. if ($this->_fractional < 0) {
  3995. while ($this->_fractional < 0) {
  3996. $this->subSecond(1);
  3997. $this->_fractional += $max;
  3998. }
  3999. }
  4000. if ($this->_precision > strlen($this->_fractional)) {
  4001. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_LEFT);
  4002. }
  4003. return $this;
  4004. }
  4005. /**
  4006. * Subtracts a millisecond
  4007. *
  4008. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  4009. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  4010. * @return Zend_Date Provides fluid interface
  4011. */
  4012. public function subMilliSecond($milli = null, $precision = null)
  4013. {
  4014. $this->addMilliSecond(0 - $milli, $precision);
  4015. return $this;
  4016. }
  4017. /**
  4018. * Compares only the millisecond part, returning the difference
  4019. *
  4020. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  4021. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  4022. * @throws Zend_Date_Exception On invalid input
  4023. * @return integer 0 = equal, 1 = later, -1 = earlier
  4024. */
  4025. public function compareMilliSecond($milli = null, $precision = null)
  4026. {
  4027. if ($milli === null) {
  4028. list($milli, $time) = explode(" ", microtime());
  4029. $milli = intval($milli);
  4030. } else if (is_numeric($milli) === false) {
  4031. require_once 'Zend/Date/Exception.php';
  4032. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  4033. }
  4034. if ($precision === null) {
  4035. $precision = strlen($milli);
  4036. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  4037. require_once 'Zend/Date/Exception.php';
  4038. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  4039. }
  4040. if ($precision === 0) {
  4041. require_once 'Zend/Date/Exception.php';
  4042. throw new Zend_Date_Exception('precision is 0');
  4043. }
  4044. if ($precision != $this->_precision) {
  4045. if ($precision > $this->_precision) {
  4046. $diff = $precision - $this->_precision;
  4047. $milli = (int) ($milli / (10 * $diff));
  4048. } else {
  4049. $diff = $this->_precision - $precision;
  4050. $milli = (int) ($milli * (10 * $diff));
  4051. }
  4052. }
  4053. $comp = $this->_fractional - $milli;
  4054. if ($comp < 0) {
  4055. return -1;
  4056. } else if ($comp > 0) {
  4057. return 1;
  4058. }
  4059. return 0;
  4060. }
  4061. /**
  4062. * Returns the week as new date object using monday as begining of the week
  4063. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  4064. *
  4065. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  4066. * @return Zend_Date
  4067. */
  4068. public function getWeek($locale = null)
  4069. {
  4070. if (self::$_options['format_type'] == 'php') {
  4071. $format = 'W';
  4072. } else {
  4073. $format = self::WEEK;
  4074. }
  4075. return $this->copyPart($format, $locale);
  4076. }
  4077. /**
  4078. * Sets a new week. The week is always a number. The day of week is not changed.
  4079. * Returned is the new date object
  4080. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  4081. *
  4082. * @param string|integer|array|Zend_Date $week Week to set
  4083. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4084. * @return Zend_Date Provides fluid interface
  4085. * @throws Zend_Date_Exception
  4086. */
  4087. public function setWeek($week, $locale = null)
  4088. {
  4089. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  4090. }
  4091. /**
  4092. * Adds a week. The week is always a number. The day of week is not changed.
  4093. * Returned is the new date object
  4094. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  4095. *
  4096. * @param string|integer|array|Zend_Date $week Week to add
  4097. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4098. * @return Zend_Date Provides fluid interface
  4099. * @throws Zend_Date_Exception
  4100. */
  4101. public function addWeek($week, $locale = null)
  4102. {
  4103. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  4104. }
  4105. /**
  4106. * Subtracts a week. The week is always a number. The day of week is not changed.
  4107. * Returned is the new date object
  4108. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  4109. *
  4110. * @param string|integer|array|Zend_Date $week Week to sub
  4111. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4112. * @return Zend_Date Provides fluid interface
  4113. * @throws Zend_Date_Exception
  4114. */
  4115. public function subWeek($week, $locale = null)
  4116. {
  4117. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  4118. }
  4119. /**
  4120. * Compares only the week part, returning the difference
  4121. * Returned is the new date object
  4122. * Returns if equal, earlier or later
  4123. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  4124. *
  4125. * @param string|integer|array|Zend_Date $week Week to compare
  4126. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4127. * @return integer 0 = equal, 1 = later, -1 = earlier
  4128. */
  4129. public function compareWeek($week, $locale = null)
  4130. {
  4131. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  4132. }
  4133. /**
  4134. * Sets a new standard locale for the date object.
  4135. * This locale will be used for all functions
  4136. * Returned is the really set locale.
  4137. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  4138. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  4139. *
  4140. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  4141. * @throws Zend_Date_Exception When the given locale does not exist
  4142. * @return Zend_Date Provides fluent interface
  4143. */
  4144. public function setLocale($locale = null)
  4145. {
  4146. try {
  4147. $this->_locale = Zend_Locale::findLocale($locale);
  4148. } catch (Zend_Locale_Exception $e) {
  4149. require_once 'Zend/Date/Exception.php';
  4150. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  4151. }
  4152. return $this;
  4153. }
  4154. /**
  4155. * Returns the actual set locale
  4156. *
  4157. * @return string
  4158. */
  4159. public function getLocale()
  4160. {
  4161. return $this->_locale;
  4162. }
  4163. /**
  4164. * Checks if the given date is a real date or datepart.
  4165. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  4166. * But the check will only be done for the expected dateparts which are given by format.
  4167. * If no format is given the standard dateformat for the actual locale is used.
  4168. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  4169. *
  4170. * @param string|array|Zend_Date $date Date to parse for correctness
  4171. * @param string $format (Optional) Format for parsing the date string
  4172. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  4173. * @return boolean True when all date parts are correct
  4174. */
  4175. public static function isDate($date, $format = null, $locale = null)
  4176. {
  4177. if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
  4178. !is_array($date)) {
  4179. return false;
  4180. }
  4181. if (($format !== null) && ($format != 'ee') && ($format != 'ss') && ($format != 'GG') && ($format != 'MM') && ($format != 'EE') && ($format != 'TT')
  4182. && (Zend_Locale::isLocale($format, null, false))) {
  4183. $locale = $format;
  4184. $format = null;
  4185. }
  4186. $locale = Zend_Locale::findLocale($locale);
  4187. if ($format === null) {
  4188. $format = Zend_Locale_Format::getDateFormat($locale);
  4189. } else if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  4190. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  4191. }
  4192. $format = self::_getLocalizedToken($format, $locale);
  4193. if (!is_array($date)) {
  4194. try {
  4195. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  4196. 'date_format' => $format, 'format_type' => 'iso',
  4197. 'fix_date' => false));
  4198. } catch (Zend_Locale_Exception $e) {
  4199. // Date can not be parsed
  4200. return false;
  4201. }
  4202. } else {
  4203. $parsed = $date;
  4204. }
  4205. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  4206. (!isset($parsed['year']))) {
  4207. // Year expected but not found
  4208. return false;
  4209. }
  4210. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  4211. // Month expected but not found
  4212. return false;
  4213. }
  4214. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  4215. // Day expected but not found
  4216. return false;
  4217. }
  4218. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  4219. (!isset($parsed['hour']))) {
  4220. // Hour expected but not found
  4221. return false;
  4222. }
  4223. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  4224. // Minute expected but not found
  4225. return false;
  4226. }
  4227. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  4228. // Second expected but not found
  4229. return false;
  4230. }
  4231. // Set not given dateparts
  4232. if (isset($parsed['hour']) === false) {
  4233. $parsed['hour'] = 12;
  4234. }
  4235. if (isset($parsed['minute']) === false) {
  4236. $parsed['minute'] = 0;
  4237. }
  4238. if (isset($parsed['second']) === false) {
  4239. $parsed['second'] = 0;
  4240. }
  4241. if (isset($parsed['month']) === false) {
  4242. $parsed['month'] = 1;
  4243. }
  4244. if (isset($parsed['day']) === false) {
  4245. $parsed['day'] = 1;
  4246. }
  4247. if (isset($parsed['year']) === false) {
  4248. $parsed['year'] = 1970;
  4249. }
  4250. if (self::isYearLeapYear($parsed['year'])) {
  4251. $parsed['year'] = 1972;
  4252. } else {
  4253. $parsed['year'] = 1971;
  4254. }
  4255. $date = new self($parsed, null, $locale);
  4256. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  4257. $parsed['month'], $parsed['day'], $parsed['year']);
  4258. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  4259. // Given year differs from parsed year
  4260. return false;
  4261. }
  4262. if ($parsed['month'] != $date->date('n', $timestamp)) {
  4263. // Given month differs from parsed month
  4264. return false;
  4265. }
  4266. if ($parsed['day'] != $date->date('j', $timestamp)) {
  4267. // Given day differs from parsed day
  4268. return false;
  4269. }
  4270. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  4271. // Given hour differs from parsed hour
  4272. return false;
  4273. }
  4274. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4275. // Given minute differs from parsed minute
  4276. return false;
  4277. }
  4278. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4279. // Given second differs from parsed second
  4280. return false;
  4281. }
  4282. return true;
  4283. }
  4284. /**
  4285. * Returns the ISO Token for all localized constants
  4286. *
  4287. * @param string $token Token to normalize
  4288. * @param string $locale Locale to search
  4289. * @return string
  4290. */
  4291. protected static function _getLocalizedToken($token, $locale)
  4292. {
  4293. switch($token) {
  4294. case self::ISO_8601 :
  4295. return "yyyy-MM-ddThh:mm:ss";
  4296. break;
  4297. case self::RFC_2822 :
  4298. return "EEE, dd MMM yyyy HH:mm:ss";
  4299. break;
  4300. case self::DATES :
  4301. return Zend_Locale_Data::getContent($locale, 'date');
  4302. break;
  4303. case self::DATE_FULL :
  4304. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  4305. break;
  4306. case self::DATE_LONG :
  4307. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4308. break;
  4309. case self::DATE_MEDIUM :
  4310. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4311. break;
  4312. case self::DATE_SHORT :
  4313. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4314. break;
  4315. case self::TIMES :
  4316. return Zend_Locale_Data::getContent($locale, 'time');
  4317. break;
  4318. case self::TIME_FULL :
  4319. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  4320. break;
  4321. case self::TIME_LONG :
  4322. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'long'));
  4323. break;
  4324. case self::TIME_MEDIUM :
  4325. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'medium'));
  4326. break;
  4327. case self::TIME_SHORT :
  4328. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'short'));
  4329. break;
  4330. case self::DATETIME :
  4331. return Zend_Locale_Data::getContent($locale, 'datetime');
  4332. break;
  4333. case self::DATETIME_FULL :
  4334. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  4335. break;
  4336. case self::DATETIME_LONG :
  4337. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  4338. break;
  4339. case self::DATETIME_MEDIUM :
  4340. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  4341. break;
  4342. case self::DATETIME_SHORT :
  4343. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  4344. break;
  4345. case self::ATOM :
  4346. case self::RFC_3339 :
  4347. case self::W3C :
  4348. return "yyyy-MM-DD HH:mm:ss";
  4349. break;
  4350. case self::COOKIE :
  4351. case self::RFC_850 :
  4352. return "EEEE, dd-MM-yyyy HH:mm:ss";
  4353. break;
  4354. case self::RFC_822 :
  4355. case self::RFC_1036 :
  4356. case self::RFC_1123 :
  4357. case self::RSS :
  4358. return "EEE, dd MM yyyy HH:mm:ss";
  4359. break;
  4360. }
  4361. return $token;
  4362. }
  4363. }