home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / php_sunrise_sunset.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  6.7 KB  |  209 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexander Wirtz <alex@pc4p.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: php_sunrise_sunset.php,v 1.2 2004/07/11 21:40:47 eru Exp $
  20.  
  21. // The sun position algorithm taken from the 'US Naval Observatory's
  22. // Almanac for Computers', implemented by Ken Bloom <kekabloom@ucdavis.edu>
  23. // for the zmanim project <http://sourceforge.net/projects/zmanim/>
  24. // and finally converted to C by Moshe Doron <mosdoron@netvision.net.il>.
  25. // 
  26. // Taken from the PHP5 sources and converted to PHP by above authors.
  27.  
  28.  
  29. if(!defined("SUNFUNCS_RET_TIMESTAMP")) {
  30.     define("SUNFUNCS_RET_TIMESTAMP", 1);
  31.     define("SUNFUNCS_RET_STRING",    2);
  32.     define("SUNFUNCS_RET_DOUBLE",    3);
  33. }
  34. define("SUNFUNCS_DEFAULT_LATITUDE",  31.7667);
  35. define("SUNFUNCS_DEFAULT_LONGITUDE", 35.2333);
  36. define("SUNFUNCS_SUNRISE_ZENITH",    90.83);
  37. define("SUNFUNCS_SUNSET_ZENITH",     90.83);
  38.  
  39. function php_sunrise_sunset($N, $latitude, $longitude, $zenith, $calc_sunset)
  40. {
  41.     // step 1: First calculate the day of the year
  42.     // int N = theday - date(1, 1, theday.year()) + 1;
  43.  
  44.     // step 2: convert the longitude to hour value and calculate an approximate time
  45.     $lngHour = $longitude / 15;
  46.  
  47.     // use 18 for sunset instead of 6
  48.     if ($calc_sunset) {
  49.         // Sunset
  50.         $t = $N + ((18 - $lngHour) / 24);
  51.     } else {
  52.         // Sunrise
  53.         $t = $N + ((6 - $lngHour) / 24);
  54.     } 
  55.  
  56.     // step 3: calculate the sun's mean anomaly
  57.     $M = (0.9856 * $t) - 3.289;
  58.  
  59.     // step 4: calculate the sun's true longitude
  60.     $L = $M + (1.916 * sin(deg2rad($M))) + (0.020 * sin(deg2rad(2 * $M))) + 282.634;
  61.  
  62.     while ($L < 0) {
  63.         $Lx = $L + 360;
  64.         assert($Lx != $L); // askingtheguru: really needed?
  65.         $L = $Lx;
  66.     }
  67.     
  68.     while ($L >= 360) {
  69.         $Lx = $L - 360;
  70.         assert($Lx != $L); // askingtheguru: really needed?
  71.         $L = $Lx;
  72.     }
  73.  
  74.     // step 5a: calculate the sun's right ascension
  75.     $RA = rad2deg(atan(0.91764 * tan(deg2rad($L))));
  76.  
  77.     while ($RA < 0) {
  78.         $RAx = $RA + 360;
  79.         assert($RAx != $RA); // askingtheguru: really needed?
  80.         $RA = $RAx;
  81.     }
  82.  
  83.     while ($RA >= 360) {
  84.         $RAx = $RA - 360;
  85.         assert($RAx != $RA); // askingtheguru: really needed?
  86.         $RA = $RAx;
  87.     } 
  88.  
  89.     // step 5b: right ascension value needs to be in the same quadrant as L
  90.     $Lquadrant  = floor($L / 90) * 90;
  91.     $RAquadrant = floor($RA / 90) * 90;
  92.  
  93.     $RA = $RA + ($Lquadrant - $RAquadrant);
  94.  
  95.     // step 5c: right ascension value needs to be converted into hours
  96.     $RA /= 15;
  97.  
  98.     // step 6: calculate the sun's declination
  99.     $sinDec = 0.39782 * sin(deg2rad($L));
  100.     $cosDec = cos(asin($sinDec));
  101.  
  102.     // step 7a: calculate the sun's local hour angle
  103.     $cosH = (cos(deg2rad($zenith)) - ($sinDec * sin(deg2rad($latitude)))) / ($cosDec * cos(deg2rad($latitude)));
  104.  
  105.     // XXX: What's the use of this block.. ?
  106.     // if (!calc_sunset && cosH > 1 || calc_sunset && cosH < -1) {
  107.     //     throw doesnthappen();
  108.     // }
  109.  
  110.     // step 7b: finish calculating H and convert into hours 
  111.     if ($calc_sunset) {
  112.         // Sunset
  113.         $H = rad2deg(acos($cosH));
  114.     } else {
  115.         // Sunrise
  116.         $H = 360 - rad2deg(acos($cosH));
  117.     }
  118.     $H = $H / 15;
  119.  
  120.     // step 8: calculate local mean time
  121.     $T = $H + $RA - (0.06571 * $t) - 6.622;
  122.  
  123.     // Sunset step 9: convert to UTC
  124.     $UT = $T - $lngHour;
  125.  
  126.     while ($UT < 0) {
  127.         $UTx = $UT + 24;
  128.         assert($UTx != $UT); // askingtheguru: really needed?
  129.         $UT = $UTx;
  130.     }
  131.  
  132.     while ($UT >= 24) {
  133.         $UTx = $UT - 24;
  134.         assert($UTx != $UT); // askingtheguru: really needed?
  135.         $UT = $UTx;
  136.     }
  137.  
  138.     return $UT;
  139. }
  140.  
  141. function php_do_sunrise_sunset($date, $retformat, $latitude, $longitude, $zenith, $gmt_offset, $calc_sunset)
  142. {
  143.     if (is_int($date)) {
  144.         $time = $date;
  145.     } elseif (is_string($date)) {
  146.         // todo: more user friendly format
  147.     } else {
  148.         // date must be timestamp for now
  149.         trigger_error("date must be timestamp for now", E_USER_WARNING);
  150.         return false;
  151.     }
  152.     
  153.     $N = date("z", $time) + 1;
  154.     
  155.     if ($retformat === "") {
  156.         $retformat  = SUNFUNCS_RET_STRING;
  157.     }
  158.     if ($latitude === "") {
  159.         $latitude   = SUNFUNCS_DEFAULT_LATITUDE;
  160.     }
  161.     if ($longitude === "") {
  162.         $longitude  = SUNFUNCS_DEFAULT_LONGITUDE;
  163.     }
  164.     if ($zenith === "") {
  165.         if($calc_sunset) {
  166.             $zenith = SUNFUNCS_SUNSET_ZENITH;
  167.         } else {
  168.             $zenith = SUNFUNCS_SUNRISE_ZENITH;
  169.         }
  170.             
  171.     }
  172.     if ($gmt_offset === "") {
  173.         $gmt_offset = date("Z", $time) / 3600;
  174.     }
  175.  
  176.     $ret = php_sunrise_sunset($N, $latitude, $longitude, $zenith, $calc_sunset) + $gmt_offset;
  177.  
  178.     switch ($retformat) {
  179.         case SUNFUNCS_RET_TIMESTAMP:
  180.             return floor($time - ($time % (24 * 3600))) + floor(60 * $ret);
  181.             break;
  182.         case SUNFUNCS_RET_STRING:
  183.             $N = floor($ret);
  184.             return sprintf("%02d:%02d", $N, floor(60 * ($ret - $N)));
  185.             break;
  186.         case SUNFUNCS_RET_DOUBLE:
  187.             return $ret;
  188.             break;
  189.         default:
  190.             trigger_error("invalid format", E_USER_WARNING);
  191.             return false;
  192.     } 
  193. }
  194.  
  195. if (!function_exists("date_sunrise")) {
  196.     function date_sunrise($date, $retformat = "", $latitude = "", $longitude = "", $zenith = "", $gmt_offset = "")
  197.     {
  198.         return php_do_sunrise_sunset($date, $retformat, $latitude, $longitude, $zenith, $gmt_offset, 0);
  199.     }
  200. }
  201.  
  202. if (!function_exists("date_sunset")) {
  203.     function date_sunset($date, $retformat = "", $latitude = "", $longitude = "", $zenith = "", $gmt_offset = "")
  204.     {
  205.         return php_do_sunrise_sunset($date, $retformat, $latitude, $longitude, $zenith, $gmt_offset, 1);
  206.     }
  207. }
  208. ?>
  209.