CancelImage Upload

How to calculate a given year's Easter Dates in PHP

Let's say you want to plan your Easter weekend for 2016. Naturally enough to do this you will require the exact date that Easter falls on. You can now do two things. Go look up the date in a calendar, or simply make use of a predefined PHP function capable of returning every Easter that has or will occur during the Unix era.

<?php

$easter = easter_date(2016);

echo strftime("%d %b %Y", $easter);

?>

In case you were asking yourself what the day the date returned falls on, it is actually 00:00 of Easter Sunday! And if you are unaware the Unix era started in 1970, and will end in 2037. For Easter dates before 1970, or after 2037 you will have to make do with the function easter_days(), which returns the number of days between Easter and the 21st of March from a given year:

<?php

$year = 2016;

$easter = easter_days($year);

$easterSunday = mktime(0, 0, 0, 3 , 21 + $easter, $year);

echo strftime("%d %b %Y", $easterSunday);

?>

As you can see some time converting is needed to return the Easter Sunday by using easter_days. The result however is the same. All we have done is taken the number of days returned from easter_days(), and added them to the 21st of March, at 00:00:00 of the year specified:

$easterSunday = mktime(0, 0, 0, 3 , 21 + $easter, $year);

This then returns to use the timestamp for the Easter Sunday, and using the function strftime() we can convert it into a number that even makes sense to a human being!

echo strftime("%d %b %Y", $easterSunday);

For a deeper insight on how to format timestamps here is a reference.

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com