## Posted By

Scooter on 05/19/08

## Who likes this?

1 person have marked this snippet as a favorite

# Calculating Easter

/ Published in: PHP

Function library for calculation of Easter and related ecclesiastical holidays.

1. <?php
2. /*
3.   PHP Easter Calculation Library
4.
5.   Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
6.
9.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
10.   94305, USA.
11.
12.   This function library allows calculation of the following ecclesiastical holidays:
13.   Ash Wednesday, Palm Sunday, Good Friday, Easter, Ascension Day, and Pentecost.
14.
15.   Refer to Wikipedia for information on the purpose of these holidays..
16.   */
17.
18. // Determine the date of Easter for a given year.
19. function dateEaster(\$someYear)
20. {
21. \$goldenNumber = fmod(\$someYear, 19) + 1;
22.
23. if (\$someYear <= 1752)
24. {
25. // Julian calendar
26. \$dominicalNumber = fmod(\$someYear + (\$someYear / 4) + 5, 7);
27. \$paschalFullMoon = fmod(3 - (11 * \$goldenNumber) - 7, 30);
28. }
29. else
30. {
31. // Gregorian calendar
32. \$dominicalNumber = fmod(\$someYear + (\$someYear / 4) - (\$someYear / 100) + (\$someYear / 400), 7);
33. \$solarCorrection = (\$someYear - 1600) / 100 - (\$someYear - 1600) / 400;
34. \$lunarCorrection = (((\$someYear - 1400) / 100) * 8) / 25;
35. \$paschalFullMoon = fmod(3 - 11 * \$goldenNumber + \$solarCorrection - \$lunarCorrection, 30);
36. }
37.
38. while (\$dominicalNumber < 0)
39. {
40. \$dominicalNumber += 7;
41. }
42.
43. while (\$paschalFullMoon < 0)
44. {
45. \$paschalFullMoon += 30;
46. }
47.
48. if (\$paschalFullMoon == 29 || (\$paschalFullMoon == 28 && \$goldenNumber > 11))
49. {
50. \$paschalFullMoon--;
51. }
52.
53. \$difference = fmod(4 - \$paschalFullMoon - \$dominicalNumber, 7);
54.
55. if (\$difference < 0)
56. {
57. \$difference += 7;
58. }
59.
60. \$dayEaster = \$paschalFullMoon + \$difference + 1;
61.
62. if (\$dayEaster < 11)
63. {
64. // Easter occurs in March.
65. \$dateEaster = mktime(0, 0, 0, 3, \$dayEaster + 21, \$someYear);
66. }
67. else
68. {
69. // Easter occurs in April.
70. \$dateEaster = mktime(0, 0, 0, 4, \$dayEaster - 10, \$someYear);
71. }
72.
73. return \$dateEaster;
74. }
75.
76. // Determine the date of Good Friday for a given year.
77. // Requires dateEaster()
78. function dateGoodFriday(\$someYear)
79. {
80. \$easter = getDate(dateEaster(\$someYear));
81. return mktime(0, 0, 0, \$easter[mon], \$easter[mday] - 2, \$easter[year]);
82. }
83.
84. // Determine the date of Palm Sunday for a given year.
85. // Requires dateEaster()
86. function datePalmSunday(\$someYear)
87. {
88. \$easter = getDate(dateEaster(\$someYear));
89. return mktime(0, 0, 0, \$easter[mon], \$easter[mday] - 7, \$easter[year]);
90. }
91.
92. // Determines the date of Ash Wednesday for a given year.
93. // Requires dateEaster()
94. function dateAshWednesday(\$someYear)
95. {
96. \$easter = getDate(dateEaster(\$someYear));
97. return mktime(0, 0, 0, \$easter[mon], \$easter[mday] - 46, \$easter[year]);
98. }
99.
100. // Deteremines the date of Ascension Day for a given year.
101. // Requires dateEaster()
102. function dateAscensionDay(\$someYear)
103. {
104. \$easter = getDate(dateEaster(\$someYear));
105. return mktime(0, 0, 0, \$easter[mon], \$easter[mday] + 39, \$easter[year]);
106. }
107.
108. // Determines the date of Pentecost for a given year.
109. // Requires dateEaster()
110. function datePentecost(\$someYear)
111. {
112. \$easter = getDate(dateEaster(\$someYear));
113. return mktime(0, 0, 0, \$easter[mon], \$easter[mday] + 49, \$easter[year]);
114. }
115.
116. // Unit tests
117. // \$testYear = 2008;
118. // echo "Ash Wednesday: " . date("j F Y", dateAshWednesday(\$testYear)) . "<br>";
119. // echo "Palm Sunday: " . date("j F Y", datePalmSunday(\$testYear)) . "<br>";
120. // echo "Good Friday: " . date("j F Y", dateGoodFriday(\$testYear)) . "<br>";
121. // echo "Easter: " . date("j F Y", dateEaster(\$testYear)) . "<br>";
122. // echo "Ascension Day: " . date("j F Y", dateAscensionDay(\$testYear)) . "<br>";
123. // echo "Pentecost: " . date("j F Y", datePentecost(\$testYear)) . "<br>";
124. ?>