PHP Calculate Easter

My browser homepage is set to a localhost php calendar that is connected to a database (to display birthdays, events, reminders, data of all sorts). One of the dates I originally found difficult to calculate was Easter. One of the definitions I found was, “Easter is observed on the first Sunday following the full moon that occurs on or following the spring equinox (March 21). Easter is a ‘movable’ feast which can occur as early as March 22 or as late as April 25.”

So, the logic is:
calculate the full moon on or after March 21
then find the next Sunday.

Easy right?

Fast forward to a long weekend where I just wanted something to play with (to take my mind off troubles). Did some research and came across some interesting ways of calculating a full moon. Some good, some not so good. Finally decided to go with a class I found at:

Note: For Easter you only need set, get_next_newmoon, and get_next_fullmoon methods. Also, because get_next_fullmoon calls get_next_newmoon (which means the full moon is based on the new moon), errors can occur if you specify a date after the new moon for a given month. I encountered this issue when I originally used ‘year-04-01’ in the conditional (reared its ugly head by saying the next full moon was in May).

include 'classes/moonPhase.class.php';

$moon = new moon();
$moon->set("date", date("$year_to_display-03-01"));
// year_to_display is calculated in my calendar - you can hardcode or calc
$nextFullMoon = $moon->get_next_fullmoon();


if(strtotime($nextFullMoon) < strtotime("$year_to_display-03-21")){
// might need to change to the 22nd
$moon->set("date", date("$year_to_display-03-22"));
// get next full moon
$nextFullMoon = $moon->get_next_fullmoon();
$easter = date("m-d-Y", strtotime("next Sunday", strtotime($nextFullMoon)));
// format to display in format I prefer
echo $easter;

I did most of that and then decided to go with using an array, because I suddenly decided it was not worth the effort. The next day, I decided to finish it. I parsed $easter (exploded) for comparison with displayed month and date, so my calendar would display Easter on the correct date. I compared it to the array of dates for Easter till 2022. Worked up to that point.

Note: My coding is not bullet proof. Use at your own risk. Also, as with many languages, there are many ways to do the same thing in PHP. I’m not saying I did it the best or most efficient way, just that it worked for me. If it helps you, all the better.

This entry was posted in Computer Stuff and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image