PageRenderTime 67ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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