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