PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/app/sitesearch/lib/Zend/Date.php

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