Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Date/DateObject.php
Go to the documentation of this file.
00001 <?php
00029 abstract class Zend_Date_DateObject {
00030 
00034     private   $_unixTimestamp;
00035     protected static $_cache         = null;
00036     protected static $_defaultOffset = 0;
00037 
00041     private   $_timezone    = 'UTC';
00042     private   $_offset      = 0;
00043     private   $_syncronised = 0;
00044 
00045     // turn off DST correction if UTC or GMT
00046     protected $_dst         = true;
00047 
00051     private static $_monthTable = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
00052 
00056     private static $_yearTable = array(
00057         1970 => 0,            1960 => -315619200,   1950 => -631152000,
00058         1940 => -946771200,   1930 => -1262304000,  1920 => -1577923200,
00059         1910 => -1893456000,  1900 => -2208988800,  1890 => -2524521600,
00060         1880 => -2840140800,  1870 => -3155673600,  1860 => -3471292800,
00061         1850 => -3786825600,  1840 => -4102444800,  1830 => -4417977600,
00062         1820 => -4733596800,  1810 => -5049129600,  1800 => -5364662400,
00063         1790 => -5680195200,  1780 => -5995814400,  1770 => -6311347200,
00064         1760 => -6626966400,  1750 => -6942499200,  1740 => -7258118400,
00065         1730 => -7573651200,  1720 => -7889270400,  1710 => -8204803200,
00066         1700 => -8520336000,  1690 => -8835868800,  1680 => -9151488000,
00067         1670 => -9467020800,  1660 => -9782640000,  1650 => -10098172800,
00068         1640 => -10413792000, 1630 => -10729324800, 1620 => -11044944000,
00069         1610 => -11360476800, 1600 => -11676096000);
00070 
00078     protected function setUnixTimestamp($timestamp = null)
00079     {
00080         $old = $this->_unixTimestamp;
00081 
00082         if (is_numeric($timestamp)) {
00083             $this->_unixTimestamp = $timestamp;
00084         } else if ($timestamp === null) {
00085             $this->_unixTimestamp = time();
00086         } else {
00087             require_once 'Zend/Date/Exception.php';
00088             throw new Zend_Date_Exception('\'' . $timestamp . '\' is not a valid UNIX timestamp', 0, null, $timestamp);
00089         }
00090 
00091         return $old;
00092     }
00093 
00101     protected function getUnixTimestamp()
00102     {
00103         if ($this->_unixTimestamp === intval($this->_unixTimestamp)) {
00104             return (int) $this->_unixTimestamp;
00105         } else {
00106             return (string) $this->_unixTimestamp;
00107         }
00108     }
00109 
00118     protected function _getTime($sync = null)
00119     {
00120         if ($sync !== null) {
00121             $this->_syncronised = round($sync);
00122         }
00123         return (time() + $this->_syncronised);
00124     }
00125 
00145     protected function mktime($hour, $minute, $second, $month, $day, $year, $gmt = false)
00146     {
00147         // complete date but in 32bit timestamp - use PHP internal
00148         if ((1901 < $year) and ($year < 2038)) {
00149 
00150             $oldzone = @date_default_timezone_get();
00151             // Timezone also includes DST settings, therefor substracting the GMT offset is not enough
00152             // We have to set the correct timezone to get the right value
00153             if (($this->_timezone != $oldzone) and ($gmt === false)) {
00154                 date_default_timezone_set($this->_timezone);
00155             }
00156             $result = ($gmt) ? @gmmktime($hour, $minute, $second, $month, $day, $year)
00157                              :   @mktime($hour, $minute, $second, $month, $day, $year);
00158             date_default_timezone_set($oldzone);
00159 
00160             return $result;
00161         }
00162 
00163         if ($gmt !== true) {
00164             $second += $this->_offset;
00165         }
00166 
00167         if (isset(self::$_cache)) {
00168             $id = strtr('Zend_DateObject_mkTime_' . $this->_offset . '_' . $year.$month.$day.'_'.$hour.$minute.$second . '_'.(int)$gmt, '-','_');
00169             if ($result = self::$_cache->load($id)) {
00170                 return unserialize($result);
00171             }
00172         }
00173 
00174         // date to integer
00175         $day   = intval($day);
00176         $month = intval($month);
00177         $year  = intval($year);
00178 
00179         // correct months > 12 and months < 1
00180         if ($month > 12) {
00181             $overlap = floor($month / 12);
00182             $year   += $overlap;
00183             $month  -= $overlap * 12;
00184         } else {
00185             $overlap = ceil((1 - $month) / 12);
00186             $year   -= $overlap;
00187             $month  += $overlap * 12;
00188         }
00189 
00190         $date = 0;
00191         if ($year >= 1970) {
00192 
00193             // Date is after UNIX epoch
00194             // go through leapyears
00195             // add months from latest given year
00196             for ($count = 1970; $count <= $year; $count++) {
00197 
00198                 $leapyear = self::isYearLeapYear($count);
00199                 if ($count < $year) {
00200 
00201                     $date += 365;
00202                     if ($leapyear === true) {
00203                         $date++;
00204                     }
00205 
00206                 } else {
00207 
00208                     for ($mcount = 0; $mcount < ($month - 1); $mcount++) {
00209                         $date += self::$_monthTable[$mcount];
00210                         if (($leapyear === true) and ($mcount == 1)) {
00211                             $date++;
00212                         }
00213 
00214                     }
00215                 }
00216             }
00217 
00218             $date += $day - 1;
00219             $date = (($date * 86400) + ($hour * 3600) + ($minute * 60) + $second);
00220         } else {
00221 
00222             // Date is before UNIX epoch
00223             // go through leapyears
00224             // add months from latest given year
00225             for ($count = 1969; $count >= $year; $count--) {
00226 
00227                 $leapyear = self::isYearLeapYear($count);
00228                 if ($count > $year)
00229                 {
00230                     $date += 365;
00231                     if ($leapyear === true)
00232                         $date++;
00233                 } else {
00234 
00235                     for ($mcount = 11; $mcount > ($month - 1); $mcount--) {
00236                         $date += self::$_monthTable[$mcount];
00237                         if (($leapyear === true) and ($mcount == 2)) {
00238                             $date++;
00239                         }
00240 
00241                     }
00242                 }
00243             }
00244 
00245             $date += (self::$_monthTable[$month - 1] - $day);
00246             $date = -(($date * 86400) + (86400 - (($hour * 3600) + ($minute * 60) + $second)));
00247 
00248             // gregorian correction for 5.Oct.1582
00249             if ($date < -12220185600) {
00250                 $date += 864000;
00251             } else if ($date < -12219321600) {
00252                 $date  = -12219321600;
00253             }
00254         }
00255 
00256         if (isset(self::$_cache)) {
00257             self::$_cache->save( serialize($date), $id);
00258         }
00259 
00260         return $date;
00261     }
00262 
00269     protected static function isYearLeapYear($year)
00270     {
00271         // all leapyears can be divided through 4
00272         if (($year % 4) != 0) {
00273             return false;
00274         }
00275 
00276         // all leapyears can be divided through 400
00277         if ($year % 400 == 0) {
00278             return true;
00279         } else if (($year > 1582) and ($year % 100 == 0)) {
00280             return false;
00281         }
00282 
00283         return true;
00284     }
00285 
00296     protected function date($format, $timestamp = null, $gmt = false)
00297     {
00298         $oldzone = @date_default_timezone_get();
00299         if ($this->_timezone != $oldzone) {
00300             date_default_timezone_set($this->_timezone);
00301         }
00302 
00303         if ($timestamp === null) {
00304             $result = ($gmt) ? @gmdate($format) : @date($format);
00305             date_default_timezone_set($oldzone);
00306             return $result;
00307         }
00308 
00309         if (abs($timestamp) <= 0x7FFFFFFF) {
00310             $result = ($gmt) ? @gmdate($format, $timestamp) : @date($format, $timestamp);
00311             date_default_timezone_set($oldzone);
00312             return $result;
00313         }
00314 
00315         $jump      = false;
00316         $origstamp = $timestamp;
00317         if (isset(self::$_cache)) {
00318             $idstamp = strtr('Zend_DateObject_date_' . $this->_offset . '_'. $timestamp . '_'.(int)$gmt, '-','_');
00319             if ($result2 = self::$_cache->load($idstamp)) {
00320                 $timestamp = unserialize($result2);
00321                 $jump = true;
00322             }
00323         }
00324 
00325         // check on false or null alone fails
00326         if (empty($gmt) and empty($jump)) {
00327             $tempstamp = $timestamp;
00328             if ($tempstamp > 0) {
00329                 while (abs($tempstamp) > 0x7FFFFFFF) {
00330                     $tempstamp -= (86400 * 23376);
00331                 }
00332 
00333                 $dst = date("I", $tempstamp);
00334                 if ($dst === 1) {
00335                     $timestamp += 3600;
00336                 }
00337 
00338                 $temp       = date('Z', $tempstamp);
00339                 $timestamp += $temp;
00340             }
00341 
00342             if (isset(self::$_cache)) {
00343                 self::$_cache->save( serialize($timestamp), $idstamp);
00344             }
00345         }
00346 
00347         if (($timestamp < 0) and ($gmt !== true)) {
00348             $timestamp -= $this->_offset;
00349         }
00350 
00351         date_default_timezone_set($oldzone);
00352         $date   = $this->getDateParts($timestamp, true);
00353         $length = strlen($format);
00354         $output = '';
00355 
00356         for ($i = 0; $i < $length; $i++) {
00357             switch($format[$i]) {
00358                 // day formats
00359                 case 'd':  // day of month, 2 digits, with leading zero, 01 - 31
00360                     $output .= (($date['mday'] < 10) ? '0' . $date['mday'] : $date['mday']);
00361                     break;
00362 
00363                 case 'D':  // day of week, 3 letters, Mon - Sun
00364                     $output .= date('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
00365                     break;
00366 
00367                 case 'j':  // day of month, without leading zero, 1 - 31
00368                     $output .= $date['mday'];
00369                     break;
00370 
00371                 case 'l':  // day of week, full string name, Sunday - Saturday
00372                     $output .= date('l', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday'])));
00373                     break;
00374 
00375                 case 'N':  // ISO 8601 numeric day of week, 1 - 7
00376                     $day = self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
00377                     if ($day == 0) {
00378                         $day = 7;
00379                     }
00380                     $output .= $day;
00381                     break;
00382 
00383                 case 'S':  // english suffix for day of month, st nd rd th
00384                     if (($date['mday'] % 10) == 1) {
00385                         $output .= 'st';
00386                     } else if ((($date['mday'] % 10) == 2) and ($date['mday'] != 12)) {
00387                         $output .= 'nd';
00388                     } else if (($date['mday'] % 10) == 3) {
00389                         $output .= 'rd';
00390                     } else {
00391                         $output .= 'th';
00392                     }
00393                     break;
00394 
00395                 case 'w':  // numeric day of week, 0 - 6
00396                     $output .= self::dayOfWeek($date['year'], $date['mon'], $date['mday']);
00397                     break;
00398 
00399                 case 'z':  // day of year, 0 - 365
00400                     $output .= $date['yday'];
00401                     break;
00402 
00403 
00404                 // week formats
00405                 case 'W':  // ISO 8601, week number of year
00406                     $output .= $this->weekNumber($date['year'], $date['mon'], $date['mday']);
00407                     break;
00408 
00409 
00410                 // month formats
00411                 case 'F':  // string month name, january - december
00412                     $output .= date('F', mktime(0, 0, 0, $date['mon'], 2, 1971));
00413                     break;
00414 
00415                 case 'm':  // number of month, with leading zeros, 01 - 12
00416                     $output .= (($date['mon'] < 10) ? '0' . $date['mon'] : $date['mon']);
00417                     break;
00418 
00419                 case 'M':  // 3 letter month name, Jan - Dec
00420                     $output .= date('M',mktime(0, 0, 0, $date['mon'], 2, 1971));
00421                     break;
00422 
00423                 case 'n':  // number of month, without leading zeros, 1 - 12
00424                     $output .= $date['mon'];
00425                     break;
00426 
00427                 case 't':  // number of day in month
00428                     $output .= self::$_monthTable[$date['mon'] - 1];
00429                     break;
00430 
00431 
00432                 // year formats
00433                 case 'L':  // is leap year ?
00434                     $output .= (self::isYearLeapYear($date['year'])) ? '1' : '0';
00435                     break;
00436 
00437                 case 'o':  // ISO 8601 year number
00438                     $week = $this->weekNumber($date['year'], $date['mon'], $date['mday']);
00439                     if (($week > 50) and ($date['mon'] == 1)) {
00440                         $output .= ($date['year'] - 1);
00441                     } else {
00442                         $output .= $date['year'];
00443                     }
00444                     break;
00445 
00446                 case 'Y':  // year number, 4 digits
00447                     $output .= $date['year'];
00448                     break;
00449 
00450                 case 'y':  // year number, 2 digits
00451                     $output .= substr($date['year'], strlen($date['year']) - 2, 2);
00452                     break;
00453 
00454 
00455                 // time formats
00456                 case 'a':  // lower case am/pm
00457                     $output .= (($date['hours'] >= 12) ? 'pm' : 'am');
00458                     break;
00459 
00460                 case 'A':  // upper case am/pm
00461                     $output .= (($date['hours'] >= 12) ? 'PM' : 'AM');
00462                     break;
00463 
00464                 case 'B':  // swatch internet time
00465                     $dayseconds = ($date['hours'] * 3600) + ($date['minutes'] * 60) + $date['seconds'];
00466                     if ($gmt === true) {
00467                         $dayseconds += 3600;
00468                     }
00469                     $output .= (int) (($dayseconds % 86400) / 86.4);
00470                     break;
00471 
00472                 case 'g':  // hours without leading zeros, 12h format
00473                     if ($date['hours'] > 12) {
00474                         $hour = $date['hours'] - 12;
00475                     } else {
00476                         if ($date['hours'] == 0) {
00477                             $hour = '12';
00478                         } else {
00479                             $hour = $date['hours'];
00480                         }
00481                     }
00482                     $output .= $hour;
00483                     break;
00484 
00485                 case 'G':  // hours without leading zeros, 24h format
00486                     $output .= $date['hours'];
00487                     break;
00488 
00489                 case 'h':  // hours with leading zeros, 12h format
00490                     if ($date['hours'] > 12) {
00491                         $hour = $date['hours'] - 12;
00492                     } else {
00493                         if ($date['hours'] == 0) {
00494                             $hour = '12';
00495                         } else {
00496                             $hour = $date['hours'];
00497                         }
00498                     }
00499                     $output .= (($hour < 10) ? '0'.$hour : $hour);
00500                     break;
00501 
00502                 case 'H':  // hours with leading zeros, 24h format
00503                     $output .= (($date['hours'] < 10) ? '0' . $date['hours'] : $date['hours']);
00504                     break;
00505 
00506                 case 'i':  // minutes with leading zeros
00507                     $output .= (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']);
00508                     break;
00509 
00510                 case 's':  // seconds with leading zeros
00511                     $output .= (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']);
00512                     break;
00513 
00514 
00515                 // timezone formats
00516                 case 'e':  // timezone identifier
00517                     if ($gmt === true) {
00518                         $output .= gmdate('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
00519                                                       $date['mon'], $date['mday'], 2000));
00520                     } else {
00521                         $output .=   date('e', mktime($date['hours'], $date['minutes'], $date['seconds'],
00522                                                       $date['mon'], $date['mday'], 2000));
00523                     }
00524                     break;
00525 
00526                 case 'I':  // daylight saving time or not
00527                     if ($gmt === true) {
00528                         $output .= gmdate('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
00529                                                       $date['mon'], $date['mday'], 2000));
00530                     } else {
00531                         $output .=   date('I', mktime($date['hours'], $date['minutes'], $date['seconds'],
00532                                                       $date['mon'], $date['mday'], 2000));
00533                     }
00534                     break;
00535 
00536                 case 'O':  // difference to GMT in hours
00537                     $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
00538                     $output .= sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
00539                     break;
00540 
00541                 case 'P':  // difference to GMT with colon
00542                     $gmtstr = ($gmt === true) ? 0 : $this->getGmtOffset();
00543                     $gmtstr = sprintf('%s%04d', ($gmtstr <= 0) ? '+' : '-', abs($gmtstr) / 36);
00544                     $output = $output . substr($gmtstr, 0, 3) . ':' . substr($gmtstr, 3);
00545                     break;
00546 
00547                 case 'T':  // timezone settings
00548                     if ($gmt === true) {
00549                         $output .= gmdate('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
00550                                                       $date['mon'], $date['mday'], 2000));
00551                     } else {
00552                         $output .=   date('T', mktime($date['hours'], $date['minutes'], $date['seconds'],
00553                                                       $date['mon'], $date['mday'], 2000));
00554                     }
00555                     break;
00556 
00557                 case 'Z':  // timezone offset in seconds
00558                     $output .= ($gmt === true) ? 0 : -$this->getGmtOffset();
00559                     break;
00560 
00561 
00562                 // complete time formats
00563                 case 'c':  // ISO 8601 date format
00564                     $difference = $this->getGmtOffset();
00565                     $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
00566                     $difference = substr($difference, 0, 3) . ':' . substr($difference, 3);
00567                     $output .= $date['year'] . '-'
00568                              . (($date['mon']     < 10) ? '0' . $date['mon']     : $date['mon'])     . '-'
00569                              . (($date['mday']    < 10) ? '0' . $date['mday']    : $date['mday'])    . 'T'
00570                              . (($date['hours']   < 10) ? '0' . $date['hours']   : $date['hours'])   . ':'
00571                              . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
00572                              . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds'])
00573                              . $difference;
00574                     break;
00575 
00576                 case 'r':  // RFC 2822 date format
00577                     $difference = $this->getGmtOffset();
00578                     $difference = sprintf('%s%04d', ($difference <= 0) ? '+' : '-', abs($difference) / 36);
00579                     $output .= gmdate('D', 86400 * (3 + self::dayOfWeek($date['year'], $date['mon'], $date['mday']))) . ', '
00580                              . (($date['mday']    < 10) ? '0' . $date['mday']    : $date['mday'])    . ' '
00581                              . date('M', mktime(0, 0, 0, $date['mon'], 2, 1971)) . ' '
00582                              . $date['year'] . ' '
00583                              . (($date['hours']   < 10) ? '0' . $date['hours']   : $date['hours'])   . ':'
00584                              . (($date['minutes'] < 10) ? '0' . $date['minutes'] : $date['minutes']) . ':'
00585                              . (($date['seconds'] < 10) ? '0' . $date['seconds'] : $date['seconds']) . ' '
00586                              . $difference;
00587                     break;
00588 
00589                 case 'U':  // Unix timestamp
00590                     $output .= $origstamp;
00591                     break;
00592 
00593 
00594                 // special formats
00595                 case "\\":  // next letter to print with no format
00596                     $i++;
00597                     if ($i < $length) {
00598                         $output .= $format[$i];
00599                     }
00600                     break;
00601 
00602                 default:  // letter is no format so add it direct
00603                     $output .= $format[$i];
00604                     break;
00605             }
00606         }
00607 
00608         return (string) $output;
00609     }
00610 
00620     protected static function dayOfWeek($year, $month, $day)
00621     {
00622         if ((1901 < $year) and ($year < 2038)) {
00623             return (int) date('w', mktime(0, 0, 0, $month, $day, $year));
00624         }
00625 
00626         // gregorian correction
00627         $correction = 0;
00628         if (($year < 1582) or (($year == 1582) and (($month < 10) or (($month == 10) && ($day < 15))))) {
00629             $correction = 3;
00630         }
00631 
00632         if ($month > 2) {
00633             $month -= 2;
00634         } else {
00635             $month += 10;
00636             $year--;
00637         }
00638 
00639         $day  = floor((13 * $month - 1) / 5) + $day + ($year % 100) + floor(($year % 100) / 4);
00640         $day += floor(($year / 100) / 4) - 2 * floor($year / 100) + 77 + $correction;
00641 
00642         return (int) ($day - 7 * floor($day / 7));
00643     }
00644 
00658     protected function getDateParts($timestamp = null, $fast = null)
00659     {
00660 
00661         // actual timestamp
00662         if (!is_numeric($timestamp)) {
00663             return getdate();
00664         }
00665 
00666         // 32bit timestamp
00667         if (abs($timestamp) <= 0x7FFFFFFF) {
00668             return @getdate((int) $timestamp);
00669         }
00670 
00671         if (isset(self::$_cache)) {
00672             $id = strtr('Zend_DateObject_getDateParts_' . $timestamp.'_'.(int)$fast, '-','_');
00673             if ($result = self::$_cache->load($id)) {
00674                 return unserialize($result);
00675             }
00676         }
00677 
00678         $otimestamp = $timestamp;
00679         $numday = 0;
00680         $month = 0;
00681         // gregorian correction
00682         if ($timestamp < -12219321600) {
00683             $timestamp -= 864000;
00684         }
00685 
00686         // timestamp lower 0
00687         if ($timestamp < 0) {
00688             $sec = 0;
00689             $act = 1970;
00690 
00691             // iterate through 10 years table, increasing speed
00692             foreach(self::$_yearTable as $year => $seconds) {
00693                 if ($timestamp >= $seconds) {
00694                     $i = $act;
00695                     break;
00696                 }
00697                 $sec = $seconds;
00698                 $act = $year;
00699             }
00700 
00701             $timestamp -= $sec;
00702             if (!isset($i)) {
00703                 $i = $act;
00704             }
00705 
00706             // iterate the max last 10 years
00707             do {
00708                 --$i;
00709                 $day = $timestamp;
00710 
00711                 $timestamp += 31536000;
00712                 $leapyear = self::isYearLeapYear($i);
00713                 if ($leapyear === true) {
00714                     $timestamp += 86400;
00715                 }
00716 
00717                 if ($timestamp >= 0) {
00718                     $year = $i;
00719                     break;
00720                 }
00721             } while ($timestamp < 0);
00722 
00723             $secondsPerYear = 86400 * ($leapyear ? 366 : 365) + $day;
00724 
00725             $timestamp = $day;
00726             // iterate through months
00727             for ($i = 12; --$i >= 0;) {
00728                 $day = $timestamp;
00729 
00730                 $timestamp += self::$_monthTable[$i] * 86400;
00731                 if (($leapyear === true) and ($i == 1)) {
00732                     $timestamp += 86400;
00733                 }
00734 
00735                 if ($timestamp >= 0) {
00736                     $month  = $i;
00737                     $numday = self::$_monthTable[$i];
00738                     if (($leapyear === true) and ($i == 1)) {
00739                         ++$numday;
00740                     }
00741                     break;
00742                 }
00743             }
00744 
00745             $timestamp  = $day;
00746             $numberdays = $numday + ceil(($timestamp + 1) / 86400);
00747 
00748             $timestamp += ($numday - $numberdays + 1) * 86400;
00749             $hours      = floor($timestamp / 3600);
00750         } else {
00751 
00752             // iterate through years
00753             for ($i = 1970;;$i++) {
00754                 $day = $timestamp;
00755 
00756                 $timestamp -= 31536000;
00757                 $leapyear = self::isYearLeapYear($i);
00758                 if ($leapyear === true) {
00759                     $timestamp -= 86400;
00760                 }
00761 
00762                 if ($timestamp < 0) {
00763                     $year = $i;
00764                     break;
00765                 }
00766             }
00767 
00768             $secondsPerYear = $day;
00769 
00770             $timestamp = $day;
00771             // iterate through months
00772             for ($i = 0; $i <= 11; $i++) {
00773                 $day = $timestamp;
00774                 $timestamp -= self::$_monthTable[$i] * 86400;
00775 
00776                 if (($leapyear === true) and ($i == 1)) {
00777                     $timestamp -= 86400;
00778                 }
00779 
00780                 if ($timestamp < 0) {
00781                     $month  = $i;
00782                     $numday = self::$_monthTable[$i];
00783                     if (($leapyear === true) and ($i == 1)) {
00784                         ++$numday;
00785                     }
00786                     break;
00787                 }
00788             }
00789 
00790             $timestamp  = $day;
00791             $numberdays = ceil(($timestamp + 1) / 86400);
00792             $timestamp  = $timestamp - ($numberdays - 1) * 86400;
00793             $hours = floor($timestamp / 3600);
00794         }
00795 
00796         $timestamp -= $hours * 3600;
00797 
00798         $month  += 1;
00799         $minutes = floor($timestamp / 60);
00800         $seconds = $timestamp - $minutes * 60;
00801 
00802         if ($fast === true) {
00803             $array = array(
00804                 'seconds' => $seconds,
00805                 'minutes' => $minutes,
00806                 'hours'   => $hours,
00807                 'mday'    => $numberdays,
00808                 'mon'     => $month,
00809                 'year'    => $year,
00810                 'yday'    => floor($secondsPerYear / 86400),
00811             );
00812         } else {
00813 
00814             $dayofweek = self::dayOfWeek($year, $month, $numberdays);
00815             $array = array(
00816                     'seconds' => $seconds,
00817                     'minutes' => $minutes,
00818                     'hours'   => $hours,
00819                     'mday'    => $numberdays,
00820                     'wday'    => $dayofweek,
00821                     'mon'     => $month,
00822                     'year'    => $year,
00823                     'yday'    => floor($secondsPerYear / 86400),
00824                     'weekday' => gmdate('l', 86400 * (3 + $dayofweek)),
00825                     'month'   => gmdate('F', mktime(0, 0, 0, $month, 1, 1971)),
00826                     0         => $otimestamp
00827             );
00828         }
00829 
00830         if (isset(self::$_cache)) {
00831             self::$_cache->save( serialize($array), $id);
00832         }
00833 
00834         return $array;
00835     }
00836 
00847     protected function weekNumber($year, $month, $day)
00848     {
00849         if ((1901 < $year) and ($year < 2038)) {
00850             return (int) date('W', mktime(0, 0, 0, $month, $day, $year));
00851         }
00852 
00853         $dayofweek = self::dayOfWeek($year, $month, $day);
00854         $firstday  = self::dayOfWeek($year, 1, 1);
00855         if (($month == 1) and (($firstday < 1) or ($firstday > 4)) and ($day < 4)) {
00856             $firstday  = self::dayOfWeek($year - 1, 1, 1);
00857             $month     = 12;
00858             $day       = 31;
00859 
00860         } else if (($month == 12) and ((self::dayOfWeek($year + 1, 1, 1) < 5) and
00861                    (self::dayOfWeek($year + 1, 1, 1) > 0))) {
00862             return 1;
00863         }
00864 
00865         return intval (((self::dayOfWeek($year, 1, 1) < 5) and (self::dayOfWeek($year, 1, 1) > 0)) +
00866                4 * ($month - 1) + (2 * ($month - 1) + ($day - 1) + $firstday - $dayofweek + 6) * 36 / 256);
00867     }
00868 
00876     private function _range($a, $b) {
00877         while ($a < 0) {
00878             $a += $b;
00879         }
00880         while ($a >= $b) {
00881             $a -= $b;
00882         }
00883         return $a;
00884     }
00885 
00893     protected function calcSun($location, $horizon, $rise = false)
00894     {
00895         // timestamp within 32bit
00896         if (abs($this->_unixTimestamp) <= 0x7FFFFFFF) {
00897             if ($rise === false) {
00898                 return date_sunset($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
00899                                    $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
00900             }
00901             return date_sunrise($this->_unixTimestamp, SUNFUNCS_RET_TIMESTAMP, $location['latitude'],
00902                                 $location['longitude'], 90 + $horizon, $this->getGmtOffset() / 3600);
00903         }
00904 
00905         // self calculation - timestamp bigger than 32bit
00906         // fix circle values
00907         $quarterCircle      = 0.5 * M_PI;
00908         $halfCircle         =       M_PI;
00909         $threeQuarterCircle = 1.5 * M_PI;
00910         $fullCircle         = 2   * M_PI;
00911 
00912         // radiant conversion for coordinates
00913         $radLatitude  = $location['latitude']   * $halfCircle / 180;
00914         $radLongitude = $location['longitude']  * $halfCircle / 180;
00915 
00916         // get solar coordinates
00917         $tmpRise       = $rise ? $quarterCircle : $threeQuarterCircle;
00918         $radDay        = $this->date('z',$this->_unixTimestamp) + ($tmpRise - $radLongitude) / $fullCircle;
00919 
00920         // solar anomoly and longitude
00921         $solAnomoly    = $radDay * 0.017202 - 0.0574039;
00922         $solLongitude  = $solAnomoly + 0.0334405 * sin($solAnomoly);
00923         $solLongitude += 4.93289 + 3.49066E-4 * sin(2 * $solAnomoly);
00924 
00925         // get quadrant
00926         $solLongitude = $this->_range($solLongitude, $fullCircle);
00927 
00928         if (($solLongitude / $quarterCircle) - intval($solLongitude / $quarterCircle) == 0) {
00929             $solLongitude += 4.84814E-6;
00930         }
00931 
00932         // solar ascension
00933         $solAscension = sin($solLongitude) / cos($solLongitude);
00934         $solAscension = atan2(0.91746 * $solAscension, 1);
00935 
00936         // adjust quadrant
00937         if ($solLongitude > $threeQuarterCircle) {
00938             $solAscension += $fullCircle;
00939         } else if ($solLongitude > $quarterCircle) {
00940             $solAscension += $halfCircle;
00941         }
00942 
00943         // solar declination
00944         $solDeclination  = 0.39782 * sin($solLongitude);
00945         $solDeclination /=  sqrt(-$solDeclination * $solDeclination + 1);
00946         $solDeclination  = atan2($solDeclination, 1);
00947 
00948         $solHorizon = $horizon - sin($solDeclination) * sin($radLatitude);
00949         $solHorizon /= cos($solDeclination) * cos($radLatitude);
00950 
00951         // midnight sun, always night
00952         if (abs($solHorizon) > 1) {
00953             return false;
00954         }
00955 
00956         $solHorizon /= sqrt(-$solHorizon * $solHorizon + 1);
00957         $solHorizon  = $quarterCircle - atan2($solHorizon, 1);
00958 
00959         if ($rise) {
00960             $solHorizon = $fullCircle - $solHorizon;
00961         }
00962 
00963         // time calculation
00964         $localTime     = $solHorizon + $solAscension - 0.0172028 * $radDay - 1.73364;
00965         $universalTime = $localTime - $radLongitude;
00966 
00967         // determinate quadrant
00968         $universalTime = $this->_range($universalTime, $fullCircle);
00969 
00970         // radiant to hours
00971         $universalTime *= 24 / $fullCircle;
00972 
00973         // convert to time
00974         $hour = intval($universalTime);
00975         $universalTime    = ($universalTime - $hour) * 60;
00976         $min  = intval($universalTime);
00977         $universalTime    = ($universalTime - $min) * 60;
00978         $sec  = intval($universalTime);
00979 
00980         return $this->mktime($hour, $min, $sec, $this->date('m', $this->_unixTimestamp),
00981                              $this->date('j', $this->_unixTimestamp), $this->date('Y', $this->_unixTimestamp),
00982                              -1, true);
00983     }
00984 
00994     public function setTimezone($zone = null)
00995     {
00996         $oldzone = @date_default_timezone_get();
00997         if ($zone === null) {
00998             $zone = $oldzone;
00999         }
01000 
01001         // throw an error on false input, but only if the new date extension is available
01002         if (function_exists('timezone_open')) {
01003             if (!@timezone_open($zone)) {
01004                 require_once 'Zend/Date/Exception.php';
01005                 throw new Zend_Date_Exception("timezone ($zone) is not a known timezone", 0, null, $zone);
01006             }
01007         }
01008         // this can generate an error if the date extension is not available and a false timezone is given
01009         $result = @date_default_timezone_set($zone);
01010         if ($result === true) {
01011             $this->_offset   = mktime(0, 0, 0, 1, 2, 1970) - gmmktime(0, 0, 0, 1, 2, 1970);
01012             $this->_timezone = $zone;
01013         }
01014         date_default_timezone_set($oldzone);
01015 
01016         if (($zone == 'UTC') or ($zone == 'GMT')) {
01017             $this->_dst = false;
01018         } else {
01019             $this->_dst = true;
01020         }
01021 
01022         return $this;
01023     }
01024 
01031     public function getTimezone()
01032     {
01033         return $this->_timezone;
01034     }
01035 
01043     public function getGmtOffset()
01044     {
01045         $date   = $this->getDateParts($this->getUnixTimestamp(), true);
01046         $zone   = @date_default_timezone_get();
01047         $result = @date_default_timezone_set($this->_timezone);
01048         if ($result === true) {
01049             $offset = $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
01050                                     $date['mon'], $date['mday'], $date['year'], false)
01051                     - $this->mktime($date['hours'], $date['minutes'], $date['seconds'],
01052                                     $date['mon'], $date['mday'], $date['year'], true);
01053         }
01054         date_default_timezone_set($zone);
01055 
01056         return $offset;
01057     }
01058 }
 All Data Structures Namespaces Files Functions Variables Enumerations