PageRenderTime 75ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Date.php

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