Author Topic: PHP Moon Phase Script  (Read 17979 times)

0 Members and 1 Guest are viewing this topic.

Offline rubble

  • Member
  • *
  • Posts: 8
    • elginweather.com
PHP Moon Phase Script
« on: September 30, 2008, 12:51:39 PM »
Is anyone aware of a script that shows Moon Phase and Percent illumination?

I don't have Weather Display and I'm not looking for an image, I just want text.

Example:

Waxing Gibbous 28% Illumination.

Offline SLOweather

  • Administrator
  • Forecaster
  • *****
  • Posts: 2708
    • http://www.sloweather.com
Re: PHP Moon Phase Script
« Reply #1 on: September 30, 2008, 02:51:24 PM »
http://www.phpclasses.org/browse/package/1201.html

http://jivebay.com/2008/09/07/calculating-the-moon-phase/

Also, you say you don't have WD, if you have VWS, it has tags:

Moonrise   8:21am

Moonset   7:16pm

Moon Day  2    1%

With the Moon day or percent, you can script to choose the text description...
« Last Edit: September 30, 2008, 02:54:16 PM by SLOweather »

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 4526
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: PHP Moon Phase Script
« Reply #2 on: September 30, 2008, 05:27:17 PM »
From the VWS Moon Day tag, you can use something like this WD specific function to decode what the phase should be based on the age and percent illumination .. adjust it to fit the VWS variable output for moon day/percent

Code: [Select]
//=========================================================================
// decode WD %moonage% tag and return a text description for the moon phase name
// "Moon age: 10 days,10 hours,41 minutes,80%"

function moonphase ($WDmoonage) {

  preg_match_all('|(\d+)|is',$WDmoonage,$matches);
//  print "<!-- matches=\n" . print_r($matches,true) . "-->\n";
  $mdays = $matches[1][0];
  $mhours = $matches[1][1];
  $mmins = $matches[1][2];
  $mpct  = $matches[1][3];
 
  $mdaysd = $mdays + ($mhours / 24) + ($mmins / 1440);
  // Definitions from http://www.answers.com/topic/lunar-phase
  //  * Dark Moon - Not visible
  //  * New Moon - Not visible, or traditionally, the first visible crescent of the Moon
  //  * Waxing Crescent Moon - Right 1-49% visible
  //  * First Quarter Moon - Right 50% visible
  //  * Waxing gibbous Moon - Right 51-99% visible
  //  * Full Moon - Fully visible
  //  * Waning gibbous Moon - Left 51-99% visible
  //  * Third Quarter Moon - Left 50% visible
  //  * Waning Crescent Moon - Left 1-49% visible
  //  * New Moon - Not visible

  if ($mdaysd <= 29.53/2) { // increasing illumination
    $ph = "Waxing";
$qtr = "First";
  } else { // decreasing illumination
    $ph = "Waning";
$qtr = "Last";
  }
 
  if ($mpct < 1 ) { return("New Moon"); }
  if ($mpct <= 49) { return("$ph Crescent"); }
  if ($mpct < 51) { return("$qtr Quarter"); }
  if ($mpct < 99) { return("$ph Gibbous"); }
return("Full Moon");
 }

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis Vantage Pro Plus - FARS, Boltek-PCI/NexStorm, GRLevel3, WD, WL, VWS, Cumulus, Meteohub
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline rubble

  • Member
  • *
  • Posts: 8
    • elginweather.com
Re: PHP Moon Phase Script
« Reply #3 on: October 06, 2008, 01:15:41 PM »
I guess I should have posted more info.  I'm using VWS and my site runs from:

http://www.elginweather.com/vws/vwsfulldata.xml  and I don't upload any VWS image tags.

So I do have a <moon>7</moon> tag to work with.

How can this script be used?
Code: [Select]
<?php 

$html 
implode(''file('http://www.elginweather.com/vws/vwsfulldata.xml'));

preg_match('|<MOON>(.*)</MOON>|'$html$betweenspan);
$WDmoonage  $betweenspan[1];

//=========================================================================
// decode WD %moonage% tag and return a text description for the moon phase name
// "Moon age: 10 days,10 hours,41 minutes,80%"

