PHP function to show Moon Phase name from %moonage%

WeatherLink has a tag for the name of the lunar phase name, but I couldn’t find one for WD, so I wrote a small PHP function to give you the text based on the contents of the WD %moonage% which has the age in days, hours, minutes and the % illumination.

I use it on my home page to display the phase name, the .gif of the moon, and the %illumination like this:

         <table border="0" cellpadding="4" cellspacing="0">
           <tr>
             <td style="text-align: center;" class="data"><?php echo moonphase($moonage); ?></td>
           </tr>
           <tr>
             <td style="text-align: center;"><img src="/images/moon<?php 
                        $moonagedays = preg_replace('|^Moon age: (\d+)\s.*$|i',"\$1",$moonage);
			 echo $moonagedays; ?>.gif" alt="<?php 
			 $t1 =  moonphase($moonage) . ", Moon at $moonagedays days in cycle";
			 echo $t1; ?>" title="<?php echo $t1; ?>" 
			 width="48" height="48" style="border: 0;" /></td>
           </tr>
		   <tr>
             <td style="text-align: center;" class="data" ><?php 
			 echo $moonphase; ?>

              Illuminated</td>
           </tr>
         </table>

The variables named are loaded by my testtags.php from a WD upload that sets them like this:


<?php
$moonage = "%moonage%";	// current age of the moon (days since new moon)
$moonphase = "%moonphase%";	// Moon phase %
?>

Sample contents of the variables substituted by WD are

<?php
$moonage = "Moon age: 26 days,13 hours,41 minutes,10%";	// current age of the moon (days since new moon)
$moonphase = "10%";	// Moon phase %
?>

Here’s the function that decodes $moonage into the text description

<?php
function moonphase ($WDmoonage) {
// decode WD %moonage% tag and return a text description for the moon phase name
// "Moon age: 10 days,10 hours,41 minutes,80%"

  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");
 }
?>

I’ve zipped up a set of moonNN.gif files at http://saratoga-weather.org/moon.zip if you need 'em.

Enjoy!

Best regards,
Ken

Once again, very clever Ken. :smiley:

I’ve been looking for the reverse. #-o Weatherlink only shows the phase name. I’ve been looking for a PHP script that would show days, hours, minutes and the % illumination and a tiny icon for each phase. I found some javascript, but it doesn’t do all I’m looking for.

Look no more… there’s a moon-phase (illumination, etc) PHP class script/demo page available on
PHPClasses (http://www.phpclasses.org/browse/package/1201.html ).

See a sample output at http://saratoga-weather.org/WDtest/moon-phase.php

which looks like this

On this date: Jan 16 2007 10:47:38:
The position (phase) within the moon's cycle: 0.44259270596342
The phase name: Waning Crescent
The percentage of lunar illumination is 3.2%
The days until the next full moon are: 16.46
The days until the next new moon are: 1.7
The days until the next first quarter moon are: 9.1
The days until the next last quarter moon are: 23.8

Moon phases for upcoming week:
  Tuesday: Waning Crescent
  Wednesday: New Moon
  Thursday: New Moon
  Friday: Waxing Crescent
  Saturday: Waxing Crescent
  Sunday: Waxing Crescent
  Monday: Waxing Crescent

Note, you’ll need to sign up with PHPClasses (it’s free) to download the script from the link above. I do recommend PHPClasses.org – there are a lot of nice PHP classes to use… Jim’s animategr3.php uses a class from there.

Too bad I’d not searched there FIRST, I could have saved some coding for my days/illumination → name script #-o

Ken

Very nice Ken! :slight_smile:

Very nice (as expected) Ken!

To be absolutely complete it needs a northern/southen hemisphere switch for the icons :wink:

Wow, Niko, You’re very right… curse my northern-hemisphere myopia #-o

I suppose a good way would be to have the moon images rotated 180 degrees to show the image as seen from the southern hemisphere, yes?

Ken

Ken:

You really are a great asset to this (and other) forums. Thanks for the great find!!!

The images are the same, just that waxing = waning and vice versa, so I was thinking about a switch in the selection routine.

Ken,
I’ve got to say your home page looks amazing. I dont know how you do it.
I really can not think of anything more that you could add to it, but I’m sure you will think of something.
I suspect you will get that moon thing sorted by the end of the day. :slight_smile:

Niko, I think we’re both right…

http://www.museum.vic.gov.au/planetarium/solarsystem/moon.html

Shows that the moon is upside-down (from our perspective) when viewed in the southern hemisphere.

The phase names are the same, but they’re swapped right to left

New: Dark both sides (day=0, day=29)

Waxing Crescent,First quarter, Waxing Gibbous: North: light on right, South: light on left

Full : light on both sides. Day=14.25 or so

Waning Gibbous, Last Quarter, Waning Crescent: North: light on left, South: light on right

Waxing= illumination increasing, Waning=illumination decreasing so it should be always waxing from New to Full and always waning from Full to New.

Ken

There seems to be a bit of disparity between this script and when the next New Moon is. According to WD’s calculations, the next New Moon is 04:01 UTC Fri 19 Jan 2007 (and this agrees with many other tables), but the above script suggests that the next New Moon will be on Wednesday. Also, at the present moment in time, the moon is 6% illuminated, as opposed to 3.2% in the script. :?

I had a look at the script and up-loaded it as a test (www.chanctonbury.org.uk/test/moon-phase.php), and it comes up with exactly the same output as Ken’s above. I’m a bit confused now??

I thought that it would be easier to use that script rather than modifying Ken’s script that works with his testtags.php file to mine that works with a wx30.html file. I’ll have another look another day, getting late now!

Ian

maybe i could add a custom tag…
let me know what to check/use etc…

Yes, the author states that there are slight differences that occur during the course of a lunar month; mainly based on the different programs placement rounding, and the difference from one computer to the next with floating point calculations. It also depends on the accuracy of the server’s clock. The new script was within 1% of my old one and this on has much more info. I have it running at:

http://eetee.us/station/station.php

Now, I’m going to try to create a tooltip to show a picture of the moon phase when the visitor mouses over the “written” moon phase on the page (similar to my “current conditions” link). I’ll have to think about this one for a while :roll:

Right, I realized as I was heading into the valley that I had probably misinterpreted your post by thinking it was saying that you were going to actually 180 deg rotate all the .gif’s :oops:

Ummm… actually, I was going to rotate them 180 degrees, since if you stand on your head (in the northern hemisphere) and look at the moon, you’ll see what the southern hemisphere sees :slight_smile:

Ken

Brian:

If you’d like to create a WD custom tag named %moonphasename% and have it set using your built-in calcs, that would be great! You could use similar logic to determine the name that I’d used in the PHP script.

Best regards,
Ken

But you already have the 180 rotated versions in the set.

So instead of selecting moon(n).gif you would select moon((n-29)*-1)).gif

Your formula would select the correct lighted image (left and right), but the inside image of the moon would still be upside-down for a southern hemisphere observer. If I rotate each image 180 degrees (and keep the same names), then the images should match exactly with what a southern hemisphere viewer would see.

Ken

Oh, that’s what happens when you don’t look closely at the actual images :oops: