PageRenderTime 84ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/library/Zend/Date.php

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