function 
moonphase ($WDmoonage) {

  
preg_match_all('|(\d+)|is',$WDmoonage,$matches);
//  print "<!-- matches=\n" . print_r($matches,true) . "-->\n";
  
$mdays $matches[1][0];
  
$mhours $matches[1][1];
  
$mmins $matches[1][2];
  
$mpct  $matches[1][3];
  
  
$mdaysd $mdays + ($mhours 24) + ($mmins 1440);
  
// Definitions from http://www.answers.com/topic/lunar-phase
  //  * Dark Moon - Not visible
  //  * New Moon - Not visible, or traditionally, the first visible crescent of the Moon
  //  * Waxing Crescent Moon - Right 1-49% visible
  //  * First Quarter Moon - Right 50% visible
  //  * Waxing gibbous Moon - Right 51-99% visible
  //  * Full Moon - Fully visible
  //  * Waning gibbous Moon - Left 51-99% visible
  //  * Third Quarter Moon - Left 50% visible
  //  * Waning Crescent Moon - Left 1-49% visible
  //  * New Moon - Not visible

  
if ($mdaysd <= 29.53/2) { // increasing illumination
    
$ph "Waxing";
$qtr "First";
  } else { 
// decreasing illumination
    
$ph "Waning";
$qtr "Last";
  }
  
  if (
$mpct ) { return("New Moon"); }
  if (
$mpct <= 49) { return("$ph Crescent"); }
  if (
$mpct 51) { return("$qtr Quarter"); }
  if (
$mpct 99) { return("$ph Gibbous"); }
return("Full Moon");
 }
 
?>

 
<p>moonday=<?php echo $WDmoonage?></p>
<p>ph=<?php echo $ph?></p>
<p>mpct=<?php echo $mpct?></p>
<p>qtr=<?php echo $qtr?></p>

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 4526
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: PHP Moon Phase Script
« Reply #4 on: October 06, 2008, 03:16:54 PM »
If you only have the days in the cycle available, and not the percent illumination available, then you won't get an accurate descriptor for the moon-phase name, since that's based on percentage illumination, and only the waxing/waning is determined by first half/last half of the cycle.   Sorry...
Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis Vantage Pro Plus - FARS, Boltek-PCI/NexStorm, GRLevel3, WD, WL, VWS, Cumulus, Meteohub
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline rubble

  • Member
  • *
  • Posts: 8
    • elginweather.com
Re: PHP Moon Phase Script
« Reply #5 on: October 06, 2008, 05:30:11 PM »

The http://www.phpclasses.org/browse/package/1201.html  script blows!

I had that on my website and it needs constant attention.  Even the author said I shouldn't use it.

Offline Cristian

  • Member
  • *
  • Posts: 1
Re: PHP Moon Phase Script
« Reply #6 on: April 10, 2009, 08:36:07 AM »
More precise. Easy to use:

