WXforum.net

Web Weather => Weather Website PHP/AJAX scripting => Topic started by: rubble on September 30, 2008, 12:51:39 PM

Title: PHP Moon Phase Script
Post by: rubble 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.
Title: Re: PHP Moon Phase Script
Post by: SLOweather 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...
Title: Re: PHP Moon Phase Script
Post by: saratogaWX 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
Title: Re: PHP Moon Phase Script
Post by: rubble 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>
Title: Re: PHP Moon Phase Script
Post by: saratogaWX 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
Title: Re: PHP Moon Phase Script
Post by: rubble 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.
Title: Re: PHP Moon Phase Script
Post by: Cristian on April 10, 2009, 08:36:07 AM
Re: PHP Moon Phase Script
Title: Re: PHP Moon Phase Script
Post by: phingers 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.