Calculating the Moon Phase

I was searching for a algorithm to get the phase of the moon, and unfortunately the Moon Phase Class on PHP Classes is inaccurate. With some searching I was able to find 2 sites that have code to pull it off (
Lunar Phase Calculator and Moon Phase Calculation.

I modified the latter (Moon Phase Calculation and used the first function listed on that page).

<?php
function moon_phase($year, $month, $day)
{
/*
modified from http://www.voidware.com/moon_phase.htm
*/
$c = $e = $jd = $b = 0;
if ($month < 3)
{
$year--;
$month += 12;
}
++$month;
$c = 365.25 * $year;
$e = 30.6 * $month;
$jd = $c + $e + $day - 694039.09; //jd is total days elapsed
$jd /= 29.5305882; //divide by the moon cycle
$b = (int) $jd; //int(jd) -> b, take integer part of jd
$jd -= $b; //subtract integer part to leave fractional part of original jd
$b = round($jd * 8); //scale fraction from 0-8 and round
if ($b >= 8 )
{
$b = 0;//0 and 8 are the same so turn 8 into 0
}
switch ($b)
{
case 0:
return 'New Moon';
break;
case 1:
return 'Waxing Crescent Moon';
break;
case 2:
return 'Quarter Moon';
break;
case 3:
return 'Waxing Gibbous Moon';
break;
case 4:
return 'Full Moon';
break;
case 5:
return 'Waning Gibbous Moon';
break;
case 6:
return 'Last Quarter Moon';
break;
case 7:
return 'Waning Crescent Moon';
break;
default:
return 'Error';
}
}
$timestamp = time();
echo moon_phase(date('Y', $timestamp), date('n', $timestamp), date('j', $timestamp));
?>

I haven’t been able to find out how to calculate the percentage for how lit up (full) the moon is yet though.

I converted the other code in another blog post. Calculating the Moon Phase Part 2

Found two WordPress plugins that have some code you might look at for moon phase calculation: ephemeris and moon phases

Tags: ,

Comments 1-14 of 14 to “Calculating the Moon Phase”

  1. 1 · Happy says:

    Big THX for this code.

    Until today, I have found functions and classes, which calculated moon-phase from 1970 year. That was too small range for me.

    This script calculates correctly phase of the Moon and is so simple :)

  2. 2 · Happy_2 says:

    I’m also grateful for the code. (Although that’s a lot of empty lines to delete after copy/paste. :) )

    It looks to me like the percentage lit can be gotten this way: using the value of $jd after the line “$jd -= $b”, take the difference from 4 if $jd 4, and divide that difference by 4.

    Thanks again.

  3. 3 · Happy_2 says:

    I’ll try again, this time without the accidental markup. :)

    Using the value of $jd after the line “$jd -= $b”, subtract $jd from 4 if $jd is less than 4, or from 8 if $jd is greater than 4. Divide the result by 4.

  4. 4 · Anonymous says:

    Just found this page – I’m going to try it out ..For illumination percentage
    after $b = round($jd * 8);
    how about

    $percent=abs($b-8)*25

  5. 5 · newbie says:

    Hi there – I’m a bit (read: completely) new to this, so bear with me. I’m trying to calculate moon phases for East Africa (to see if phase of the moon affects hunting success of lions), and just came across your script. I was wondering what language it was in? Basically, I’m looking for something that I can implement in excel (that’s where I’ve got my data currently) – I was hoping to try to (learn to) write a macro so it would grab the date in one column and spit out the moon phase and % illumination in the next column.
    So two questions: Is that something I can do with this code? and doesn’t moon phase depend on your location on the globe? (i.e. Would this algorithm be accurate for the US but not for Tanzania?)
    Anyway, thanks for anything you can tell me!
    Cheers

  6. 6 · blogger says:

    The code is in PHP and I cover how to get the longitude and latitude for the area you want. I’m sure it’s possible to implement it in Excel.

  7. 7 · blogger says:

    I’ve tried calculating the illumination of the moon using Happy_2 and Anonymous’s suggestions. However, it’s hard to tell which one is more accurate, sometimes one seemed more accurate than the other. Not only that but many sites online differ by several percentage points themselves, so it’s hard to figure out which one is correct.

  8. 8 · john says:

    i found this code inaccurate!

    For today 7th April 2010 im getting “Last Quarter Moon” when it should be infact “Waning Cresent”

  9. 9 · john says:

    reply to my own prev post

    if i change

    echo moon_phase(date(‘Y’, $timestamp), date(‘n’, $timestamp), date(‘j’, $timestamp));

    to

    echo moon_phase(‘2010′, ‘6’, ‘7’);

    i get the correct phase, strange how echo moon_phase(date(‘Y’, $timestamp), date(‘n’, $timestamp), date(‘j’, $timestamp)); is not working

  10. 10 · blogger says:

    You might also try the code I have at Calculating the Moon Phase Part 2. However I’ve yet to find an algorithm that is 100% correct.

  11. 11 · miza says:

    How calculate the eclipses with PHP??????

  12. 12 · Harshal says:

    Sorry, I am newbie so please bare with me. How to calculate this for a specific location?

    Thanks

  13. 13 · blogger says:

    I added 2 links to WordPress plugins that calculate the moon phase more accurately than the code above.

  14. 14 · Igor says:

    WOW, thanks so much your content, you the best. ;D

Leave a Comment

Comments are reviewed before publishing to prevent spam.