DateObject.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @version $Id$
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Date
  24. * @subpackage Zend_Date_DateObject
  25. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. abstract class Zend_Date_DateObject {
  29. /**
  30. * UNIX Timestamp
  31. */
  32. private $_unixTimestamp;
  33. protected static $_cache = null;
  34. protected static $_defaultOffset = 0;
  35. /**
  36. * active timezone
  37. */
  38. private $_timezone = 'UTC';
  39. private $_offset = 0;
  40. private $_syncronised = 0;
  41. // turn off DST correction if UTC or GMT
  42. protected $_dst = true;
  43. /**
  44. * Table of Monthdays
  45. */
  46. private static $_monthTable = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  47. /**
  48. * Table of Years
  49. */
  50. private static $_yearTable = array(
  51. 1970 => 0, 1960 => -315619200, 1950 => -631152000,
  52. 1940 => -946771200, 1930 => -1262304000, 1920 => -1577923200,
  53. 1910 => -1893456000, 1900 => -2208988800, 1890 => -2524521600,
  54. 1880 => -2840140800, 1870 => -3155673600, 1860 => -3471292800,
  55. 1850 => -3786825600, 1840 => -4102444800, 1830 => -4417977600,
  56. 1820 => -4733596800, 1810 => -5049129600, 1800 => -5364662400,
  57. 1790 => -5680195200, 1780 => -5995814400, 1770 => -6311347200,
  58. 1760 => -6626966400, 1750 => -6942499200, 1740 => -7258118400,
  59. 1730 => -7573651200, 1720 => -7889270400, 1710 => -8204803200,
  60. 1700 => -8520336000, 1690 => -8835868800, 1680 => -9151488000,
  61. 1670 => -9467020800, 1660 => -9782640000, 1650 => -10098172800,
  62. 1640 => -10413792000, 1630 => -10729324800, 1620 => -11044944000,
  63. 1610 => -11360476800, 1600 => -11676096000);
  64. /**
  65. * Set this object to have a new UNIX timestamp.
  66. *
  67. * @param string|integer $timestamp OPTIONAL timestamp; defaults to local time using time()
  68. * @return string|integer old timestamp
  69. * @throws Zend_Date_Exception
  70. */
  71. protected function setUnixTimestamp($timestamp = null)
  72. {
  73. $old = $this->_unixTimestamp;
  74. if (is_numeric($timestamp)) {
  75. $this->_unixTimestamp = $timestamp;
  76. } else if ($timestamp === null) {
  77. $this->_unixTimestamp = time();
  78. } else {
  79. require_once 'Zend/Date/Exception.php';
  80. throw new Zend_Date_Exception('\'' . $timestamp . '\' is not a valid UNIX timestamp', $timestamp);
  81. }
  82. return $old;
  83. }
  84. /**
  85. * Returns this object's UNIX timestamp
  86. * A timestamp greater then the integer range will be returned as string
  87. * This function does not return the timestamp as object. Use copy() instead.
  88. *
  89. * @return integer|string timestamp
  90. */
  91. protected function getUnixTimestamp()
  92. {
  93. if ($this->_unixTimestamp === intval($this->_unixTimestamp)) {
  94. return (int) $this->_unixTimestamp;
  95. } else {
  96. return (string) $this->_unixTimestamp;
  97. }
  98. }
  99. /**
  100. * Internal function.
  101. * Returns time(). This method exists to allow unit tests to work-around methods that might otherwise
  102. * be hard-coded to use time(). For example, this makes it possible to test isYesterday() in Date.php.
  103. *
  104. * @param integer $sync OPTIONAL time syncronisation value
  105. * @return integer timestamp
  106. */
  107. protected function _getTime($sync = null)
  108. {
  109. if ($sync !== null) {
  110. $this->_syncronised = round($sync);
  111. }
  112. return (time() + $this->_syncronised);
  113. }
  114. /**
  115. * Internal mktime function used by Zend_Date.
  116. * The timestamp returned by mktime() can exceed the precision of traditional UNIX timestamps,
  117. * by allowing PHP to auto-convert to using a float value.
  118. *
  119. * Returns a timestamp relative to 1970/01/01 00:00:00 GMT/UTC.
  120. * DST (Summer/Winter) is depriciated since php 5.1.0.
  121. * Year has to be 4 digits otherwise it would be recognised as
  122. * year 70 AD instead of 1970 AD as expected !!
  123. *
  124. * @param integer $hour
  125. * @param integer $minute
  126. * @param integer $second
  127. * @param integer $month
  128. * @param integer $day
  129. * @param integer $year
  130. * @param boolean $gmt OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
  131. * @return integer|float timestamp (number of seconds elapsed relative to 1970/01/01 00:00:00 GMT/UTC)
  132. */
  133. protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
  134. {
  135. // complete date but in 32bit timestamp - use PHP internal
  136. if ((1901 < $year) and ($year < 2038)) {
  137. $oldzone = @date_default_timezone_get();
  138. // Timezone also includes DST settings, therefor substracting the GMT offset is not enough
  139. // We have to set the correct timezone to get the right value
  140. if (($this->_timezone != $oldzone) and ($gmt === false)) {
  141. date_default_timezone_set($this->_timezone);
  142. }
  143. $result = ($gmt) ? @gmmktime($hour, $minute, $second, $month, $day, $year)
  144. : @mktime($hour, $minute, $second, $month, $day, $year);
  145. date_default_timezone_set($oldzone);
  146. return $result;
  147. }
  148. if ($gmt !== true) {
  149. $second += $this->_offset;
  150. }
  151. if (isset(self::$_cache)) {
  152. $id = strtr('Zend_DateObject_mkTime_' . $this->_offset . '_' . $year.$month.$day.'_'.$hour.$minute.$second . '_'.(int)$gmt, '-','_');
  153. if ($result = self::$_cache->load($id)) {
  154. return unserialize($result);
  155. }
  156. }
  157. // date to integer
  158. $day = intval($day);
  159. $month = intval($month);
  160. $year = intval($year);
  161. // correct months > 12 and months < 1
  162. if ($month > 12) {
  163. $overlap = floor($month / 12);
  164. $year += $overlap;
  165. $month -= $overlap * 12;
  166. } else {
  167. $overlap = ceil((1 - $month) / 12);
  168. $year -= $overlap;
  169. $month += $overlap * 12;
  170. }
  171. $date = 0;
  172. if ($year >= 1970) {
  173. // Date is after UNIX epoch
  174. // go through leapyears
  175. // add months from latest given year
  176. for ($count = 1970; $count <= $year; $count++) {
  177. $leapyear = self::isYearLeapYear($count);
  178. if ($count < $year) {
  179. $date += 365;
  180. if ($leapyear === true) {
  181. $date++;
  182. }
  183. } else {
  184. for ($mcount = 0; $mcount < ($month - 1); $mcount++) {
  185. $date += self::$_monthTable[$mcount];
  186. if (($leapyear === true) and ($mcount == 1)) {
  187. $date++;
  188. }
  189. }
  190. }
  191. }
  192. $date += $day - 1;
  193. $date = (($date * 86400) + ($hour * 3600) + ($minute * 60) + $second);
  194. } else {
  195. // Date is before UNIX epoch
  196. // go through leapyears
  197. // add months from latest given year
  198. for ($count = 1969; $count >= $year; $count--) {
  199. $leapyear = self::isYearLeapYear($count);
  200. if ($count > $year)
  201. {
  202. $date += 365;
  203. if ($leapyear === true)
  204. $date++;
  205. } else {
  206. for ($mcount = 11; $mcount > ($month - 1); $mcount--) {
  207. $date += self::$_monthTable[$mcount];
  208. if (($leapyear === true) and ($mcount == 1)) {
  209. $date++;
  210. }
  211. }
  212. }
  213. }
  214. $date += (self::$_monthTable[$month - 1] - $day);
  215. $date = -(($date * 86400) + (86400 - (($hour * 3600) + ($minute * 60) + $second)));
  216. // gregorian correction for 5.Oct.1582
  217. if ($date < -12220185600) {
  218. $date += 864000;
  219. } else if ($date < -12219321600) {
  220. $date = -12219321600;
  221. }
  222. }
  223. if (isset(self::$_cache)) {
  224. self::$_cache->save( serialize($date), $id);
  225. }
  226. return $date;
  227. }
  228. /**
  229. * Returns true, if given $year is a leap year.
  230. *
  231. * @param integer $year
  232. * @return boolean true, if year is leap year
  233. */
  234. protected static function isYearLeapYear($year)
  235. {
  236. // all leapyears can be divided through 4
  237. if (($year % 4) != 0) {
  238. return false;
  239. }
  240. // all leapyears can be divided through 400
  241. if ($year % 400 == 0) {
  242. return true;
  243. } else if (($year > 1582) and ($year % 100 == 0)) {
  244. return false;
  245. }
  246. return true;
  247. }
  248. /**
  249. * Internal mktime function used by Zend_Date for handling 64bit timestamps.
  250. *
  251. * Returns a formatted date for a given timestamp.
  252. *
  253. * @param string $format format for output
  254. * @param mixed $timestamp
  255. * @param boolean $gmt OPTIONAL true = other arguments are for UTC time, false = arguments are for local time/date
  256. * @return string
  257. */
  258. protected function date($format, $timestamp = null, $gmt = false)
  259. {
  260. $oldzone = @date_default_timezone_get();
  261. if ($this->_timezone != $oldzone) {
  262. date_default_timezone_set($this->_timezone);
  263. }
  264. if ($timestamp === null) {
  265. $result = ($gmt) ? @gmdate($format) : @date($format);
  266. date_default_timezone_set($oldzone);
  267. return $result;
  268. }
  269. if (abs($timestamp) <= 0x7FFFFFFF) {
  270. $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
  271. date_default_timezone_set($oldzone);
  272. return $result;
  273. }
  274. $jump = false;
  275. $origstamp = $timestamp;
  276. if (isset(self::$_cache)) {
  277. $idstamp = strtr('Zend_DateObject_date_' . $this->_offset . '_'. $timestamp . '_'.(int)$gmt, '-','_');
  278. if ($result2 = self::$_cache->load($idstamp)) {
  279. $timestamp = unserialize($result2);
  280. $jump = true;
  281. }
  282. }
  283. // check on false or null alone failes
  284. if (empty($gmt) and empty($jump)) {
  285. $tempstamp = $timestamp;
  286. if ($tempstamp > 0) {
  287. while (abs($tempstamp) > 0x7FFFFFFF) {
  288. $tempstamp -= (86400 * 23376);
  289. }
  290. $dst = date("I", $tempstamp);
  291. if ($dst === 1) {
  292. $timestamp += 3600;
  293. }
  294. $temp = date('Z', $tempstamp);
  295. $timestamp += $temp;
  296. }
  297. if (isset(self::$_cache)) {
  298. self::$_cache->save( serialize($timestamp), $idstamp);
  299. }
  300. }
  301. if (($timestamp < 0) and ($gmt !== true)) {
  302. $timestamp -= $this->_offset;
  303. }
  304. date_default_timezone_set($oldzone);
  305. $date = $this->getDateParts($timestamp, true);
  306. $length = strlen($format);
  307. $output = '';
  308. for ($i = 0; $i < $length; $i++) {
  309. switch($format[$i]) {
  310. // day formats
  311. case 'd': // day of month, 2 digits, with leading zero, 01 - 31
  312. $output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
  313. break;
  314. case 'D': // day of week, 3 letters, Mon - Sun
  315. $output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
  316. break;
  317. case 'j': // day of month, without leading zero, 1 - 31
  318. $output .= $date['mday'];
  319. break;
  320. case 'l': // day of week, full string name, Sunday - Saturday
  321. $output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
  322. break;
  323. case 'N': // ISO 8601 numeric day of week, 1 - 7
  324. $day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
  325. if ($day == 0) {
  326. $day = 7;
  327. }
  328. $output .= $day;
  329. break;
  330. case 'S': // english suffix for day of month, st nd rd th
  331. if (($date['mday'] % 10) == 1) {
  332. $output .= 'st';
  333. } else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
  334. $output .= 'nd';
  335. } else if (($date['mday'] % 10) == 3) {
  336. $output .= 'rd';
  337. } else {
  338. $output .= 'th';
  339. }
  340. break;
  341. case 'w': // numeric day of week, 0 - 6
  342. $output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
  343. break;
  344. case 'z': // day of year, 0 - 365
  345. $output .= $date['yday'];
  346. break;
  347. // week formats
  348. case 'W': // ISO 8601, week number of year
  349. $output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
  350. break;
  351. // month formats
  352. case 'F': // string month name, january - december
  353. $output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
  354. break;
  355. case 'm': // number of month, with leading zeros, 01 - 12
  356. $output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
  357. break;
  358. case 'M': // 3 letter month name, Jan - Dec
  359. $output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
  360. break;
  361. case 'n': // number of month, without leading zeros, 1 - 12
  362. $output .= $date['mon'];
  363. break;
  364. case 't': // number of day in month
  365. $output .= self::$_monthTable[$date['mon'] - 1];
  366. break;
  367. // year formats
  368. case 'L': // is leap year ?
  369. $output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
  370. break;
  371. case 'o': // ISO 8601 year number
  372. $week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
  373. if (($week > 50) and ($date['mon'] == 1)) {
  374. $output .= ($date['year'] - 1);
  375. } else {
  376. $output .= $date['year'];
  377. }
  378. break;
  379. case 'Y': // year number, 4 digits
  380. $output .= $date['year'];
  381. break;
  382. case 'y': // year number, 2 digits
  383. $output .= substr($date['year'], strlen($date['year']) - 2, 2);
  384. break;
  385. // time formats
  386. case 'a': // lower case am/pm
  387. $output .= (($date['hours'] >= 12) ? 'pm' : 'am');
  388. break;
  389. case 'A': // upper case am/pm
  390. $output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
  391. break;
  392. case 'B': // swatch internet time
  393. $dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
  394. if ($gmt === true) {
  395. $dayseconds += 3600;
  396. }
  397. $output .= (int) (($dayseconds % 86400) / 86.4);
  398. break;
  399. case 'g': // hours without leading zeros, 12h format
  400. if ($date['hours'] > 12) {
  401. $hour = $date['hours'] - 12;
  402. } else {
  403. if ($date['hours'] == 0) {
  404. $hour = '12';
  405. } else {
  406. $hour = $date['hours'];
  407. }
  408. }
  409. $output .= $hour;
  410. break;
  411. case 'G': // hours without leading zeros, 24h format
  412. $output .= $date['hours'];
  413. break;
  414. case 'h': // hours with leading zeros, 12h format
  415. if ($date['hours'] > 12) {
  416. $hour = $date['hours'] - 12;
  417. } else {
  418. if ($date['hours'] == 0) {
  419. $hour = '12';
  420. } else {
  421. $hour = $date['hours'];
  422. }
  423. }
  424. $output .= (($hour < 10) ? '0'.$hour : $hour);
  425. break;
  426. case 'H': // hours with leading zeros, 24h format
  427. $output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
  428. break;
  429. case 'i': // minutes with leading zeros
  430. $output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
  431. break;
  432. case 's': // seconds with leading zeros
  433. $output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
  434. break;
  435. // timezone formats
  436. case 'e': // timezone identifier
  437. if ($gmt === true) {
  438. $output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
  439. $date['mon'], $date['mday'], 2000));
  440. } else {
  441. $output .= date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
  442. $date['mon'], $date['mday'], 2000));
  443. }
  444. break;
  445. case 'I': // daylight saving time or not
  446. if ($gmt === true) {
  447. $output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
  448. $date['mon'], $date['mday'], 2000));
  449. } else {
  450. $output .= date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
  451. $date['mon'], $date['mday'], 2000));
  452. }
  453. break;
  454. case 'O': // difference to GMT in hours
  455. $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
  456. $output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
  457. break;
  458. case 'P': // difference to GMT with colon
  459. $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
  460. $gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
  461. $output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
  462. break;
  463. case 'T': // timezone settings
  464. if ($gmt === true) {
  465. $output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
  466. $date['mon'], $date['mday'], 2000));
  467. } else {
  468. $output .= date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
  469. $date['mon'], $date['mday'], 2000));
  470. }
  471. break;
  472. case 'Z': // timezone offset in seconds
  473. $output .= ($gmt === true) ? 0 : -$this->getGmtOffset();
  474. break;
  475. // complete time formats
  476. case 'c': // ISO 8601 date format
  477. $difference = $this->getGmtOffset();
  478. $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
  479. $output .= $date['year'] . '-'
  480. . (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']) . '-'
  481. . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . 'T'
  482. . (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']) . ':'
  483. . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
  484. . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
  485. . $difference;
  486. break;
  487. case 'r': // RFC 2822 date format
  488. $difference = $this->getGmtOffset();
  489. $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
  490. $output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) . ', '
  491. . (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']) . ' '
  492. . date('M', mktime(0, 0, 0, $date['mon'], 2, 1971)) . ' '
  493. . $date['year'] . ' '
  494. . (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']) . ':'
  495. . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
  496. . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']) . ' '
  497. . $difference;
  498. break;
  499. case 'U': // Unix timestamp
  500. $output .= $origstamp;
  501. break;
  502. // special formats
  503. case "\\": // next letter to print with no format
  504. $i++;
  505. if ($i < $length) {
  506. $output .= $format[$i];
  507. }
  508. break;
  509. default: // letter is no format so add it direct
  510. $output .= $format[$i];
  511. break;
  512. }
  513. }
  514. return (string) $output;
  515. }
  516. /**
  517. * Returns the day of week for a Gregorian calendar date.
  518. * 0 = sunday, 6 = saturday
  519. *
  520. * @param integer $year
  521. * @param integer $month
  522. * @param integer $day
  523. * @return integer dayOfWeek
  524. */
  525. protected static function dayOfWeek($year, $month, $day)
  526. {
  527. if ((1901 < $year) and ($year < 2038)) {
  528. return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
  529. }
  530. // gregorian correction
  531. $correction = 0;
  532. if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
  533. $correction = 3;
  534. }
  535. if ($month > 2) {
  536. $month -= 2;
  537. } else {
  538. $month += 10;
  539. $year--;
  540. }
  541. $day = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
  542. $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
  543. return (int) ($day - 7 * floor($day / 7));
  544. }
  545. /**
  546. * Internal getDateParts function for handling 64bit timestamps, similar to:
  547. * http://www.php.net/getdate
  548. *
  549. * Returns an array of date parts for $timestamp, relative to 1970/01/01 00:00:00 GMT/UTC.
  550. *
  551. * $fast specifies ALL date parts should be returned (slower)
  552. * Default is false, and excludes $dayofweek, weekday, month and timestamp from parts returned.
  553. *
  554. * @param mixed $timestamp
  555. * @param boolean $fast OPTIONAL defaults to fast (false), resulting in fewer date parts
  556. * @return array
  557. */
  558. protected function getDateParts($timestamp = null, $fast = null)
  559. {
  560. // actual timestamp
  561. if (!is_numeric($timestamp)) {
  562. return getdate();
  563. }
  564. // 32bit timestamp
  565. if (abs($timestamp) <= 0x7FFFFFFF) {
  566. return @getdate((int) $timestamp);
  567. }
  568. if (isset(self::$_cache)) {
  569. $id = strtr('Zend_DateObject_getDateParts_' . $timestamp.'_'.(int)$fast, '-','_');
  570. if ($result = self::$_cache->load($id)) {
  571. return unserialize($result);
  572. }
  573. }
  574. $otimestamp = $timestamp;
  575. $numday = 0;
  576. $month = 0;
  577. // gregorian correction
  578. if ($timestamp < -12219321600) {
  579. $timestamp -= 864000;
  580. }
  581. // timestamp lower 0
  582. if ($timestamp < 0) {
  583. $sec = 0;
  584. $act = 1970;
  585. // iterate through 10 years table, increasing speed
  586. foreach(self::$_yearTable as $year => $seconds) {
  587. if ($timestamp >= $seconds) {
  588. $i = $act;
  589. break;
  590. }
  591. $sec = $seconds;
  592. $act = $year;
  593. }
  594. $timestamp -= $sec;
  595. if (!isset($i)) {
  596. $i = $act;
  597. }
  598. // iterate the max last 10 years
  599. do {
  600. --$i;
  601. $day = $timestamp;
  602. $timestamp += 31536000;
  603. $leapyear = self::isYearLeapYear($i);
  604. if ($leapyear === true) {
  605. $timestamp += 86400;
  606. }
  607. if ($timestamp >= 0) {
  608. $year = $i;
  609. break;
  610. }
  611. } while ($timestamp < 0);
  612. $secondsPerYear = 86400 * ($leapyear ? 366 : 365) + $day;
  613. $timestamp = $day;
  614. // iterate through months
  615. for ($i = 12; --$i >= 0;) {
  616. $day = $timestamp;
  617. $timestamp += self::$_monthTable[$i] * 86400;
  618. if (($leapyear === true) and ($i == 1)) {
  619. $timestamp += 86400;
  620. }
  621. if ($timestamp >= 0) {
  622. $month = $i;
  623. $numday = self::$_monthTable[$i];
  624. if (($leapyear === true) and ($i == 1)) {
  625. ++$numday;
  626. }
  627. break;
  628. }
  629. }
  630. $timestamp = $day;
  631. $numberdays = $numday + ceil(($timestamp + 1) / 86400);
  632. $timestamp += ($numday - $numberdays + 1) * 86400;
  633. $hours = floor($timestamp / 3600);
  634. } else {
  635. // iterate through years
  636. for ($i = 1970;;$i++) {
  637. $day = $timestamp;
  638. $timestamp -= 31536000;
  639. $leapyear = self::isYearLeapYear($i);
  640. if ($leapyear === true) {
  641. $timestamp -= 86400;
  642. }
  643. if ($timestamp < 0) {
  644. $year = $i;
  645. break;
  646. }
  647. }
  648. $secondsPerYear = $day;
  649. $timestamp = $day;
  650. // iterate through months
  651. for ($i = 0; $i <= 11; $i++) {
  652. $day = $timestamp;
  653. $timestamp -= self::$_monthTable[$i] * 86400;
  654. if (($leapyear === true) and ($i == 1)) {
  655. $timestamp -= 86400;
  656. }
  657. if ($timestamp < 0) {
  658. $month = $i;
  659. $numday = self::$_monthTable[$i];
  660. if (($leapyear === true) and ($i == 1)) {
  661. ++$numday;
  662. }
  663. break;
  664. }
  665. }
  666. $timestamp = $day;
  667. $numberdays = ceil(($timestamp + 1) / 86400);
  668. $timestamp = $timestamp - ($numberdays - 1) * 86400;
  669. $hours = floor($timestamp / 3600);
  670. }
  671. $timestamp -= $hours * 3600;
  672. $month += 1;
  673. $minutes = floor($timestamp / 60);
  674. $seconds = $timestamp - $minutes * 60;
  675. if ($fast === true) {
  676. $array = array(
  677. 'seconds' => $seconds,
  678. 'minutes' => $minutes,
  679. 'hours' => $hours,
  680. 'mday' => $numberdays,
  681. 'mon' => $month,
  682. 'year' => $year,
  683. 'yday' => floor($secondsPerYear / 86400),
  684. );
  685. } else {
  686. $dayofweek = self::dayOfWeek($year, $month, $numberdays);
  687. $array = array(
  688. 'seconds' => $seconds,
  689. 'minutes' => $minutes,
  690. 'hours' => $hours,
  691. 'mday' => $numberdays,
  692. 'wday' => $dayofweek,
  693. 'mon' => $month,
  694. 'year' => $year,
  695. 'yday' => floor($secondsPerYear / 86400),
  696. 'weekday' => gmdate('l', 86400 * (3 + $dayofweek)),
  697. 'month' => gmdate('F', mktime(0, 0, 0, $month, 1, 1971)),
  698. 0 => $otimestamp
  699. );
  700. }
  701. if (isset(self::$_cache)) {
  702. self::$_cache->save( serialize($array), $id);
  703. }
  704. return $array;
  705. }
  706. /**
  707. * Internal getWeekNumber function for handling 64bit timestamps
  708. *
  709. * Returns the ISO 8601 week number of a given date
  710. *
  711. * @param integer $year
  712. * @param integer $month
  713. * @param integer $day
  714. * @return integer
  715. */
  716. protected function weekNumber($year, $month, $day)
  717. {
  718. if ((1901 < $year) and ($year < 2038)) {
  719. return (int) date('W', mktime(0, 0, 0, $month, $day, $year));
  720. }
  721. $dayofweek = self::dayOfWeek($year, $month, $day);
  722. $firstday = self::dayOfWeek($year, 1, 1);
  723. if (($month == 1) and (($firstday < 1) or ($firstday > 4)) and ($day < 4)) {
  724. $firstday = self::dayOfWeek($year - 1, 1, 1);
  725. $month = 12;
  726. $day = 31;
  727. } else if (($month == 12) and ((self::dayOfWeek($year + 1, 1, 1) < 5) and
  728. (self::dayOfWeek($year + 1, 1, 1) > 0))) {
  729. return 1;
  730. }
  731. return intval (((self::dayOfWeek($year, 1, 1) < 5) and (self::dayOfWeek($year, 1, 1) > 0)) +
  732. 4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
  733. }
  734. /**
  735. * Internal _range function
  736. * Sets the value $a to be in the range of [0, $b]
  737. *
  738. * @param float $a - value to correct
  739. * @param float $b - maximum range to set
  740. */
  741. private function _range($a, $b) {
  742. while ($a < 0) {
  743. $a += $b;
  744. }
  745. while ($a >= $b) {
  746. $a -= $b;
  747. }
  748. return $a;
  749. }
  750. /**
  751. * Calculates the sunrise or sunset based on a location
  752. *
  753. * @param array $location Location for calculation MUST include 'latitude', 'longitude', 'horizon'
  754. * @param bool $horizon true: sunrise; false: sunset
  755. * @return mixed - false: midnight sun, integer:
  756. */
  757. protected function calcSun($location, $horizon, $rise = false)
  758. {
  759. // timestamp within 32bit
  760. if (abs($this->_unixTimestamp) <= 0x7FFFFFFF) {
  761. if ($rise === false) {
  762. return date_sunset($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
  763. $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
  764. }
  765. return date_sunrise($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
  766. $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
  767. }
  768. // self calculation - timestamp bigger than 32bit
  769. // fix circle values
  770. $quarterCircle = 0.5 * M_PI;
  771. $halfCircle = M_PI;
  772. $threeQuarterCircle = 1.5 * M_PI;
  773. $fullCircle = 2 * M_PI;
  774. // radiant conversion for coordinates
  775. $radLatitude = $location['latitude'] * $halfCircle / 180;
  776. $radLongitude = $location['longitude'] * $halfCircle / 180;
  777. // get solar coordinates
  778. $tmpRise = $rise ? $quarterCircle : $threeQuarterCircle;
  779. $radDay = $this->date('z',$this->_unixTimestamp) + ($tmpRise - $radLongitude) / $fullCircle;
  780. // solar anomoly and longitude
  781. $solAnomoly = $radDay * 0.017202 - 0.0574039;
  782. $solLongitude = $solAnomoly + 0.0334405 * sin($solAnomoly);
  783. $solLongitude += 4.93289 + 3.49066E-4 * sin(2 * $solAnomoly);
  784. // get quadrant
  785. $solLongitude = $this->_range($solLongitude, $fullCircle);
  786. if (($solLongitude / $quarterCircle) - intval($solLongitude / $quarterCircle) == 0) {
  787. $solLongitude += 4.84814E-6;
  788. }
  789. // solar ascension
  790. $solAscension = sin($solLongitude) / cos($solLongitude);
  791. $solAscension = atan2(0.91746 * $solAscension, 1);
  792. // adjust quadrant
  793. if ($solLongitude > $threeQuarterCircle) {
  794. $solAscension += $fullCircle;
  795. } else if ($solLongitude > $quarterCircle) {
  796. $solAscension += $halfCircle;
  797. }
  798. // solar declination
  799. $solDeclination = 0.39782 * sin($solLongitude);
  800. $solDeclination /= sqrt(-$solDeclination * $solDeclination + 1);
  801. $solDeclination = atan2($solDeclination, 1);
  802. $solHorizon = $horizon - sin($solDeclination) * sin($radLatitude);
  803. $solHorizon /= cos($solDeclination) * cos($radLatitude);
  804. // midnight sun, always night
  805. if (abs($solHorizon) > 1) {
  806. return false;
  807. }
  808. $solHorizon /= sqrt(-$solHorizon * $solHorizon + 1);
  809. $solHorizon = $quarterCircle - atan2($solHorizon, 1);
  810. if ($rise) {
  811. $solHorizon = $fullCircle - $solHorizon;
  812. }
  813. // time calculation
  814. $localTime = $solHorizon + $solAscension - 0.0172028 * $radDay - 1.73364;
  815. $universalTime = $localTime - $radLongitude;
  816. // determinate quadrant
  817. $universalTime = $this->_range($universalTime, $fullCircle);
  818. // radiant to hours
  819. $universalTime *= 24 / $fullCircle;
  820. // convert to time
  821. $hour = intval($universalTime);
  822. $universalTime = ($universalTime - $hour) * 60;
  823. $min = intval($universalTime);
  824. $universalTime = ($universalTime - $min) * 60;
  825. $sec = intval($universalTime);
  826. return $this->mktime($hour, $min, $sec, $this->date('m', $this->_unixTimestamp),
  827. $this->date('j', $this->_unixTimestamp), $this->date('Y', $this->_unixTimestamp),
  828. -1, true);
  829. }
  830. /**
  831. * Sets a new timezone for calculation of $this object's gmt offset.
  832. * For a list of supported timezones look here: http://php.net/timezones
  833. * If no timezone can be detected or the given timezone is wrong UTC will be set.
  834. *
  835. * @param string $zone OPTIONAL timezone for date calculation; defaults to date_default_timezone_get()
  836. * @return Zend_Date_DateObject Provides fluent interface
  837. * @throws Zend_Date_Exception
  838. */
  839. public function setTimezone($zone = null)
  840. {
  841. $oldzone = @date_default_timezone_get();
  842. if ($zone === null) {
  843. $zone = $oldzone;
  844. }
  845. // throw an error on false input, but only if the new date extension is available
  846. if (function_exists('timezone_open')) {
  847. if (!@timezone_open($zone)) {
  848. require_once 'Zend/Date/Exception.php';
  849. throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", $zone);
  850. }
  851. }
  852. // this can generate an error if the date extension is not available and a false timezone is given
  853. $result = @date_default_timezone_set($zone);
  854. if ($result === true) {
  855. $this->_offset = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
  856. $this->_timezone = $zone;
  857. }
  858. date_default_timezone_set($oldzone);
  859. if (($zone == 'UTC') or ($zone == 'GMT')) {
  860. $this->_dst = false;
  861. } else {
  862. $this->_dst = true;
  863. }
  864. return $this;
  865. }
  866. /**
  867. * Return the timezone of $this object.
  868. * The timezone is initially set when the object is instantiated.
  869. *
  870. * @return string actual set timezone string
  871. */
  872. public function getTimezone()
  873. {
  874. return $this->_timezone;
  875. }
  876. /**
  877. * Return the offset to GMT of $this object's timezone.
  878. * The offset to GMT is initially set when the object is instantiated using the currently,
  879. * in effect, default timezone for PHP functions.
  880. *
  881. * @return integer seconds difference between GMT timezone and timezone when object was instantiated
  882. */
  883. public function getGmtOffset()
  884. {
  885. $date = $this->getDateParts($this->getUnixTimestamp(), true);
  886. $zone = @date_default_timezone_get();
  887. $result = @date_default_timezone_set($this->_timezone);
  888. if ($result === true) {
  889. $offset = $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
  890. $date['mon'], $date['mday'], $date['year'], false)
  891. - $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
  892. $date['mon'], $date['mday'], $date['year'], true);
  893. }
  894. date_default_timezone_set($zone);
  895. return $offset;
  896. }
  897. }