PageRenderTime 80ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Date.php

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