 # Moon Phases, Sunrise and Sunset

So I want the web front end to give some other data, that might be of use to people.

Since I am also into astronomy, I would like to know what phase the moon is in (not that we’ve seen all that much of it recently). Turns out some clever bloke has written a php class to tell you what phase the moon is in, source is available via github.

PHP 5.1+ contains functions for calculating sunrise and sunset. As far as I can see there’s 3 functions, date_sunrisedate_sunset and date_sun_info. The latter looks like the best bet as you don’t have to supply as many variables.

Now I just need to get the GPS coords for my flat.

## 3 thoughts on “Moon Phases, Sunrise and Sunset”

1. Andy P says:

Did you get your moon phase working?

I found a PHP script and converted it into C++ so it would be easy to convert back.

std::string WorkOutMoonPhase(int year, int month, int day) {
std::string phase = “”;

float c, e, jd, b = 0;
if (month 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
}
if (b == 0) phase = “New Moon”;
if (b == 1) phase = “Waxing Crescent Moon”;
if (b == 2) phase = “Quarter Moon”;
if (b == 3) phase = “Waxing Gibbous Moon”;
if (b == 4) phase = “Full Moon”;
if (b == 5) phase = “Waning Gibbous Moon”;
if (b == 6) phase = “Last Quarter Moon”;
if (b == 7) phase = “Waning Crescent Moon”;
if (b 7) phase = “Error”;

return phase;
}

1. Andy P says:

Several bits of code have been stripped out. Hopefully this version will work…

std::string WorkOutMoonPhase(int year, int month, int day) {
std::string phase = “”;

float 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
}
if (b == 0) phase = “New Moon”;
if (b == 1) phase = “Waxing Crescent Moon”;
if (b == 2) phase = “Quarter Moon”;
if (b == 3) phase = “Waxing Gibbous Moon”;
if (b == 4) phase = “Full Moon”;
if (b == 5) phase = “Waning Gibbous Moon”;
if (b == 6) phase = “Last Quarter Moon”;
if (b == 7) phase = “Waning Crescent Moon”;
if (b < 0 || b > 7) phase = “Error”;

return phase;
}

1. Andrew says:

cheers for these I will have a look at these when it comes to writing the web page that will display the data.