PageRenderTime 84ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Date.php

https://bitbucket.org/mayorbrain/precurio-v2
PHP | 4923 lines | 3154 code | 494 blank | 1275 comment | 600 complexity | a6297af7416e75ace2e27b6203ea1fe5 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, MIT
  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 20936 2010-02-05 22:36:44Z 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. $time = str_pad($parsed['hour'], 2, '0', STR_PAD_LEFT) . ":";
  2463. $time .= str_pad($parsed['minute'], 2, '0', STR_PAD_LEFT) . ":";
  2464. $time .= str_pad($parsed['second'], 2, '0', STR_PAD_LEFT);
  2465. }
  2466. $return = $this->_calcdetail($calc, $time, self::TIMES, 'de');
  2467. if ($calc != 'cmp') {
  2468. return $this;
  2469. }
  2470. return $return;
  2471. }
  2472. /**
  2473. * Sets a new time for the date object. Format defines how to parse the time string.
  2474. * Also a complete date can be given, but only the time is used for setting.
  2475. * For example: dd.MMMM.yyTHH:mm' and 'ss sec'-> 10.May.07T25:11 and 44 sec => 1h11min44sec + 1 day
  2476. * Returned is the new date object and the existing date is left as it was before
  2477. *
  2478. * @param string|integer|array|Zend_Date $time Time to set
  2479. * @param string $format OPTIONAL Timeformat for parsing input
  2480. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2481. * @return Zend_Date Provides fluid interface
  2482. * @throws Zend_Date_Exception
  2483. */
  2484. public function setTime($time, $format = null, $locale = null)
  2485. {
  2486. return $this->_time('set', $time, $format, $locale);
  2487. }
  2488. /**
  2489. * Adds a time to the existing date. Format defines how to parse the time string.
  2490. * If only parts are given the other parts are set to 0.
  2491. * If no format is given, the standardformat of this locale is used.
  2492. * For example: HH:mm:ss -> 10 -> +10 hours
  2493. *
  2494. * @param string|integer|array|Zend_Date $time Time to add
  2495. * @param string $format OPTIONAL Timeformat for parsing input
  2496. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2497. * @return Zend_Date Provides fluid interface
  2498. * @throws Zend_Date_Exception
  2499. */
  2500. public function addTime($time, $format = null, $locale = null)
  2501. {
  2502. return $this->_time('add', $time, $format, $locale);
  2503. }
  2504. /**
  2505. * Subtracts a time from the existing date. Format defines how to parse the time string.
  2506. * If only parts are given the other parts are set to 0.
  2507. * If no format is given, the standardformat of this locale is used.
  2508. * For example: HH:mm:ss -> 10 -> -10 hours
  2509. *
  2510. * @param string|integer|array|Zend_Date $time Time to sub
  2511. * @param string $format OPTIONAL Timeformat for parsing input
  2512. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2513. * @return Zend_Date Provides fluid inteface
  2514. * @throws Zend_Date_Exception
  2515. */
  2516. public function subTime($time, $format = null, $locale = null)
  2517. {
  2518. return $this->_time('sub', $time, $format, $locale);
  2519. }
  2520. /**
  2521. * Compares the time from the existing date. Format defines how to parse the time string.
  2522. * If only parts are given the other parts are set to default.
  2523. * If no format us given, the standardformat of this locale is used.
  2524. * For example: HH:mm:ss -> 10 -> 10 hours
  2525. *
  2526. * @param string|integer|array|Zend_Date $time Time to compare
  2527. * @param string $format OPTIONAL Timeformat for parsing input
  2528. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2529. * @return integer 0 = equal, 1 = later, -1 = earlier
  2530. * @throws Zend_Date_Exception
  2531. */
  2532. public function compareTime($time, $format = null, $locale = null)
  2533. {
  2534. return $this->_time('cmp', $time, $format, $locale);
  2535. }
  2536. /**
  2537. * Returns a clone of $this, with the time part set to 00:00:00.
  2538. *
  2539. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2540. * @return Zend_Date
  2541. */
  2542. public function getDate($locale = null)
  2543. {
  2544. $orig = self::$_options['format_type'];
  2545. if (self::$_options['format_type'] == 'php') {
  2546. self::$_options['format_type'] = 'iso';
  2547. }
  2548. $date = $this->copyPart(self::DATE_MEDIUM, $locale);
  2549. $date->addTimestamp($this->getGmtOffset());
  2550. self::$_options['format_type'] = $orig;
  2551. return $date;
  2552. }
  2553. /**
  2554. * Returns the calculated date
  2555. *
  2556. * @param string $calc Calculation to make
  2557. * @param string|integer|array|Zend_Date $date Date to calculate with, if null the actual date is taken
  2558. * @param string $format Date format for parsing
  2559. * @param string|Zend_Locale $locale Locale for parsing input
  2560. * @return integer|Zend_Date new date
  2561. * @throws Zend_Date_Exception
  2562. */
  2563. private function _date($calc, $date, $format, $locale)
  2564. {
  2565. if ($date === null) {
  2566. require_once 'Zend/Date/Exception.php';
  2567. throw new Zend_Date_Exception('parameter $date must be set, null is not allowed');
  2568. }
  2569. if ($date instanceof Zend_Date) {
  2570. // extract date from object
  2571. $date = $date->toString('d.M.y', 'iso');
  2572. } else {
  2573. if (is_array($date)) {
  2574. if ((isset($date['year']) === true) or (isset($date['month']) === true) or
  2575. (isset($date['day']) === true)) {
  2576. $parsed = $date;
  2577. } else {
  2578. require_once 'Zend/Date/Exception.php';
  2579. throw new Zend_Date_Exception("no day,month or year given in array");
  2580. }
  2581. } else {
  2582. if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  2583. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  2584. }
  2585. try {
  2586. if ($locale === null) {
  2587. $locale = $this->getLocale();
  2588. }
  2589. $parsed = Zend_Locale_Format::getDate($date, array('date_format' => $format, 'locale' => $locale, 'format_type' => 'iso'));
  2590. if ((strpos(strtoupper($format), 'YY') !== false) and (strpos(strtoupper($format), 'YYYY') === false)) {
  2591. $parsed['year'] = self::getFullYear($parsed['year']);
  2592. }
  2593. } catch (Zend_Locale_Exception $e) {
  2594. require_once 'Zend/Date/Exception.php';
  2595. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  2596. }
  2597. }
  2598. $date = $parsed['day'] . "." . $parsed['month'] . "." . $parsed['year'];
  2599. }
  2600. $return = $this->_calcdetail($calc, $date, self::DATE_MEDIUM, 'de');
  2601. if ($calc != 'cmp') {
  2602. return $this;
  2603. }
  2604. return $return;
  2605. }
  2606. /**
  2607. * Sets a new date for the date object. Format defines how to parse the date string.
  2608. * Also a complete date with time can be given, but only the date is used for setting.
  2609. * For example: MMMM.yy HH:mm-> May.07 22:11 => 01.May.07 00:00
  2610. * Returned is the new date object and the existing time is left as it was before
  2611. *
  2612. * @param string|integer|array|Zend_Date $date Date to set
  2613. * @param string $format OPTIONAL Date format for parsing
  2614. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2615. * @return Zend_Date Provides fluid interface
  2616. * @throws Zend_Date_Exception
  2617. */
  2618. public function setDate($date, $format = null, $locale = null)
  2619. {
  2620. return $this->_date('set', $date, $format, $locale);
  2621. }
  2622. /**
  2623. * Adds a date to the existing date object. Format defines how to parse the date string.
  2624. * If only parts are given the other parts are set to 0.
  2625. * If no format is given, the standardformat of this locale is used.
  2626. * For example: MM.dd.YYYY -> 10 -> +10 months
  2627. *
  2628. * @param string|integer|array|Zend_Date $date Date to add
  2629. * @param string $format OPTIONAL Date format for parsing input
  2630. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2631. * @return Zend_Date Provides fluid interface
  2632. * @throws Zend_Date_Exception
  2633. */
  2634. public function addDate($date, $format = null, $locale = null)
  2635. {
  2636. return $this->_date('add', $date, $format, $locale);
  2637. }
  2638. /**
  2639. * Subtracts a date from the existing date object. Format defines how to parse the date string.
  2640. * If only parts are given the other parts are set to 0.
  2641. * If no format is given, the standardformat of this locale is used.
  2642. * For example: MM.dd.YYYY -> 10 -> -10 months
  2643. * Be aware: Subtracting 2 months is not equal to Adding -2 months !!!
  2644. *
  2645. * @param string|integer|array|Zend_Date $date Date to sub
  2646. * @param string $format OPTIONAL Date format for parsing input
  2647. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2648. * @return Zend_Date Provides fluid interface
  2649. * @throws Zend_Date_Exception
  2650. */
  2651. public function subDate($date, $format = null, $locale = null)
  2652. {
  2653. return $this->_date('sub', $date, $format, $locale);
  2654. }
  2655. /**
  2656. * Compares the date from the existing date object, ignoring the time.
  2657. * 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: 10.01.2000 => 10.02.1999 -> false
  2661. *
  2662. * @param string|integer|array|Zend_Date $date Date to compare
  2663. * @param string $format OPTIONAL Date format for parsing input
  2664. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2665. * @return integer 0 = equal, 1 = later, -1 = earlier
  2666. * @throws Zend_Date_Exception
  2667. */
  2668. public function compareDate($date, $format = null, $locale = null)
  2669. {
  2670. return $this->_date('cmp', $date, $format, $locale);
  2671. }
  2672. /**
  2673. * Returns the full ISO 8601 date from the date object.
  2674. * Always the complete ISO 8601 specifiction is used. If an other ISO date is needed
  2675. * (ISO 8601 defines several formats) use toString() instead.
  2676. * This function does not return the ISO date as object. Use copy() instead.
  2677. *
  2678. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2679. * @return string
  2680. */
  2681. public function getIso($locale = null)
  2682. {
  2683. return $this->toString(self::ISO_8601, 'iso', $locale);
  2684. }
  2685. /**
  2686. * Sets a new date for the date object. Not given parts are set to default.
  2687. * Only supported ISO 8601 formats are accepted.
  2688. * For example: 050901 -> 01.Sept.2005 00:00:00, 20050201T10:00:30 -> 01.Feb.2005 10h00m30s
  2689. * Returned is the new date object
  2690. *
  2691. * @param string|integer|Zend_Date $date ISO Date to set
  2692. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2693. * @return Zend_Date Provides fluid interface
  2694. * @throws Zend_Date_Exception
  2695. */
  2696. public function setIso($date, $locale = null)
  2697. {
  2698. return $this->_calcvalue('set', $date, 'iso', self::ISO_8601, $locale);
  2699. }
  2700. /**
  2701. * Adds a ISO date to the date object. Not given parts are set to default.
  2702. * Only supported ISO 8601 formats are accepted.
  2703. * For example: 050901 -> + 01.Sept.2005 00:00:00, 10:00:00 -> +10h
  2704. * Returned is the new date object
  2705. *
  2706. * @param string|integer|Zend_Date $date ISO Date to add
  2707. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2708. * @return Zend_Date Provides fluid interface
  2709. * @throws Zend_Date_Exception
  2710. */
  2711. public function addIso($date, $locale = null)
  2712. {
  2713. return $this->_calcvalue('add', $date, 'iso', self::ISO_8601, $locale);
  2714. }
  2715. /**
  2716. * Subtracts a ISO date from the date object. Not given parts are set to default.
  2717. * Only supported ISO 8601 formats are accepted.
  2718. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2719. * Returned is the new date object
  2720. *
  2721. * @param string|integer|Zend_Date $date ISO Date to sub
  2722. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2723. * @return Zend_Date Provides fluid interface
  2724. * @throws Zend_Date_Exception
  2725. */
  2726. public function subIso($date, $locale = null)
  2727. {
  2728. return $this->_calcvalue('sub', $date, 'iso', self::ISO_8601, $locale);
  2729. }
  2730. /**
  2731. * Compares a ISO date with the date object. Not given parts are set to default.
  2732. * Only supported ISO 8601 formats are accepted.
  2733. * For example: 050901 -> - 01.Sept.2005 00:00:00, 10:00:00 -> -10h
  2734. * Returns if equal, earlier or later
  2735. *
  2736. * @param string|integer|Zend_Date $date ISO Date to sub
  2737. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2738. * @return integer 0 = equal, 1 = later, -1 = earlier
  2739. * @throws Zend_Date_Exception
  2740. */
  2741. public function compareIso($date, $locale = null)
  2742. {
  2743. return $this->_calcvalue('cmp', $date, 'iso', self::ISO_8601, $locale);
  2744. }
  2745. /**
  2746. * Returns a RFC 822 compilant datestring from the date object.
  2747. * This function does not return the RFC date as object. Use copy() instead.
  2748. *
  2749. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2750. * @return string
  2751. */
  2752. public function getArpa($locale = null)
  2753. {
  2754. if (self::$_options['format_type'] == 'php') {
  2755. $format = 'D\, d M y H\:i\:s O';
  2756. } else {
  2757. $format = self::RFC_822;
  2758. }
  2759. return $this->toString($format, 'iso', $locale);
  2760. }
  2761. /**
  2762. * Sets a RFC 822 date as new date for the date object.
  2763. * Only RFC 822 compilant date strings are accepted.
  2764. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2765. * Returned is the new date object
  2766. *
  2767. * @param string|integer|Zend_Date $date RFC 822 to set
  2768. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2769. * @return Zend_Date Provides fluid interface
  2770. * @throws Zend_Date_Exception
  2771. */
  2772. public function setArpa($date, $locale = null)
  2773. {
  2774. return $this->_calcvalue('set', $date, 'arpa', self::RFC_822, $locale);
  2775. }
  2776. /**
  2777. * Adds a RFC 822 date to the date object.
  2778. * ARPA messages are used in emails or HTTP Headers.
  2779. * Only RFC 822 compilant date strings are accepted.
  2780. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2781. * Returned is the new date object
  2782. *
  2783. * @param string|integer|Zend_Date $date RFC 822 Date to add
  2784. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2785. * @return Zend_Date Provides fluid interface
  2786. * @throws Zend_Date_Exception
  2787. */
  2788. public function addArpa($date, $locale = null)
  2789. {
  2790. return $this->_calcvalue('add', $date, 'arpa', self::RFC_822, $locale);
  2791. }
  2792. /**
  2793. * Subtracts a RFC 822 date from the date object.
  2794. * ARPA messages are used in emails or HTTP Headers.
  2795. * Only RFC 822 compilant date strings are accepted.
  2796. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2797. * Returned is the new date object
  2798. *
  2799. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2800. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2801. * @return Zend_Date Provides fluid interface
  2802. * @throws Zend_Date_Exception
  2803. */
  2804. public function subArpa($date, $locale = null)
  2805. {
  2806. return $this->_calcvalue('sub', $date, 'arpa', self::RFC_822, $locale);
  2807. }
  2808. /**
  2809. * Compares a RFC 822 compilant date with the date object.
  2810. * ARPA messages are used in emails or HTTP Headers.
  2811. * Only RFC 822 compilant date strings are accepted.
  2812. * For example: Sat, 14 Feb 09 00:31:30 +0100
  2813. * Returns if equal, earlier or later
  2814. *
  2815. * @param string|integer|Zend_Date $date RFC 822 Date to sub
  2816. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  2817. * @return integer 0 = equal, 1 = later, -1 = earlier
  2818. * @throws Zend_Date_Exception
  2819. */
  2820. public function compareArpa($date, $locale = null)
  2821. {
  2822. return $this->_calcvalue('cmp', $date, 'arpa', self::RFC_822, $locale);
  2823. }
  2824. /**
  2825. * Check if location is supported
  2826. *
  2827. * @param $location array - locations array
  2828. * @return $horizon float
  2829. */
  2830. private function _checkLocation($location)
  2831. {
  2832. if (!isset($location['longitude']) or !isset($location['latitude'])) {
  2833. require_once 'Zend/Date/Exception.php';
  2834. throw new Zend_Date_Exception('Location must include \'longitude\' and \'latitude\'', 0, null, $location);
  2835. }
  2836. if (($location['longitude'] > 180) or ($location['longitude'] < -180)) {
  2837. require_once 'Zend/Date/Exception.php';
  2838. throw new Zend_Date_Exception('Longitude must be between -180 and 180', 0, null, $location);
  2839. }
  2840. if (($location['latitude'] > 90) or ($location['latitude'] < -90)) {
  2841. require_once 'Zend/Date/Exception.php';
  2842. throw new Zend_Date_Exception('Latitude must be between -90 and 90', 0, null, $location);
  2843. }
  2844. if (!isset($location['horizon'])){
  2845. $location['horizon'] = 'effective';
  2846. }
  2847. switch ($location['horizon']) {
  2848. case 'civil' :
  2849. return -0.104528;
  2850. break;
  2851. case 'nautic' :
  2852. return -0.207912;
  2853. break;
  2854. case 'astronomic' :
  2855. return -0.309017;
  2856. break;
  2857. default :
  2858. return -0.0145439;
  2859. break;
  2860. }
  2861. }
  2862. /**
  2863. * Returns the time of sunrise for this date and a given location as new date object
  2864. * For a list of cities and correct locations use the class Zend_Date_Cities
  2865. *
  2866. * @param $location array - location of sunrise
  2867. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2868. * ['longitude'] -> longitude of location
  2869. * ['latitude'] -> latitude of location
  2870. * @return Zend_Date
  2871. * @throws Zend_Date_Exception
  2872. */
  2873. public function getSunrise($location)
  2874. {
  2875. $horizon = $this->_checkLocation($location);
  2876. $result = clone $this;
  2877. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2878. return $result;
  2879. }
  2880. /**
  2881. * Returns the time of sunset 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 sunset
  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 getSunset($location)
  2892. {
  2893. $horizon = $this->_checkLocation($location);
  2894. $result = clone $this;
  2895. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2896. return $result;
  2897. }
  2898. /**
  2899. * Returns an array with the sunset and sunrise dates for all horizon types
  2900. * For a list of cities and correct locations use the class Zend_Date_Cities
  2901. *
  2902. * @param $location array - location of suninfo
  2903. * ['horizon'] -> civil, nautic, astronomical, effective (default)
  2904. * ['longitude'] -> longitude of location
  2905. * ['latitude'] -> latitude of location
  2906. * @return array - [sunset|sunrise][effective|civil|nautic|astronomic]
  2907. * @throws Zend_Date_Exception
  2908. */
  2909. public function getSunInfo($location)
  2910. {
  2911. $suninfo = array();
  2912. for ($i = 0; $i < 4; ++$i) {
  2913. switch ($i) {
  2914. case 0 :
  2915. $location['horizon'] = 'effective';
  2916. break;
  2917. case 1 :
  2918. $location['horizon'] = 'civil';
  2919. break;
  2920. case 2 :
  2921. $location['horizon'] = 'nautic';
  2922. break;
  2923. case 3 :
  2924. $location['horizon'] = 'astronomic';
  2925. break;
  2926. }
  2927. $horizon = $this->_checkLocation($location);
  2928. $result = clone $this;
  2929. $result->set($this->calcSun($location, $horizon, true), self::TIMESTAMP);
  2930. $suninfo['sunrise'][$location['horizon']] = $result;
  2931. $result = clone $this;
  2932. $result->set($this->calcSun($location, $horizon, false), self::TIMESTAMP);
  2933. $suninfo['sunset'][$location['horizon']] = $result;
  2934. }
  2935. return $suninfo;
  2936. }
  2937. /**
  2938. * Check a given year for leap year.
  2939. *
  2940. * @param integer|array|Zend_Date $year Year to check
  2941. * @return boolean
  2942. */
  2943. public static function checkLeapYear($year)
  2944. {
  2945. if ($year instanceof Zend_Date) {
  2946. $year = (int) $year->toString(self::YEAR, 'iso');
  2947. }
  2948. if (is_array($year)) {
  2949. if (isset($year['year']) === true) {
  2950. $year = $year['year'];
  2951. } else {
  2952. require_once 'Zend/Date/Exception.php';
  2953. throw new Zend_Date_Exception("no year given in array");
  2954. }
  2955. }
  2956. if (!is_numeric($year)) {
  2957. require_once 'Zend/Date/Exception.php';
  2958. throw new Zend_Date_Exception("year ($year) has to be integer for checkLeapYear()", 0, null, $year);
  2959. }
  2960. return (bool) parent::isYearLeapYear($year);
  2961. }
  2962. /**
  2963. * Returns true, if the year is a leap year.
  2964. *
  2965. * @return boolean
  2966. */
  2967. public function isLeapYear()
  2968. {
  2969. return self::checkLeapYear($this);
  2970. }
  2971. /**
  2972. * Returns if the set date is todays date
  2973. *
  2974. * @return boolean
  2975. */
  2976. public function isToday()
  2977. {
  2978. $today = $this->date('Ymd', $this->_getTime());
  2979. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2980. return ($today == $day);
  2981. }
  2982. /**
  2983. * Returns if the set date is yesterdays date
  2984. *
  2985. * @return boolean
  2986. */
  2987. public function isYesterday()
  2988. {
  2989. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  2990. // adjusts for leap days and DST changes that are timezone specific
  2991. $yesterday = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day -1, $year));
  2992. $day = $this->date('Ymd', $this->getUnixTimestamp());
  2993. return $day == $yesterday;
  2994. }
  2995. /**
  2996. * Returns if the set date is tomorrows date
  2997. *
  2998. * @return boolean
  2999. */
  3000. public function isTomorrow()
  3001. {
  3002. list($year, $month, $day) = explode('-', $this->date('Y-m-d', $this->_getTime()));
  3003. // adjusts for leap days and DST changes that are timezone specific
  3004. $tomorrow = $this->date('Ymd', $this->mktime(0, 0, 0, $month, $day +1, $year));
  3005. $day = $this->date('Ymd', $this->getUnixTimestamp());
  3006. return $day == $tomorrow;
  3007. }
  3008. /**
  3009. * Returns the actual date as new date object
  3010. *
  3011. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3012. * @return Zend_Date
  3013. */
  3014. public static function now($locale = null)
  3015. {
  3016. return new Zend_Date(time(), self::TIMESTAMP, $locale);
  3017. }
  3018. /**
  3019. * Calculate date details
  3020. *
  3021. * @param string $calc Calculation to make
  3022. * @param string|integer|array|Zend_Date $date Date or Part to calculate
  3023. * @param string $part Datepart for Calculation
  3024. * @param string|Zend_Locale $locale Locale for parsing input
  3025. * @return integer|string new date
  3026. * @throws Zend_Date_Exception
  3027. */
  3028. private function _calcdetail($calc, $date, $type, $locale)
  3029. {
  3030. $old = false;
  3031. if (self::$_options['format_type'] == 'php') {
  3032. self::$_options['format_type'] = 'iso';
  3033. $old = true;
  3034. }
  3035. switch($calc) {
  3036. case 'set' :
  3037. $return = $this->set($date, $type, $locale);
  3038. break;
  3039. case 'add' :
  3040. $return = $this->add($date, $type, $locale);
  3041. break;
  3042. case 'sub' :
  3043. $return = $this->sub($date, $type, $locale);
  3044. break;
  3045. default :
  3046. $return = $this->compare($date, $type, $locale);
  3047. break;
  3048. }
  3049. if ($old) {
  3050. self::$_options['format_type'] = 'php';
  3051. }
  3052. return $return;
  3053. }
  3054. /**
  3055. * Internal calculation, returns the requested date type
  3056. *
  3057. * @param string $calc Calculation to make
  3058. * @param string|integer|Zend_Date $value Datevalue to calculate with, if null the actual value is taken
  3059. * @param string|Zend_Locale $locale Locale for parsing input
  3060. * @return integer|Zend_Date new date
  3061. * @throws Zend_Date_Exception
  3062. */
  3063. private function _calcvalue($calc, $value, $type, $parameter, $locale)
  3064. {
  3065. if ($value === null) {
  3066. require_once 'Zend/Date/Exception.php';
  3067. throw new Zend_Date_Exception("parameter $type must be set, null is not allowed");
  3068. }
  3069. if ($locale === null) {
  3070. $locale = $this->getLocale();
  3071. }
  3072. if ($value instanceof Zend_Date) {
  3073. // extract value from object
  3074. $value = $value->toString($parameter, 'iso', $locale);
  3075. } else if (!is_array($value) && !is_numeric($value) && ($type != 'iso') && ($type != 'arpa')) {
  3076. require_once 'Zend/Date/Exception.php';
  3077. throw new Zend_Date_Exception("invalid $type ($value) operand", 0, null, $value);
  3078. }
  3079. $return = $this->_calcdetail($calc, $value, $parameter, $locale);
  3080. if ($calc != 'cmp') {
  3081. return $this;
  3082. }
  3083. return $return;
  3084. }
  3085. /**
  3086. * Returns only the year from the date object as new object.
  3087. * For example: 10.May.2000 10:30:00 -> 01.Jan.2000 00:00:00
  3088. *
  3089. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3090. * @return Zend_Date
  3091. */
  3092. public function getYear($locale = null)
  3093. {
  3094. if (self::$_options['format_type'] == 'php') {
  3095. $format = 'Y';
  3096. } else {
  3097. $format = self::YEAR;
  3098. }
  3099. return $this->copyPart($format, $locale);
  3100. }
  3101. /**
  3102. * Sets a new year
  3103. * If the year is between 0 and 69, 2000 will be set (2000-2069)
  3104. * If the year if between 70 and 99, 1999 will be set (1970-1999)
  3105. * 3 or 4 digit years are set as expected. If you need to set year 0-99
  3106. * use set() instead.
  3107. * Returned is the new date object
  3108. *
  3109. * @param string|integer|array|Zend_Date $date Year to set
  3110. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3111. * @return Zend_Date Provides fluid interface
  3112. * @throws Zend_Date_Exception
  3113. */
  3114. public function setYear($year, $locale = null)
  3115. {
  3116. return $this->_calcvalue('set', $year, 'year', self::YEAR, $locale);
  3117. }
  3118. /**
  3119. * Adds the year to the existing date object
  3120. * If the year is between 0 and 69, 2000 will be added (2000-2069)
  3121. * If the year if between 70 and 99, 1999 will be added (1970-1999)
  3122. * 3 or 4 digit years are added as expected. If you need to add years from 0-99
  3123. * use add() instead.
  3124. * Returned is the new date object
  3125. *
  3126. * @param string|integer|array|Zend_Date $date Year to add
  3127. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3128. * @return Zend_Date Provides fluid interface
  3129. * @throws Zend_Date_Exception
  3130. */
  3131. public function addYear($year, $locale = null)
  3132. {
  3133. return $this->_calcvalue('add', $year, 'year', self::YEAR, $locale);
  3134. }
  3135. /**
  3136. * Subs the year from the existing date object
  3137. * If the year is between 0 and 69, 2000 will be subtracted (2000-2069)
  3138. * If the year if between 70 and 99, 1999 will be subtracted (1970-1999)
  3139. * 3 or 4 digit years are subtracted as expected. If you need to subtract years from 0-99
  3140. * use sub() instead.
  3141. * Returned is the new date object
  3142. *
  3143. * @param string|integer|array|Zend_Date $date Year to sub
  3144. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3145. * @return Zend_Date Provides fluid interface
  3146. * @throws Zend_Date_Exception
  3147. */
  3148. public function subYear($year, $locale = null)
  3149. {
  3150. return $this->_calcvalue('sub', $year, 'year', self::YEAR, $locale);
  3151. }
  3152. /**
  3153. * Compares the year with the existing date object, ignoring other date parts.
  3154. * For example: 10.03.2000 -> 15.02.2000 -> true
  3155. * Returns if equal, earlier or later
  3156. *
  3157. * @param string|integer|array|Zend_Date $year Year to compare
  3158. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3159. * @return integer 0 = equal, 1 = later, -1 = earlier
  3160. * @throws Zend_Date_Exception
  3161. */
  3162. public function compareYear($year, $locale = null)
  3163. {
  3164. return $this->_calcvalue('cmp', $year, 'year', self::YEAR, $locale);
  3165. }
  3166. /**
  3167. * Returns only the month from the date object as new object.
  3168. * For example: 10.May.2000 10:30:00 -> 01.May.1970 00:00:00
  3169. *
  3170. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3171. * @return Zend_Date
  3172. */
  3173. public function getMonth($locale = null)
  3174. {
  3175. if (self::$_options['format_type'] == 'php') {
  3176. $format = 'm';
  3177. } else {
  3178. $format = self::MONTH;
  3179. }
  3180. return $this->copyPart($format, $locale);
  3181. }
  3182. /**
  3183. * Returns the calculated month
  3184. *
  3185. * @param string $calc Calculation to make
  3186. * @param string|integer|array|Zend_Date $month Month to calculate with, if null the actual month is taken
  3187. * @param string|Zend_Locale $locale Locale for parsing input
  3188. * @return integer|Zend_Date new time
  3189. * @throws Zend_Date_Exception
  3190. */
  3191. private function _month($calc, $month, $locale)
  3192. {
  3193. if ($month === null) {
  3194. require_once 'Zend/Date/Exception.php';
  3195. throw new Zend_Date_Exception('parameter $month must be set, null is not allowed');
  3196. }
  3197. if ($locale === null) {
  3198. $locale = $this->getLocale();
  3199. }
  3200. if ($month instanceof Zend_Date) {
  3201. // extract month from object
  3202. $found = $month->toString(self::MONTH_SHORT, 'iso', $locale);
  3203. } else {
  3204. if (is_numeric($month)) {
  3205. $found = $month;
  3206. } else if (is_array($month)) {
  3207. if (isset($month['month']) === true) {
  3208. $month = $month['month'];
  3209. } else {
  3210. require_once 'Zend/Date/Exception.php';
  3211. throw new Zend_Date_Exception("no month given in array");
  3212. }
  3213. } else {
  3214. $monthlist = Zend_Locale_Data::getList($locale, 'month');
  3215. $monthlist2 = Zend_Locale_Data::getList($locale, 'month', array('gregorian', 'format', 'abbreviated'));
  3216. $monthlist = array_merge($monthlist, $monthlist2);
  3217. $found = 0;
  3218. $cnt = 0;
  3219. foreach ($monthlist as $key => $value) {
  3220. if (strtoupper($value) == strtoupper($month)) {
  3221. $found = ($key % 12) + 1;
  3222. break;
  3223. }
  3224. ++$cnt;
  3225. }
  3226. if ($found == 0) {
  3227. foreach ($monthlist2 as $key => $value) {
  3228. if (strtoupper(iconv_substr($value, 0, 1, 'UTF-8')) == strtoupper($month)) {
  3229. $found = $key + 1;
  3230. break;
  3231. }
  3232. ++$cnt;
  3233. }
  3234. }
  3235. if ($found == 0) {
  3236. require_once 'Zend/Date/Exception.php';
  3237. throw new Zend_Date_Exception("unknown month name ($month)", 0, null, $month);
  3238. }
  3239. }
  3240. }
  3241. $return = $this->_calcdetail($calc, $found, self::MONTH_SHORT, $locale);
  3242. if ($calc != 'cmp') {
  3243. return $this;
  3244. }
  3245. return $return;
  3246. }
  3247. /**
  3248. * Sets a new month
  3249. * The month can be a number or a string. Setting months lower then 0 and greater then 12
  3250. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3251. * If a localized monthname is given it will be parsed with the default locale or the optional
  3252. * set locale.
  3253. * Returned is the new date object
  3254. *
  3255. * @param string|integer|array|Zend_Date $month Month to set
  3256. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3257. * @return Zend_Date Provides fluid interface
  3258. * @throws Zend_Date_Exception
  3259. */
  3260. public function setMonth($month, $locale = null)
  3261. {
  3262. return $this->_month('set', $month, $locale);
  3263. }
  3264. /**
  3265. * Adds months to the existing date object.
  3266. * The month can be a number or a string. Adding months lower then 0 and greater then 12
  3267. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3268. * If a localized monthname is given it will be parsed with the default locale or the optional
  3269. * set locale.
  3270. * Returned is the new date object
  3271. *
  3272. * @param string|integer|array|Zend_Date $month Month to add
  3273. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3274. * @return Zend_Date Provides fluid interface
  3275. * @throws Zend_Date_Exception
  3276. */
  3277. public function addMonth($month, $locale = null)
  3278. {
  3279. return $this->_month('add', $month, $locale);
  3280. }
  3281. /**
  3282. * Subtracts months from the existing date object.
  3283. * The month can be a number or a string. Subtracting months lower then 0 and greater then 12
  3284. * will result in adding or subtracting the relevant year. (12 months equal one year)
  3285. * If a localized monthname is given it will be parsed with the default locale or the optional
  3286. * set locale.
  3287. * Returned is the new date object
  3288. *
  3289. * @param string|integer|array|Zend_Date $month Month to sub
  3290. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3291. * @return Zend_Date Provides fluid interface
  3292. * @throws Zend_Date_Exception
  3293. */
  3294. public function subMonth($month, $locale = null)
  3295. {
  3296. return $this->_month('sub', $month, $locale);
  3297. }
  3298. /**
  3299. * Compares the month with the existing date object, ignoring other date parts.
  3300. * For example: 10.03.2000 -> 15.03.1950 -> true
  3301. * Returns if equal, earlier or later
  3302. *
  3303. * @param string|integer|array|Zend_Date $month Month to compare
  3304. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3305. * @return integer 0 = equal, 1 = later, -1 = earlier
  3306. * @throws Zend_Date_Exception
  3307. */
  3308. public function compareMonth($month, $locale = null)
  3309. {
  3310. return $this->_month('cmp', $month, $locale);
  3311. }
  3312. /**
  3313. * Returns the day as new date object
  3314. * Example: 20.May.1986 -> 20.Jan.1970 00:00:00
  3315. *
  3316. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3317. * @return Zend_Date
  3318. */
  3319. public function getDay($locale = null)
  3320. {
  3321. return $this->copyPart(self::DAY_SHORT, $locale);
  3322. }
  3323. /**
  3324. * Returns the calculated day
  3325. *
  3326. * @param $calc string Type of calculation to make
  3327. * @param $day string|integer|Zend_Date Day to calculate, when null the actual day is calculated
  3328. * @param $locale string|Zend_Locale Locale for parsing input
  3329. * @return Zend_Date|integer
  3330. */
  3331. private function _day($calc, $day, $locale)
  3332. {
  3333. if ($day === null) {
  3334. require_once 'Zend/Date/Exception.php';
  3335. throw new Zend_Date_Exception('parameter $day must be set, null is not allowed');
  3336. }
  3337. if ($locale === null) {
  3338. $locale = $this->getLocale();
  3339. }
  3340. if ($day instanceof Zend_Date) {
  3341. $day = $day->toString(self::DAY_SHORT, 'iso', $locale);
  3342. }
  3343. if (is_numeric($day)) {
  3344. $type = self::DAY_SHORT;
  3345. } else if (is_array($day)) {
  3346. if (isset($day['day']) === true) {
  3347. $day = $day['day'];
  3348. $type = self::WEEKDAY;
  3349. } else {
  3350. require_once 'Zend/Date/Exception.php';
  3351. throw new Zend_Date_Exception("no day given in array");
  3352. }
  3353. } else {
  3354. switch (iconv_strlen($day, 'UTF-8')) {
  3355. case 1 :
  3356. $type = self::WEEKDAY_NARROW;
  3357. break;
  3358. case 2:
  3359. $type = self::WEEKDAY_NAME;
  3360. break;
  3361. case 3:
  3362. $type = self::WEEKDAY_SHORT;
  3363. break;
  3364. default:
  3365. $type = self::WEEKDAY;
  3366. break;
  3367. }
  3368. }
  3369. $return = $this->_calcdetail($calc, $day, $type, $locale);
  3370. if ($calc != 'cmp') {
  3371. return $this;
  3372. }
  3373. return $return;
  3374. }
  3375. /**
  3376. * Sets a new day
  3377. * The day can be a number or a string. Setting days lower then 0 or greater than the number of this months days
  3378. * will result in adding or subtracting the relevant month.
  3379. * If a localized dayname is given it will be parsed with the default locale or the optional
  3380. * set locale.
  3381. * Returned is the new date object
  3382. * Example: setDay('Montag', 'de_AT'); will set the monday of this week as day.
  3383. *
  3384. * @param string|integer|array|Zend_Date $month Day to set
  3385. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3386. * @return Zend_Date Provides fluid interface
  3387. * @throws Zend_Date_Exception
  3388. */
  3389. public function setDay($day, $locale = null)
  3390. {
  3391. return $this->_day('set', $day, $locale);
  3392. }
  3393. /**
  3394. * Adds days to the existing date object.
  3395. * The day can be a number or a string. Adding 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. *
  3400. * @param string|integer|array|Zend_Date $month Day to add
  3401. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3402. * @return Zend_Date Provides fluid interface
  3403. * @throws Zend_Date_Exception
  3404. */
  3405. public function addDay($day, $locale = null)
  3406. {
  3407. return $this->_day('add', $day, $locale);
  3408. }
  3409. /**
  3410. * Subtracts days from the existing date object.
  3411. * The day can be a number or a string. Subtracting days lower then 0 or greater than the number of this months days
  3412. * will result in adding or subtracting the relevant month.
  3413. * If a localized dayname is given it will be parsed with the default locale or the optional
  3414. * set locale.
  3415. *
  3416. * @param string|integer|array|Zend_Date $month Day to sub
  3417. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3418. * @return Zend_Date Provides fluid interface
  3419. * @throws Zend_Date_Exception
  3420. */
  3421. public function subDay($day, $locale = null)
  3422. {
  3423. return $this->_day('sub', $day, $locale);
  3424. }
  3425. /**
  3426. * Compares the day with the existing date object, ignoring other date parts.
  3427. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3428. * Returns if equal, earlier or later
  3429. *
  3430. * @param string|integer|array|Zend_Date $day Day to compare
  3431. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3432. * @return integer 0 = equal, 1 = later, -1 = earlier
  3433. * @throws Zend_Date_Exception
  3434. */
  3435. public function compareDay($day, $locale = null)
  3436. {
  3437. return $this->_day('cmp', $day, $locale);
  3438. }
  3439. /**
  3440. * Returns the weekday as new date object
  3441. * Weekday is always from 1-7
  3442. * Example: 09-Jan-2007 -> 2 = Tuesday -> 02-Jan-1970 (when 02.01.1970 is also Tuesday)
  3443. *
  3444. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3445. * @return Zend_Date
  3446. */
  3447. public function getWeekday($locale = null)
  3448. {
  3449. if (self::$_options['format_type'] == 'php') {
  3450. $format = 'l';
  3451. } else {
  3452. $format = self::WEEKDAY;
  3453. }
  3454. return $this->copyPart($format, $locale);
  3455. }
  3456. /**
  3457. * Returns the calculated weekday
  3458. *
  3459. * @param $calc string Type of calculation to make
  3460. * @param $weekday string|integer|array|Zend_Date Weekday to calculate, when null the actual weekday is calculated
  3461. * @param $locale string|Zend_Locale Locale for parsing input
  3462. * @return Zend_Date|integer
  3463. * @throws Zend_Date_Exception
  3464. */
  3465. private function _weekday($calc, $weekday, $locale)
  3466. {
  3467. if ($weekday === null) {
  3468. require_once 'Zend/Date/Exception.php';
  3469. throw new Zend_Date_Exception('parameter $weekday must be set, null is not allowed');
  3470. }
  3471. if ($locale === null) {
  3472. $locale = $this->getLocale();
  3473. }
  3474. if ($weekday instanceof Zend_Date) {
  3475. $weekday = $weekday->toString(self::WEEKDAY_8601, 'iso', $locale);
  3476. }
  3477. if (is_numeric($weekday)) {
  3478. $type = self::WEEKDAY_8601;
  3479. } else if (is_array($weekday)) {
  3480. if (isset($weekday['weekday']) === true) {
  3481. $weekday = $weekday['weekday'];
  3482. $type = self::WEEKDAY;
  3483. } else {
  3484. require_once 'Zend/Date/Exception.php';
  3485. throw new Zend_Date_Exception("no weekday given in array");
  3486. }
  3487. } else {
  3488. switch(iconv_strlen($weekday, 'UTF-8')) {
  3489. case 1:
  3490. $type = self::WEEKDAY_NARROW;
  3491. break;
  3492. case 2:
  3493. $type = self::WEEKDAY_NAME;
  3494. break;
  3495. case 3:
  3496. $type = self::WEEKDAY_SHORT;
  3497. break;
  3498. default:
  3499. $type = self::WEEKDAY;
  3500. break;
  3501. }
  3502. }
  3503. $return = $this->_calcdetail($calc, $weekday, $type, $locale);
  3504. if ($calc != 'cmp') {
  3505. return $this;
  3506. }
  3507. return $return;
  3508. }
  3509. /**
  3510. * Sets a new weekday
  3511. * The weekday can be a number or a string. If a localized weekday name is given,
  3512. * then it will be parsed as a date in $locale (defaults to the same locale as $this).
  3513. * Returned is the new date object.
  3514. * Example: setWeekday(3); will set the wednesday of this week as day.
  3515. *
  3516. * @param string|integer|array|Zend_Date $month Weekday to set
  3517. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3518. * @return Zend_Date Provides fluid interface
  3519. * @throws Zend_Date_Exception
  3520. */
  3521. public function setWeekday($weekday, $locale = null)
  3522. {
  3523. return $this->_weekday('set', $weekday, $locale);
  3524. }
  3525. /**
  3526. * Adds weekdays to the existing date object.
  3527. * The weekday can be a number or a string.
  3528. * If a localized dayname is given it will be parsed with the default locale or the optional
  3529. * set locale.
  3530. * Returned is the new date object
  3531. * Example: addWeekday(3); will add the difference of days from the begining of the month until
  3532. * wednesday.
  3533. *
  3534. * @param string|integer|array|Zend_Date $month Weekday to add
  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 addWeekday($weekday, $locale = null)
  3540. {
  3541. return $this->_weekday('add', $weekday, $locale);
  3542. }
  3543. /**
  3544. * Subtracts weekdays from 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: subWeekday(3); will subtract the difference of days from the begining of the month until
  3550. * wednesday.
  3551. *
  3552. * @param string|integer|array|Zend_Date $month Weekday to sub
  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 subWeekday($weekday, $locale = null)
  3558. {
  3559. return $this->_weekday('sub', $weekday, $locale);
  3560. }
  3561. /**
  3562. * Compares the weekday with the existing date object, ignoring other date parts.
  3563. * For example: 'Monday', 'en' -> 08.Jan.2007 -> 0
  3564. * Returns if equal, earlier or later
  3565. *
  3566. * @param string|integer|array|Zend_Date $weekday Weekday to compare
  3567. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3568. * @return integer 0 = equal, 1 = later, -1 = earlier
  3569. * @throws Zend_Date_Exception
  3570. */
  3571. public function compareWeekday($weekday, $locale = null)
  3572. {
  3573. return $this->_weekday('cmp', $weekday, $locale);
  3574. }
  3575. /**
  3576. * Returns the day of year as new date object
  3577. * Example: 02.Feb.1986 10:00:00 -> 02.Feb.1970 00:00:00
  3578. *
  3579. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3580. * @return Zend_Date
  3581. */
  3582. public function getDayOfYear($locale = null)
  3583. {
  3584. if (self::$_options['format_type'] == 'php') {
  3585. $format = 'D';
  3586. } else {
  3587. $format = self::DAY_OF_YEAR;
  3588. }
  3589. return $this->copyPart($format, $locale);
  3590. }
  3591. /**
  3592. * Sets a new day of year
  3593. * The day of year is always a number.
  3594. * Returned is the new date object
  3595. * Example: 04.May.2004 -> setDayOfYear(10) -> 10.Jan.2004
  3596. *
  3597. * @param string|integer|array|Zend_Date $day Day of Year to set
  3598. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3599. * @return Zend_Date Provides fluid interface
  3600. * @throws Zend_Date_Exception
  3601. */
  3602. public function setDayOfYear($day, $locale = null)
  3603. {
  3604. return $this->_calcvalue('set', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3605. }
  3606. /**
  3607. * Adds a day of year to the existing date object.
  3608. * The day of year is always a number.
  3609. * Returned is the new date object
  3610. * Example: addDayOfYear(10); will add 10 days to the existing date object.
  3611. *
  3612. * @param string|integer|array|Zend_Date $day Day of Year to add
  3613. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3614. * @return Zend_Date Provides fluid interface
  3615. * @throws Zend_Date_Exception
  3616. */
  3617. public function addDayOfYear($day, $locale = null)
  3618. {
  3619. return $this->_calcvalue('add', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3620. }
  3621. /**
  3622. * Subtracts a day of year from the existing date object.
  3623. * The day of year is always a number.
  3624. * Returned is the new date object
  3625. * Example: subDayOfYear(10); will subtract 10 days from the existing date object.
  3626. *
  3627. * @param string|integer|array|Zend_Date $day Day of Year to sub
  3628. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3629. * @return Zend_Date Provides fluid interface
  3630. * @throws Zend_Date_Exception
  3631. */
  3632. public function subDayOfYear($day, $locale = null)
  3633. {
  3634. return $this->_calcvalue('sub', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3635. }
  3636. /**
  3637. * Compares the day of year with the existing date object.
  3638. * For example: compareDayOfYear(33) -> 02.Feb.2007 -> 0
  3639. * Returns if equal, earlier or later
  3640. *
  3641. * @param string|integer|array|Zend_Date $day Day of Year to compare
  3642. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3643. * @return integer 0 = equal, 1 = later, -1 = earlier
  3644. * @throws Zend_Date_Exception
  3645. */
  3646. public function compareDayOfYear($day, $locale = null)
  3647. {
  3648. return $this->_calcvalue('cmp', $day, 'day of year', self::DAY_OF_YEAR, $locale);
  3649. }
  3650. /**
  3651. * Returns the hour as new date object
  3652. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 10:00:00
  3653. *
  3654. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  3655. * @return Zend_Date
  3656. */
  3657. public function getHour($locale = null)
  3658. {
  3659. return $this->copyPart(self::HOUR, $locale);
  3660. }
  3661. /**
  3662. * Sets a new hour
  3663. * The hour is always a number.
  3664. * Returned is the new date object
  3665. * Example: 04.May.1993 13:07:25 -> setHour(7); -> 04.May.1993 07:07:25
  3666. *
  3667. * @param string|integer|array|Zend_Date $hour Hour to set
  3668. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3669. * @return Zend_Date Provides fluid interface
  3670. * @throws Zend_Date_Exception
  3671. */
  3672. public function setHour($hour, $locale = null)
  3673. {
  3674. return $this->_calcvalue('set', $hour, 'hour', self::HOUR_SHORT, $locale);
  3675. }
  3676. /**
  3677. * Adds hours to the existing date object.
  3678. * The hour is always a number.
  3679. * Returned is the new date object
  3680. * Example: 04.May.1993 13:07:25 -> addHour(12); -> 05.May.1993 01:07:25
  3681. *
  3682. * @param string|integer|array|Zend_Date $hour Hour to add
  3683. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3684. * @return Zend_Date Provides fluid interface
  3685. * @throws Zend_Date_Exception
  3686. */
  3687. public function addHour($hour, $locale = null)
  3688. {
  3689. return $this->_calcvalue('add', $hour, 'hour', self::HOUR_SHORT, $locale);
  3690. }
  3691. /**
  3692. * Subtracts hours from the existing date object.
  3693. * The hour is always a number.
  3694. * Returned is the new date object
  3695. * Example: 04.May.1993 13:07:25 -> subHour(6); -> 05.May.1993 07:07:25
  3696. *
  3697. * @param string|integer|array|Zend_Date $hour Hour to sub
  3698. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3699. * @return Zend_Date Provides fluid interface
  3700. * @throws Zend_Date_Exception
  3701. */
  3702. public function subHour($hour, $locale = null)
  3703. {
  3704. return $this->_calcvalue('sub', $hour, 'hour', self::HOUR_SHORT, $locale);
  3705. }
  3706. /**
  3707. * Compares the hour with the existing date object.
  3708. * For example: 10:30:25 -> compareHour(10) -> 0
  3709. * Returns if equal, earlier or later
  3710. *
  3711. * @param string|integer|array|Zend_Date $hour Hour to compare
  3712. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3713. * @return integer 0 = equal, 1 = later, -1 = earlier
  3714. * @throws Zend_Date_Exception
  3715. */
  3716. public function compareHour($hour, $locale = null)
  3717. {
  3718. return $this->_calcvalue('cmp', $hour, 'hour', self::HOUR_SHORT, $locale);
  3719. }
  3720. /**
  3721. * Returns the minute as new date object
  3722. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:30:00
  3723. *
  3724. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3725. * @return Zend_Date
  3726. */
  3727. public function getMinute($locale = null)
  3728. {
  3729. if (self::$_options['format_type'] == 'php') {
  3730. $format = 'i';
  3731. } else {
  3732. $format = self::MINUTE;
  3733. }
  3734. return $this->copyPart($format, $locale);
  3735. }
  3736. /**
  3737. * Sets a new minute
  3738. * The minute is always a number.
  3739. * Returned is the new date object
  3740. * Example: 04.May.1993 13:07:25 -> setMinute(29); -> 04.May.1993 13:29:25
  3741. *
  3742. * @param string|integer|array|Zend_Date $minute Minute to set
  3743. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3744. * @return Zend_Date Provides fluid interface
  3745. * @throws Zend_Date_Exception
  3746. */
  3747. public function setMinute($minute, $locale = null)
  3748. {
  3749. return $this->_calcvalue('set', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3750. }
  3751. /**
  3752. * Adds minutes to the existing date object.
  3753. * The minute is always a number.
  3754. * Returned is the new date object
  3755. * Example: 04.May.1993 13:07:25 -> addMinute(65); -> 04.May.1993 13:12:25
  3756. *
  3757. * @param string|integer|array|Zend_Date $minute Minute to add
  3758. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3759. * @return Zend_Date Provides fluid interface
  3760. * @throws Zend_Date_Exception
  3761. */
  3762. public function addMinute($minute, $locale = null)
  3763. {
  3764. return $this->_calcvalue('add', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3765. }
  3766. /**
  3767. * Subtracts minutes from the existing date object.
  3768. * The minute is always a number.
  3769. * Returned is the new date object
  3770. * Example: 04.May.1993 13:07:25 -> subMinute(9); -> 04.May.1993 12:58:25
  3771. *
  3772. * @param string|integer|array|Zend_Date $minute Minute to sub
  3773. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3774. * @return Zend_Date Provides fluid interface
  3775. * @throws Zend_Date_Exception
  3776. */
  3777. public function subMinute($minute, $locale = null)
  3778. {
  3779. return $this->_calcvalue('sub', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3780. }
  3781. /**
  3782. * Compares the minute with the existing date object.
  3783. * For example: 10:30:25 -> compareMinute(30) -> 0
  3784. * Returns if equal, earlier or later
  3785. *
  3786. * @param string|integer|array|Zend_Date $minute Hour to compare
  3787. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3788. * @return integer 0 = equal, 1 = later, -1 = earlier
  3789. * @throws Zend_Date_Exception
  3790. */
  3791. public function compareMinute($minute, $locale = null)
  3792. {
  3793. return $this->_calcvalue('cmp', $minute, 'minute', self::MINUTE_SHORT, $locale);
  3794. }
  3795. /**
  3796. * Returns the second as new date object
  3797. * Example: 02.Feb.1986 10:30:25 -> 01.Jan.1970 00:00:25
  3798. *
  3799. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  3800. * @return Zend_Date
  3801. */
  3802. public function getSecond($locale = null)
  3803. {
  3804. if (self::$_options['format_type'] == 'php') {
  3805. $format = 's';
  3806. } else {
  3807. $format = self::SECOND;
  3808. }
  3809. return $this->copyPart($format, $locale);
  3810. }
  3811. /**
  3812. * Sets new seconds to the existing date object.
  3813. * The second is always a number.
  3814. * Returned is the new date object
  3815. * Example: 04.May.1993 13:07:25 -> setSecond(100); -> 04.May.1993 13:08:40
  3816. *
  3817. * @param string|integer|array|Zend_Date $second Second to set
  3818. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3819. * @return Zend_Date Provides fluid interface
  3820. * @throws Zend_Date_Exception
  3821. */
  3822. public function setSecond($second, $locale = null)
  3823. {
  3824. return $this->_calcvalue('set', $second, 'second', self::SECOND_SHORT, $locale);
  3825. }
  3826. /**
  3827. * Adds seconds to the existing date object.
  3828. * The second is always a number.
  3829. * Returned is the new date object
  3830. * Example: 04.May.1993 13:07:25 -> addSecond(65); -> 04.May.1993 13:08:30
  3831. *
  3832. * @param string|integer|array|Zend_Date $second Second to add
  3833. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3834. * @return Zend_Date Provides fluid interface
  3835. * @throws Zend_Date_Exception
  3836. */
  3837. public function addSecond($second, $locale = null)
  3838. {
  3839. return $this->_calcvalue('add', $second, 'second', self::SECOND_SHORT, $locale);
  3840. }
  3841. /**
  3842. * Subtracts seconds from the existing date object.
  3843. * The second is always a number.
  3844. * Returned is the new date object
  3845. * Example: 04.May.1993 13:07:25 -> subSecond(10); -> 04.May.1993 13:07:15
  3846. *
  3847. * @param string|integer|array|Zend_Date $second Second to sub
  3848. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3849. * @return Zend_Date Provides fluid interface
  3850. * @throws Zend_Date_Exception
  3851. */
  3852. public function subSecond($second, $locale = null)
  3853. {
  3854. return $this->_calcvalue('sub', $second, 'second', self::SECOND_SHORT, $locale);
  3855. }
  3856. /**
  3857. * Compares the second with the existing date object.
  3858. * For example: 10:30:25 -> compareSecond(25) -> 0
  3859. * Returns if equal, earlier or later
  3860. *
  3861. * @param string|integer|array|Zend_Date $second Second to compare
  3862. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  3863. * @return integer 0 = equal, 1 = later, -1 = earlier
  3864. * @throws Zend_Date_Exception
  3865. */
  3866. public function compareSecond($second, $locale = null)
  3867. {
  3868. return $this->_calcvalue('cmp', $second, 'second', self::SECOND_SHORT, $locale);
  3869. }
  3870. /**
  3871. * Returns the precision for fractional seconds
  3872. *
  3873. * @return integer
  3874. */
  3875. public function getFractionalPrecision()
  3876. {
  3877. return $this->_precision;
  3878. }
  3879. /**
  3880. * Sets a new precision for fractional seconds
  3881. *
  3882. * @param integer $precision Precision for the fractional datepart 3 = milliseconds
  3883. * @throws Zend_Date_Exception
  3884. * @return Zend_Date Provides fluid interface
  3885. */
  3886. public function setFractionalPrecision($precision)
  3887. {
  3888. if (!intval($precision) or ($precision < 0) or ($precision > 9)) {
  3889. require_once 'Zend/Date/Exception.php';
  3890. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3891. }
  3892. $this->_precision = (int) $precision;
  3893. if ($this->_precision < strlen($this->_fractional)) {
  3894. $this->_fractional = substr($this->_fractional, 0, $this->_precision);
  3895. } else {
  3896. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_RIGHT);
  3897. }
  3898. return $this;
  3899. }
  3900. /**
  3901. * Returns the milliseconds of the date object
  3902. *
  3903. * @return string
  3904. */
  3905. public function getMilliSecond()
  3906. {
  3907. return $this->_fractional;
  3908. }
  3909. /**
  3910. * Sets new milliseconds for the date object
  3911. * Example: setMilliSecond(550, 2) -> equals +5 Sec +50 MilliSec
  3912. *
  3913. * @param integer|Zend_Date $milli (Optional) Millisecond to set, when null the actual millisecond is set
  3914. * @param integer $precision (Optional) Fraction precision of the given milliseconds
  3915. * @return Zend_Date Provides fluid interface
  3916. */
  3917. public function setMilliSecond($milli = null, $precision = null)
  3918. {
  3919. if ($milli === null) {
  3920. list($milli, $time) = explode(" ", microtime());
  3921. $milli = intval($milli);
  3922. $precision = 6;
  3923. } else if (!is_numeric($milli)) {
  3924. require_once 'Zend/Date/Exception.php';
  3925. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3926. }
  3927. if ($precision === null) {
  3928. $precision = $this->_precision;
  3929. }
  3930. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3931. require_once 'Zend/Date/Exception.php';
  3932. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3933. }
  3934. $this->_fractional = 0;
  3935. $this->addMilliSecond($milli, $precision);
  3936. return $this;
  3937. }
  3938. /**
  3939. * Adds milliseconds to the date object
  3940. *
  3941. * @param integer|Zend_Date $milli (Optional) Millisecond to add, when null the actual millisecond is added
  3942. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3943. * @return Zend_Date Provides fluid interface
  3944. */
  3945. public function addMilliSecond($milli = null, $precision = null)
  3946. {
  3947. if ($milli === null) {
  3948. list($milli, $time) = explode(" ", microtime());
  3949. $milli = intval($milli);
  3950. } else if (!is_numeric($milli)) {
  3951. require_once 'Zend/Date/Exception.php';
  3952. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  3953. }
  3954. if ($precision === null) {
  3955. $precision = strlen($milli);
  3956. if ($milli < 0) {
  3957. --$precision;
  3958. }
  3959. }
  3960. if (!is_int($precision) || $precision < 1 || $precision > 9) {
  3961. require_once 'Zend/Date/Exception.php';
  3962. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  3963. }
  3964. $this->_fractional += $milli;
  3965. // Add/sub milliseconds + add/sub seconds
  3966. $max = pow(10, $this->_precision);
  3967. // Milli includes seconds
  3968. if ($this->_fractional >= $max) {
  3969. while ($this->_fractional >= $max) {
  3970. $this->addSecond(1);
  3971. $this->_fractional -= $max;
  3972. }
  3973. }
  3974. if ($this->_fractional < 0) {
  3975. while ($this->_fractional < 0) {
  3976. $this->subSecond(1);
  3977. $this->_fractional += $max;
  3978. }
  3979. }
  3980. if ($this->_precision > strlen($this->_fractional)) {
  3981. $this->_fractional = str_pad($this->_fractional, $this->_precision, '0', STR_PAD_LEFT);
  3982. }
  3983. return $this;
  3984. }
  3985. /**
  3986. * Subtracts a millisecond
  3987. *
  3988. * @param integer|Zend_Date $milli (Optional) Millisecond to sub, when null the actual millisecond is subtracted
  3989. * @param integer $precision (Optional) Fractional precision for the given milliseconds
  3990. * @return Zend_Date Provides fluid interface
  3991. */
  3992. public function subMilliSecond($milli = null, $precision = null)
  3993. {
  3994. $this->addMilliSecond(0 - $milli, $precision);
  3995. return $this;
  3996. }
  3997. /**
  3998. * Compares only the millisecond part, returning the difference
  3999. *
  4000. * @param integer|Zend_Date $milli OPTIONAL Millisecond to compare, when null the actual millisecond is compared
  4001. * @param integer $precision OPTIONAL Fractional precision for the given milliseconds
  4002. * @throws Zend_Date_Exception On invalid input
  4003. * @return integer 0 = equal, 1 = later, -1 = earlier
  4004. */
  4005. public function compareMilliSecond($milli = null, $precision = null)
  4006. {
  4007. if ($milli === null) {
  4008. list($milli, $time) = explode(" ", microtime());
  4009. $milli = intval($milli);
  4010. } else if (is_numeric($milli) === false) {
  4011. require_once 'Zend/Date/Exception.php';
  4012. throw new Zend_Date_Exception("invalid milli second ($milli) operand", 0, null, $milli);
  4013. }
  4014. if ($precision === null) {
  4015. $precision = strlen($milli);
  4016. } else if (!is_int($precision) || $precision < 1 || $precision > 9) {
  4017. require_once 'Zend/Date/Exception.php';
  4018. throw new Zend_Date_Exception("precision ($precision) must be a positive integer less than 10", 0, null, $precision);
  4019. }
  4020. if ($precision === 0) {
  4021. require_once 'Zend/Date/Exception.php';
  4022. throw new Zend_Date_Exception('precision is 0');
  4023. }
  4024. if ($precision != $this->_precision) {
  4025. if ($precision > $this->_precision) {
  4026. $diff = $precision - $this->_precision;
  4027. $milli = (int) ($milli / (10 * $diff));
  4028. } else {
  4029. $diff = $this->_precision - $precision;
  4030. $milli = (int) ($milli * (10 * $diff));
  4031. }
  4032. }
  4033. $comp = $this->_fractional - $milli;
  4034. if ($comp < 0) {
  4035. return -1;
  4036. } else if ($comp > 0) {
  4037. return 1;
  4038. }
  4039. return 0;
  4040. }
  4041. /**
  4042. * Returns the week as new date object using monday as begining of the week
  4043. * Example: 12.Jan.2007 -> 08.Jan.1970 00:00:00
  4044. *
  4045. * @param $locale string|Zend_Locale OPTIONAL Locale for parsing input
  4046. * @return Zend_Date
  4047. */
  4048. public function getWeek($locale = null)
  4049. {
  4050. if (self::$_options['format_type'] == 'php') {
  4051. $format = 'W';
  4052. } else {
  4053. $format = self::WEEK;
  4054. }
  4055. return $this->copyPart($format, $locale);
  4056. }
  4057. /**
  4058. * Sets a new week. The week is always a number. The day of week is not changed.
  4059. * Returned is the new date object
  4060. * Example: 09.Jan.2007 13:07:25 -> setWeek(1); -> 02.Jan.2007 13:07:25
  4061. *
  4062. * @param string|integer|array|Zend_Date $week Week to set
  4063. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4064. * @return Zend_Date Provides fluid interface
  4065. * @throws Zend_Date_Exception
  4066. */
  4067. public function setWeek($week, $locale = null)
  4068. {
  4069. return $this->_calcvalue('set', $week, 'week', self::WEEK, $locale);
  4070. }
  4071. /**
  4072. * Adds a week. The week is always a number. The day of week is not changed.
  4073. * Returned is the new date object
  4074. * Example: 09.Jan.2007 13:07:25 -> addWeek(1); -> 16.Jan.2007 13:07:25
  4075. *
  4076. * @param string|integer|array|Zend_Date $week Week to add
  4077. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4078. * @return Zend_Date Provides fluid interface
  4079. * @throws Zend_Date_Exception
  4080. */
  4081. public function addWeek($week, $locale = null)
  4082. {
  4083. return $this->_calcvalue('add', $week, 'week', self::WEEK, $locale);
  4084. }
  4085. /**
  4086. * Subtracts a week. The week is always a number. The day of week is not changed.
  4087. * Returned is the new date object
  4088. * Example: 09.Jan.2007 13:07:25 -> subWeek(1); -> 02.Jan.2007 13:07:25
  4089. *
  4090. * @param string|integer|array|Zend_Date $week Week to sub
  4091. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4092. * @return Zend_Date Provides fluid interface
  4093. * @throws Zend_Date_Exception
  4094. */
  4095. public function subWeek($week, $locale = null)
  4096. {
  4097. return $this->_calcvalue('sub', $week, 'week', self::WEEK, $locale);
  4098. }
  4099. /**
  4100. * Compares only the week part, returning the difference
  4101. * Returned is the new date object
  4102. * Returns if equal, earlier or later
  4103. * Example: 09.Jan.2007 13:07:25 -> compareWeek(2); -> 0
  4104. *
  4105. * @param string|integer|array|Zend_Date $week Week to compare
  4106. * @param string|Zend_Locale $locale OPTIONAL Locale for parsing input
  4107. * @return integer 0 = equal, 1 = later, -1 = earlier
  4108. */
  4109. public function compareWeek($week, $locale = null)
  4110. {
  4111. return $this->_calcvalue('cmp', $week, 'week', self::WEEK, $locale);
  4112. }
  4113. /**
  4114. * Sets a new standard locale for the date object.
  4115. * This locale will be used for all functions
  4116. * Returned is the really set locale.
  4117. * Example: 'de_XX' will be set to 'de' because 'de_XX' does not exist
  4118. * 'xx_YY' will be set to 'root' because 'xx' does not exist
  4119. *
  4120. * @param string|Zend_Locale $locale (Optional) Locale for parsing input
  4121. * @throws Zend_Date_Exception When the given locale does not exist
  4122. * @return Zend_Date Provides fluent interface
  4123. */
  4124. public function setLocale($locale = null)
  4125. {
  4126. try {
  4127. $this->_locale = Zend_Locale::findLocale($locale);
  4128. } catch (Zend_Locale_Exception $e) {
  4129. require_once 'Zend/Date/Exception.php';
  4130. throw new Zend_Date_Exception($e->getMessage(), 0, $e);
  4131. }
  4132. return $this;
  4133. }
  4134. /**
  4135. * Returns the actual set locale
  4136. *
  4137. * @return string
  4138. */
  4139. public function getLocale()
  4140. {
  4141. return $this->_locale;
  4142. }
  4143. /**
  4144. * Checks if the given date is a real date or datepart.
  4145. * Returns false if a expected datepart is missing or a datepart exceeds its possible border.
  4146. * But the check will only be done for the expected dateparts which are given by format.
  4147. * If no format is given the standard dateformat for the actual locale is used.
  4148. * f.e. 30.February.2007 will return false if format is 'dd.MMMM.YYYY'
  4149. *
  4150. * @param string|array|Zend_Date $date Date to parse for correctness
  4151. * @param string $format (Optional) Format for parsing the date string
  4152. * @param string|Zend_Locale $locale (Optional) Locale for parsing date parts
  4153. * @return boolean True when all date parts are correct
  4154. */
  4155. public static function isDate($date, $format = null, $locale = null)
  4156. {
  4157. if (!is_string($date) && !is_numeric($date) && !($date instanceof Zend_Date) &&
  4158. !is_array($date)) {
  4159. return false;
  4160. }
  4161. if (($format !== null) and (Zend_Locale::isLocale($format, null, false))) {
  4162. $locale = $format;
  4163. $format = null;
  4164. }
  4165. $locale = Zend_Locale::findLocale($locale);
  4166. if ($format === null) {
  4167. $format = Zend_Locale_Format::getDateFormat($locale);
  4168. } else if ((self::$_options['format_type'] == 'php') && !defined($format)) {
  4169. $format = Zend_Locale_Format::convertPhpToIsoFormat($format);
  4170. }
  4171. $format = self::_getLocalizedToken($format, $locale);
  4172. if (!is_array($date)) {
  4173. try {
  4174. $parsed = Zend_Locale_Format::getDate($date, array('locale' => $locale,
  4175. 'date_format' => $format, 'format_type' => 'iso',
  4176. 'fix_date' => false));
  4177. } catch (Zend_Locale_Exception $e) {
  4178. // Date can not be parsed
  4179. return false;
  4180. }
  4181. } else {
  4182. $parsed = $date;
  4183. }
  4184. if (((strpos($format, 'Y') !== false) or (strpos($format, 'y') !== false)) and
  4185. (!isset($parsed['year']))) {
  4186. // Year expected but not found
  4187. return false;
  4188. }
  4189. if ((strpos($format, 'M') !== false) and (!isset($parsed['month']))) {
  4190. // Month expected but not found
  4191. return false;
  4192. }
  4193. if ((strpos($format, 'd') !== false) and (!isset($parsed['day']))) {
  4194. // Day expected but not found
  4195. return false;
  4196. }
  4197. if (((strpos($format, 'H') !== false) or (strpos($format, 'h') !== false)) and
  4198. (!isset($parsed['hour']))) {
  4199. // Hour expected but not found
  4200. return false;
  4201. }
  4202. if ((strpos($format, 'm') !== false) and (!isset($parsed['minute']))) {
  4203. // Minute expected but not found
  4204. return false;
  4205. }
  4206. if ((strpos($format, 's') !== false) and (!isset($parsed['second']))) {
  4207. // Second expected but not found
  4208. return false;
  4209. }
  4210. // Set not given dateparts
  4211. if (isset($parsed['hour']) === false) {
  4212. $parsed['hour'] = 12;
  4213. }
  4214. if (isset($parsed['minute']) === false) {
  4215. $parsed['minute'] = 0;
  4216. }
  4217. if (isset($parsed['second']) === false) {
  4218. $parsed['second'] = 0;
  4219. }
  4220. if (isset($parsed['month']) === false) {
  4221. $parsed['month'] = 1;
  4222. }
  4223. if (isset($parsed['day']) === false) {
  4224. $parsed['day'] = 1;
  4225. }
  4226. if (isset($parsed['year']) === false) {
  4227. $parsed['year'] = 1970;
  4228. }
  4229. if (self::isYearLeapYear($parsed['year'])) {
  4230. $parsed['year'] = 1972;
  4231. } else {
  4232. $parsed['year'] = 1971;
  4233. }
  4234. $date = new self($parsed, null, $locale);
  4235. $timestamp = $date->mktime($parsed['hour'], $parsed['minute'], $parsed['second'],
  4236. $parsed['month'], $parsed['day'], $parsed['year']);
  4237. if ($parsed['year'] != $date->date('Y', $timestamp)) {
  4238. // Given year differs from parsed year
  4239. return false;
  4240. }
  4241. if ($parsed['month'] != $date->date('n', $timestamp)) {
  4242. // Given month differs from parsed month
  4243. return false;
  4244. }
  4245. if ($parsed['day'] != $date->date('j', $timestamp)) {
  4246. // Given day differs from parsed day
  4247. return false;
  4248. }
  4249. if ($parsed['hour'] != $date->date('G', $timestamp)) {
  4250. // Given hour differs from parsed hour
  4251. return false;
  4252. }
  4253. if ($parsed['minute'] != $date->date('i', $timestamp)) {
  4254. // Given minute differs from parsed minute
  4255. return false;
  4256. }
  4257. if ($parsed['second'] != $date->date('s', $timestamp)) {
  4258. // Given second differs from parsed second
  4259. return false;
  4260. }
  4261. return true;
  4262. }
  4263. /**
  4264. * Returns the ISO Token for all localized constants
  4265. *
  4266. * @param string $token Token to normalize
  4267. * @param string $locale Locale to search
  4268. * @return string
  4269. */
  4270. protected static function _getLocalizedToken($token, $locale)
  4271. {
  4272. switch($token) {
  4273. case self::ISO_8601 :
  4274. return "yyyy-MM-ddThh:mm:ss";
  4275. break;
  4276. case self::RFC_2822 :
  4277. return "EEE, dd MMM yyyy HH:mm:ss";
  4278. break;
  4279. case self::DATES :
  4280. return Zend_Locale_Data::getContent($locale, 'date');
  4281. break;
  4282. case self::DATE_FULL :
  4283. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'full'));
  4284. break;
  4285. case self::DATE_LONG :
  4286. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4287. break;
  4288. case self::DATE_MEDIUM :
  4289. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4290. break;
  4291. case self::DATE_SHORT :
  4292. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4293. break;
  4294. case self::TIMES :
  4295. return Zend_Locale_Data::getContent($locale, 'date');
  4296. break;
  4297. case self::TIME_FULL :
  4298. return Zend_Locale_Data::getContent($locale, 'time', array('gregorian', 'full'));
  4299. break;
  4300. case self::TIME_LONG :
  4301. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'long'));
  4302. break;
  4303. case self::TIME_MEDIUM :
  4304. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'medium'));
  4305. break;
  4306. case self::TIME_SHORT :
  4307. return Zend_Locale_Data::getContent($locale, 'date', array('gregorian', 'short'));
  4308. break;
  4309. case self::DATETIME :
  4310. return Zend_Locale_Data::getContent($locale, 'datetime');
  4311. break;
  4312. case self::DATETIME_FULL :
  4313. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'full'));
  4314. break;
  4315. case self::DATETIME_LONG :
  4316. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'long'));
  4317. break;
  4318. case self::DATETIME_MEDIUM :
  4319. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'medium'));
  4320. break;
  4321. case self::DATETIME_SHORT :
  4322. return Zend_Locale_Data::getContent($locale, 'datetime', array('gregorian', 'short'));
  4323. break;
  4324. case self::ATOM :
  4325. case self::RFC_3339 :
  4326. case self::W3C :
  4327. return "yyyy-MM-DD HH:mm:ss";
  4328. break;
  4329. case self::COOKIE :
  4330. case self::RFC_850 :
  4331. return "EEEE, dd-MM-yyyy HH:mm:ss";
  4332. break;
  4333. case self::RFC_822 :
  4334. case self::RFC_1036 :
  4335. case self::RFC_1123 :
  4336. case self::RSS :
  4337. return "EEE, dd MM yyyy HH:mm:ss";
  4338. break;
  4339. }
  4340. return $token;
  4341. }
  4342. }