Don't like ads? PRO users don't see any ads ;-)
Guest

Moon phase ++

By: matiasalejoroldan on May 6th, 2012  |  syntax: PHP  |  size: 5.51 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. /*
  3. Based on the JavaScript
  4. Lunar Phase Calculator
  5. by Stephen R. Schmitt
  6. which was adapted from a BASIC program from the Astronomical Computing column of Sky & Telescope, April 1994
  7. */
  8.  
  9. function isdayofmonth($month, $day, $year)
  10. {
  11.     $dim = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  12.     if ($month != 2)
  13.     {
  14.         if (1 <= $day && $day <= $dim[$month - 1])
  15.             return true;
  16.         else
  17.             return false;
  18.     }
  19.     $feb = $dim[1];
  20.     if (isleapyear($year))
  21.     {
  22.         $feb++;// is leap year
  23.     }
  24.  
  25.     if (1 <= $day && $day <= $feb)
  26.     {
  27.         return true;
  28.     }
  29.     return false;
  30. }
  31.  
  32. function isleapyear($year)
  33. {
  34.     $a = floor($year - 4 * floor($year / 4));
  35.     $b = floor($year - 100 * floor($year / 100));
  36.     $c = floor($year - 400 * floor($year / 400));
  37.  
  38.     // possible leap year
  39.  
  40.     if ($a == 0)
  41.     {
  42.         if ($b == 0 && $c != 0)
  43.             return false;// not leap year
  44.         else
  45.             return true;// is leap year
  46.     }
  47.     return false;
  48. }
  49.  
  50. // compute moon position and phase
  51. function moon_posit($month = null, $day = null, $year = null)
  52. {
  53.     $moon = array();
  54.     if(!isdayofmonth($month, $day, $year))
  55.     {
  56.         $moon['errors'] = 'Invalid date';
  57.     }
  58.     else
  59.     {
  60.         $moon['errors'] = null;
  61.         $age = 0.0;// Moon's age in days from New Moon
  62.         $distance = 0.0;// Moon's distance in Earth radii
  63.         $latitude = 0.0;// Moon's ecliptic latitude in degrees
  64.         $longitude = 0.0;// Moon's ecliptic longitude in degrees
  65.         $phase = '';
  66.         $zodiac = '';
  67.         $YY = 0;
  68.         $MM = 0;
  69.         $K1 = 0;
  70.         $K2 = 0;
  71.         $K3 = 0;
  72.         $JD = 0;
  73.         $IP = 0.0;
  74.         $DP = 0.0;
  75.         $NP = 0.0;
  76.         $RP = 0.0;
  77.  
  78.         // calculate the Julian date at 12h UT
  79.  
  80.         $YY = $year - floor((12 - $month) / 10);
  81.         $MM = $month + 9;
  82.         if ($MM >= 12)
  83.         {
  84.             $MM = $MM - 12;
  85.         }
  86.         $K1 = floor(365.25 * ($YY + 4712));
  87.         $K2 = floor(30.6 * $MM + 0.5);
  88.         $K3 = floor(floor(($YY / 100) + 49) * 0.75) - 38;
  89.         $JD = $K1 + $K2 + $day + 59;// for dates in Julian calendar
  90.         if ($JD > 2299160)
  91.         {
  92.             $JD = $JD - $K3;// for Gregorian calendar
  93.         }
  94.  
  95.         // calculate moon's age in days
  96.  
  97.         $IP = normalize(($JD - 2451550.1) / 29.530588853);
  98.         $age = $IP * 29.53;
  99.         if ($age <  1.84566)
  100.             $phase = 'NEW';
  101.         else if ($age <  5.53699)
  102.             $phase = 'Evening crescent';
  103.         else if ($age <  9.22831)
  104.             $phase = 'First quarter';
  105.         else if ($age < 12.91963)
  106.             $phase = 'Waxing gibbous';
  107.         else if ($age < 16.61096)
  108.             $phase = 'FULL';
  109.         else if ($age < 20.30228)
  110.             $phase = 'Waning gibbous';
  111.         else if ($age < 23.99361)
  112.             $phase = 'Last quarter';
  113.         else if ($age < 27.68493)
  114.             $phase = 'Morning crescent';
  115.         else
  116.             $phase = 'NEW';
  117.  
  118.         $IP = $IP * 2 * pi();// Convert phase to radians
  119.  
  120.         // calculate moon's distance
  121.  
  122.         $DP = 2 * pi() * normalize(($JD - 2451562.2) / 27.55454988);
  123.         $distance = 60.4 - 3.3 * cos($DP) - 0.6 * cos(2 * $IP - $DP) - 0.5 * cos(2 * $IP);
  124.  
  125.         // calculate moon's ecliptic latitude
  126.  
  127.         $NP = 2 * pi() * normalize(($JD - 2451565.2) / 27.212220817);
  128.         $latitude = 5.1 * sin($NP);
  129.  
  130.         // calculate moon's ecliptic longitude
  131.  
  132.         $RP = normalize(($JD - 2451555.8) / 27.321582241);
  133.         $longitude = 360 * $RP + 6.3 * sin($DP) + 1.3 * sin(2 * $IP - $DP) + 0.7 * sin(2 * $IP);
  134.         if ($longitude <  33.18)
  135.             $zodiac = 'Pisces';
  136.         else if ($longitude <  51.16)
  137.             $zodiac = 'Aries';
  138.         else if ($longitude <  93.44)
  139.             $zodiac = 'Taurus';
  140.         else if ($longitude < 119.48)
  141.             $zodiac = 'Gemini';
  142.         else if ($longitude < 135.30)
  143.             $zodiac = 'Cancer';
  144.         else if ($longitude < 173.34)
  145.             $zodiac = 'Leo';
  146.         else if ($longitude < 224.17)
  147.             $zodiac = 'Virgo';
  148.         else if ($longitude < 242.57)
  149.             $zodiac = 'Libra';
  150.         else if ($longitude < 271.26)
  151.             $zodiac = 'Scorpio';
  152.         else if ($longitude < 302.49)
  153.             $zodiac = 'Sagittarius';
  154.         else if ($longitude < 311.72)
  155.             $zodiac = 'Capricorn';
  156.         else if ($longitude < 348.58)
  157.             $zodiac = 'Aquarius';
  158.         else
  159.             $zodiac = 'Pisces';
  160.  
  161.         // so longitude is not greater than 360!
  162.  
  163.         if ($longitude > 360)
  164.             $longitude = $longitude - 360;
  165.         $moon['age'] = round($age, 2);
  166.         $moon['distance'] = round($distance, 2);
  167.         $moon['latitude'] = round($latitude, 2);
  168.         $moon['longitude'] = round($longitude, 2);
  169.         $moon['phase'] = $phase;
  170.         $moon['zodiac'] = $zodiac;
  171.     }
  172.     return $moon;
  173. }
  174.  
  175. // normalize values to range 0...1
  176.  
  177. function normalize($v)
  178. {
  179.     $v = $v - floor($v);
  180.     if ($v < 0)
  181.     {
  182.         $v++;
  183.     }
  184.     return $v;
  185. }
  186. ?>
  187.  
  188. <?php
  189.  
  190. include('moon2.php');
  191.  
  192. $date = time();
  193. $year = date('Y', $date);
  194. $month = date('n', $date);
  195. $day = date('j', $date);
  196. $moon = moon_posit($month, $day, $year);
  197.  
  198. //debug results
  199.  
  200. echo '<pre>';
  201. print_r($moon);
  202. echo '</pre>';
  203.  
  204. if ($moon['errors'] === null)
  205. {
  206.     //output and format what you want
  207. }
  208. else
  209. {
  210.     //handle error how you want, perhaps give error message and then show today''s moon info
  211. }
  212. ?>