How to Calculate the Date of Easter with PHP

PHP Methods for the Calculation of Easter Dates

© Mark Alexander Bain

Mar 29, 2009
Easter Dates and PHP, Mark Alexander Bain
The date of Easter constantly changes according to a 1600 year old formula - a formula that is built into PHP, making the calculation of the Easter dates very easy.

The question “How many days is it until Easter?” poses a problem for most programmers. That’s because, unlike most annular events, the date on which Easter occurs changes from year to year in an apparently random manner. Fortunately this is not an issue for the PHP programmer.

How is the Date of Easter Calculated?

Most annular events occur on the same day every year:

  • Christmas is always December 25th
  • St. Valentine’s Day is always February 14th
  • St. Patrick’s Day is always March 17th

But Easter is not like that. The date on which Easter occurs varies from year to year. It can occur as early as March 22 or as late as April 25. The key is that the apparently random shifting of the date is not random at all and, in fact, there is a technique for calculating the date of Easter that has been in use since 325 AD.

The rule is quite simple: Easter is the first Sunday on or after the first full moon following the Spring Equinox. For the purposes of the formula the Spring Equinox is taken to be on March 21 (even though the Spring Equinox currently occurs on March 20). However, the PHP programmer does not need to know that the next time the Spring Equinox actually occurs on March 21 will be in 2102, all that the PHP programmer needs to know is that there are two functions that will carry out the necessary calculations:

  • easter_date
  • easter_days

With these two functions the programmer can work out the date of Easter and how many days Easter occurs after March 21.

Calculating the Date of Easter with PHP

The date of Easter is calculated by using the easter_date function. It takes the year number as an input (using the current year as the default) and returns a Unix timestamp, which then needs formatting into something more understandable:

$YEAR = date ("Y");
$EASTER = date ("d-M-Y", easter_date ($YEAR));
echo "Easter " . $YEAR . ": " . $EASTER . "<br>";

If this is placed into a PHP web page then it will show something like:

Easter 2009: 12-Apr-2009

It is also possible to calculate the number of days between March 21 and Easter. For that the programmer uses the easter_days method:

echo "Days from March 21 " . $YEAR .
" until Easter " . $YEAR . ": " .
easter_days($YEAR) . "<br>";

Which will produce something like:

Days from March 21 2009 until Easter 2009: 22

It is, however, worth noting that these methods only works with the range of years from 1970 to 2037. And so, bearing this limitation in mind, the next stage is to work out how long it is until Easter.

Calculating the Number of Days until Easter

To calculate the number of days until Easter the programmer simply compares the current timestamp (from the time method) with the Easter timestamp (from easter_date). They must, of course, take into account that if the current date is later than the date of Easter in the current year then it is the time until the Easter of the following year that is required:

function days_until_easter ($YEAR = NULL) {
if (!($YEAR)) {
$YEAR = date ("Y");
}
$DAYS = (int) (( easter_date ($YEAR) - time ())/(24*60*60));
if ($DAYS < 0 ) {
$DAYS = days_until_easter ($YEAR + 1);
}
return $DAYS;
}
echo "There are " . days_until_easter() . " days until Easter";

Which will produce something like:

There are 13 days until Easter

Summary

The date on which Easter occurs on any given year depends on the date of the first full moon after March 21. However, the PHP programmer does not need to be concerned about that. They simply need to use the easter_date method, which will give the date of Easter for any year (between 1970 and 2037) that they are interested in.


The copyright of the article How to Calculate the Date of Easter with PHP in PHP Programming is owned by Mark Alexander Bain. Permission to republish How to Calculate the Date of Easter with PHP in print or online must be granted by the author in writing.


Easter Dates and PHP, Mark Alexander Bain
PHP Code for Calculating the Dates for Easter, Mark Alexander Bain
Easter Dates in a PHP Web Page, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo