PageRenderTime 85ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Date.php

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