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

/DevApp/library/Zend/Date.php

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