DateObject.php 39 KB

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