{"id":180,"date":"2012-12-25T12:45:57","date_gmt":"2012-12-25T17:45:57","guid":{"rendered":"http:\/\/www.lektrikpuke.com\/blog\/?p=180"},"modified":"2012-12-25T12:45:57","modified_gmt":"2012-12-25T17:45:57","slug":"php-calculate-easter","status":"publish","type":"post","link":"https:\/\/lektrikpuke.com\/blog\/2012\/12\/25\/php-calculate-easter\/","title":{"rendered":"PHP Calculate Easter"},"content":{"rendered":"<p>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, &#8220;Easter is observed on the first Sunday following the full moon that occurs on or following the spring equinox (March 21). Easter is a &#8216;movable&#8217; feast which can occur as early as March 22 or as late as April 25.&#8221;<\/p>\n<p>So, the logic is:<br \/>\ncalculate the full moon on or after March 21<br \/>\nthen find the next Sunday.<\/p>\n<p>Easy right?<\/p>\n<p>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: <a style=\"font-size:small;\" title=\"Moon Phase Calculate Class\" href=\"http:\/\/www.phpclasses.org\/package\/5975-PHP-Calculate-the-phase-of-the-Moon-for-a-given-date.html\" target=\"_blank\">http:\/\/www.phpclasses.org\/package\/5975-PHP-Calculate-the-phase-of-the-Moon-for-a-given-date.html<\/a><\/p>\n<p>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 &#8216;year-04-01&#8217; in the conditional (reared its ugly head by saying the next full moon was in May).<\/p>\n<p><code>&lt;?php<br \/>\ninclude 'classes\/moonPhase.class.php';<\/code><\/p>\n<p><code>$moon = new moon();<br \/>\n$moon-&gt;set(\"date\", date(\"$year_to_display-03-01\"));<br \/>\n\/\/ year_to_display is calculated in my calendar - you can hardcode or calc<br \/>\n$nextFullMoon = $moon-&gt;get_next_fullmoon();<\/code><\/p>\n<p>&nbsp;<\/p>\n<p><code>if(strtotime($nextFullMoon) &lt; strtotime(\"$year_to_display-03-21\")){<br \/>\n\/\/ might need to change to the 22nd<br \/>\n$moon-&gt;set(\"date\", date(\"$year_to_display-03-22\"));<br \/>\n\/\/ get next full moon<br \/>\n$nextFullMoon = $moon-&gt;get_next_fullmoon();<br \/>\n}<br \/>\n$easter = date(\"m-d-Y\", strtotime(\"next Sunday\", strtotime($nextFullMoon)));<br \/>\n\/\/ format to display in format I prefer<br \/>\necho $easter;<br \/>\n?&gt;<\/code><\/p>\n<p>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.<\/p>\n<p>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\u2019m not saying I did it the best or most efficient way, just that it worked for me. If it helps you, all the better.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/lektrikpuke.com\/blog\/2012\/12\/25\/php-calculate-easter\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[146,5,6,89],"class_list":["post-180","post","type-post","status-publish","format-standard","hentry","category-computer-stuff","tag-easter","tag-mysql","tag-php","tag-script"],"_links":{"self":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts\/180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/comments?post=180"}],"version-history":[{"count":1,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions"}],"predecessor-version":[{"id":181,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/posts\/180\/revisions\/181"}],"wp:attachment":[{"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/media?parent=180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/categories?post=180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lektrikpuke.com\/blog\/wp-json\/wp\/v2\/tags?post=180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}