PageRenderTime 457ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-1.5.0PR/library/Zend/Date.php

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