Code: [Select]
<?php
/*
Adaptation en php du fameux et excellent scripte Astro-MoonPhase de Brett Hamilton écrit en Perl.
http://search.cpan.org/~brett/Astro-MoonPhase-0.60/

Ce Scripte vous permettra de connaître, à une date donnée, l'illumination de la Lune, son age, 
sa distance en km par rapport à la Terre, son angle en degrés, sa distance par rapport au soleil, 
et son angle par rapport au soleil.

*/
class Moon
{
function phase($Year$Month$Day$Hour$Minutes$Seconds)
{
$DateSec mktime($Hour$Minutes$Seconds$Month$Day$Year0);

ini_set(precision"20"); //Defini la precision des calcules

# Astronomical constants.
$Epoch 2444238.5; # 1980 January 0.0

# Constants defining the Sun's apparent orbit.
$Elonge 278.833540; # ecliptic longitude of the Sun at epoch 1980.0
$Elongp 282.596403; # ecliptic longitude of the Sun at perigee
$Eccent 0.016718; # eccentricity of Earth's orbit
$Sunsmax 1.495985e8; # semi-major axis of Earth's orbit, km
$Sunangsiz 0.533128; # sun's angular size, degrees, at semi-major axis distance

# Elements of the Moon's orbit, epoch 1980.0.
$Mmlong 64.975464; # moon's mean longitude at the epoch
$Mmlongp 349.383063; # mean longitude of the perigee at the epoch
$Mlnode 151.950429; # mean longitude of the node at the epoch
$Minc 5.145396; # inclination of the Moon's orbit
$Mecc 0.054900; # eccentricity of the Moon's orbit
$Mangsiz 0.5181; # moon's angular size at distance a from Earth
$Msmax 384401.0; # semi-major axis of Moon's orbit in km
$Mparallax 0.9507; # parallax at distance a from Earth
$Synmonth 29.53058868; # synodic month (new Moon to new Moon)

$pdate Moon::jtime($DateSec);

$pphase; # illuminated fraction
$mage; # age of moon in days
$dist; # distance in kilometres
$angdia; # angular diameter in degrees
$sudist; # distance to Sun
$suangdia; # sun's angular diameter


# Calculation of the Sun's position.

$Day $pdate $Epoch; # date within epoch
$N Moon::fixangle((360 365.2422) * $Day); # mean anomaly of the Sun
$M Moon::fixangle($N $Elonge $Elongp); # convert from perigee
# co-ordinates to epoch 1980.0
$Ec Moon::kepler($M$Eccent); # solve equation of Kepler
$Ec sqrt(($Eccent) / ($Eccent)) * tan($Ec 2);
$Ec Moon::todeg(atan($Ec)); # true anomaly
$Lambdasun Moon::fixangle($Ec $Elongp); # Sun's geocentric ecliptic
# longitude
# Orbital distance factor.
$F = (($Eccent cos(Moon::torad($Ec))) / ($Eccent $Eccent));
$SunDist $Sunsmax $F; # distance to Sun in km
$SunAng $F $Sunangsiz; # Sun's angular size in degrees


# Calculation of the Moon's position.

# Moon's mean longitude.
$ml Moon::fixangle(13.1763966 $Day $Mmlong);

# Moon's mean anomaly.
$MM Moon::fixangle($ml 0.1114041 $Day $Mmlongp);

# Moon's ascending node mean longitude.
$MN Moon::fixangle($Mlnode 0.0529539 $Day);

# Evection.
$Ev 1.2739 sin(Moon::torad(* ($ml $Lambdasun) - $MM));

# Annual equation.
$Ae 0.1858 sin(Moon::torad($M));

# Correction term.
$A3 0.37 sin(Moon::torad($M));

# Corrected anomaly.
$MmP $MM $Ev $Ae $A3;

# Correction for the equation of the centre.
$mEc 6.2886 sin(Moon::torad($MmP));

# Another correction term.
$A4 0.214 sin(Moon::torad($MmP));

# Corrected longitude.
$lP $ml $Ev $mEc $Ae $A4;

# Variation.
$V 0.6583 sin(Moon::torad(* ($lP $Lambdasun)));

# True longitude.
$lPP $lP $V;

# Corrected longitude of the node.
$NP $MN 0.16 sin(Moon::torad($M));

# Y inclination coordinate.
$y sin(Moon::torad($lPP $NP)) * cos(Moon::torad($Minc));

# X inclination coordinate.
$x cos(Moon::torad($lPP $NP));

# Ecliptic longitude.
$Lambdamoon Moon::todeg(atan2($y$x));
$Lambdamoon += $NP;

# Ecliptic latitude.
$BetaM Moon::todeg(asin(sin(Moon::torad($lPP $NP)) * sin(Moon::torad($Minc))));

# Calculation of the phase of the Moon.

# Age of the Moon in degrees.
$MoonAge $lPP $Lambdasun;

# Phase of the Moon.
$MoonPhase = (cos(Moon::torad($MoonAge))) / 2;

# Calculate distance of moon from the centre of the Earth.

$MoonDist = ($Msmax * ($Mecc $Mecc)) /
($Mecc cos(Moon::torad($MmP $mEc)));

# Calculate Moon's angular diameter.

$MoonDFrac $MoonDist $Msmax;
$MoonAng $Mangsiz $MoonDFrac;

# Calculate Moon's parallax.

$MoonPar $Mparallax $MoonDFrac;

$pphase $MoonPhase; # illuminated fraction
$mage $Synmonth * (Moon::fixangle($MoonAge) / 360.0); # age of moon in days
$dist $MoonDist; # distance in kilometres
$angdia $MoonAng; # angular diameter in degrees
$sudist $SunDist; # distance to Sun
$suangdia $SunAng; # sun's angular diameter
$mpfrac Moon::fixangle($MoonAge) / 360.0;
return array( $pphase$mage$dist$angdia$sudist$suangdia$mpfrac$mpfrac );
}

function fixangle($x) { return ($x 360.0 * (floor($x 360.0))); } # fix angle
function torad($x) { return ($x * (M_PI 180.0)); } # deg->rad
function todeg($x) { return ($x * (180.0 M_PI)); } # rad->deg

function jtime($t)
{
$julian = ($t 86400) + 2440587.5; # (seconds /(seconds per day)) + julian date of epoch 2440587.5 / 86400 = 28,24753472222 Days
return ($julian);
}

function kepler($m$ecc)
{
$EPSILON 1e-6;

$m Moon::torad($m);
$e $m;
while (abs($delta) > $EPSILON)
{
$delta $e $ecc sin($e) - $m;
$e -= $delta / ($ecc cos($e));
}
return ($e);
}

}


//Exemple d'utilisation :

//Pour le 11 Avril 2009 à 00h00
list($MoonPhase$MoonAge$MoonDist$MoonAng$SunDist$SunAng$mpfrac) = Moon::phase(20090411000001);
echo 
"La Lune est éclairée à ".number_format($MoonPhase*1002',''')."%"."<br>";
echo 
"Son age est de ".number_format($MoonAge0',''')." jours"."<br>";
echo 
"Et elle se situe à une distance de ".number_format($MoonDist0',''')." km par rapport à la Terre."."<br>";
?>
Use at www.CropCirclesDataBase.org

Offline phingers

  • Member
  • *
  • Posts: 1
Re: PHP Moon Phase Script
« Reply #7 on: March 21, 2011, 02:30:46 PM »
The hard coded variables in there, how often do they change?

Sun's apparent orbit, moons orbit.

Did the earthquake affect any of these variables?

Seems like a great script.

Anyway to tweak it to also show the rise and set times based on location?

Thanks.

 

anything