PageRenderTime 92ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Date.php

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