-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRomanCalendar.php
401 lines (327 loc) · 11.5 KB
/
RomanCalendar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/**
* A PHP class to get informations about the Liturgical Roman Calendar
* @author Giacomo Mirabassi <[email protected]>
* @license GNU/GPL version 3 or later
* @version 1.0.0-beta
*/
class RomanCalendar
{
/* class constants */
const TIME_ORDINARY = 1;
const TIME_ADVENT = 2;
const TIME_CHRISTMAS = 3;
const TIME_LENT = 4;
const TIME_EASTER = 5;
const EASTER_MIN = 322;
const EASTER_MAX = 425;
const EASTER_DURATION = 40;
const LENT_DURATION = 45;
const SOLEMNITY = 200;
/**
* Get the liturgical time for a given date
* @param mixed $date a date to check, it can be a DateTime object or any format accepted from DateTime::__construct(), if omitted current date is assumed
* @return integer Returns one of the TIME_* constants
* @throws Exception
*/
public static function getYearTime($date = null)
{
try {
$date = static::_getDateTimeObject($date);
$year = (int)$date->format('Y');
$easter = static::getEasterDate((int)$date->format('Y'));
$easterInterval = $easter->diff($date);
if ($easterInterval->days == 0 || ($easterInterval->days <= static::EASTER_DURATION && $easterInterval->invert == 0)) {
return static::TIME_EASTER;
}
if ($easterInterval->days <= static::LENT_DURATION && $easterInterval->invert === 1) {
return static::TIME_LENT;
}
if ($date->format('m') < 3) {
$oldYear = $year - 1;
$christmas = static::_getDateTimeObject("$oldYear-12-25");
$christmasLength = static::getChristmasTimeLength($year);
$christmasInterval = $christmas->diff($date);
if ($christmasInterval->days <= $christmasLength && $christmasInterval->invert == 0) {
return static::TIME_CHRISTMAS;
}
} else if ($date->format('m') > 9) {
$advent = static::getAdventLength($year);
$christmas = static::_getDateTimeObject("$year-12-25");
$lastDay = static::_getDateTimeObject("$year-12-31");
$adventInterval = $christmas->diff($date);
if ($date >= $christmas && $date <= $lastDay) {
return static::TIME_CHRISTMAS;
}
if ($adventInterval->days <= $advent && $adventInterval->invert == 1) {
return static::TIME_ADVENT;
}
}
return static::TIME_ORDINARY;
} catch (Exception $e) {
throw $e;
}
}
/**
* Returns a DateTime object for the easter day of the given year;
* @param int|null $year the year to consider, if omitted current year assumed
* @return DateTime
* @throws Exception
*/
public static function getEasterDate($year = null)
{
try {
$year = static::_getYear($year);
return static::_getDateTimeObject('@' . easter_date($year));
} catch (Exception $e) {
throw $e;
}
}
/**
* check the year params and returns it if valid or the current year if null
* @param integer|null $year
* @return int
* @throws Exception
*/
protected static function _getYear($year = null)
{
if (!$year) {
$cur = static::_getDateTimeObject();
$year = (int)$cur->format('Y');
} else {
if (is_numeric($year)) {
$year = (int)$year;
} else {
throw new \Exception('Invalid year notation');
}
}
return $year;
}
/**
* checks the given $date parameters and returns the relative DateTime object
* @param mixed $date can be a DateTime object or any format accepted by DateTime::__construct()
* @return DateTime
* @throws Exception
*/
protected static function _getDateTimeObject($date = null)
{
$timezone = new \DateTimeZone('UTC');
if (!$date) {
return new \DateTime('now', $timezone);
}
if ($date instanceof \DateTime) {
$date->setTimezone($timezone);
return $date;
}
try {
return new \DateTime($date, $timezone);
} catch (Exception $e) {
throw $e;
}
}
/**
* Returns the day of Ash Wednesday for the requeste year
* @param integer $year the year to consider, if omitted current year assumed
* @return DateTime
* @throws Exception
*/
public static function getAshWednesday($year = null)
{
try {
$year = static::_getYear($year);
$easter = static::getEasterDate($year);
$interval = new \DateInterval('P' . static::LENT_DURATION . 'D');
return $easter->sub($interval);
} catch (Exception $e) {
throw $e;
}
}
/**
* Returns an array of the fixed holidays (currently only solemnities are considered)
* the first key is monthday (es 1225 for christmas)
* each key is associated to an array that contains a string for the name of the holiday and an integer for the type
* @return array
*/
public static function getFixedHolidays()
{
$fixed = [
101 => ['Mary, Mother of God', static::SOLEMNITY],
106 => ['Epiphany', static::SOLEMNITY],
319 => ['Saint Joseph', static::SOLEMNITY],
325 => ['Annunciation of the Lord', static::SOLEMNITY],
624 => ['Birth of Saint John the Baptist', static::SOLEMNITY],
629 => ['Saints Peter and Paul', static::SOLEMNITY],
815 => ['Assumption of the Blessed Virgin Mary', static::SOLEMNITY],
1001 => ['All Saints', static::SOLEMNITY],
1208 => ['Immaculate Conception of the Blessed Virgin Mary', static::SOLEMNITY],
1225 => ['Nativity of the Lord', static::SOLEMNITY]
];
return $fixed;
}
/**
* Returns an array of DateTime objects, one for each advent sunday
* @param integer $year the year to consider, if omitted current year assumed
* @return array:
* @throws Exception
*/
public static function getAdventSundays($year = null)
{
try {
$year = static::_getYear($year);
$christmasEve = static::_getDateTimeObject("$year-12-24");
$sundays = [];
if ($christmasEve->format('w') == 0) {
$sundays[0] = $christmasEve;
} else {
$interval = new \DateInterval('P' . $christmasEve->format('w') . 'D');
$sundays[0] = $christmasEve->sub($interval);
}
for ($i = 1; $i < 4; $i++) {
$interval = new \DateInterval('P7D');
$new = static::_getDateTimeObject('@' . $sundays[$i - 1]->getTimestamp());
$sundays[$i] = $new->sub($interval);
}
return array_reverse($sundays);
} catch (Exception $e) {
throw $e;
}
}
/**
* returns the day of the first advent sunday (the first day of advent and the first day of the liturgical year)
* @param integer $year the year to consider, if omitted current year assumed
* @return DateTime
*/
public static function getAdventStart($year = null)
{
$sundays = static::getAdventSundays($year);
return $sundays[0];
}
/**
* alias for getAdventStart()
* @param integer $year
* @return DateTime
*/
public static function getYearStart($year = null)
{
return static::getAdventStart($year);
}
/**
* Returns how much days is the advent long
* @param integer $year
* @throws Exception
*/
public static function getAdventLength($year = null)
{
try {
$year = static::_getYear($year);
$christmas = static::_getDateTimeObject("$year-12-25");
$firstSunday = static::getAdventStart($year);
return $firstSunday->diff($christmas)->days;
} catch (Exception $e) {
throw $e;
}
}
/**
* returns the last day of Christmas time
* @param integer $year
* @return DateTime
* @throws Exception
*/
public static function getChristmasTimeEnd($year = null)
{
try {
$year = static::_getYear($year);
$afterEpiphany = static::_getDateTimeObject("$year-01-07");
if ($afterEpiphany->format('w') == 0) {
return $afterEpiphany;
}
$toSunday = 7 - $afterEpiphany->format('w');
$interval = new \DateInterval('P' . $toSunday . 'D');
$afterEpiphany->add($interval);
return $afterEpiphany;
} catch (Exception $e) {
throw $e;
}
}
/**
* Number of days the christmas time is long
* @param integer $year
* @return integer
* @throws Exception
*/
public static function getChristmasTimeLength($year = null)
{
try {
$year = static::_getYear($year);
$startYear = $year - 1;
$start = static::_getDateTimeObject($startYear . '-12-25');
$end = static::getChristmasTimeEnd($year);
return $start->diff($end)->days;
} catch (Exception $e) {
throw $e;
}
}
/**
* Get the sunday after Ascention
* @param integer $year
* @return DateTime
* @throws Exception
*/
public static function getAscention($year = null)
{
try {
$year = static::_getYear($year);
$easter = static::getEasterDate($year);
$toAscention = new \DateInterval('P43D');
return $easter->add($toAscention);
} catch (Exception $e) {
throw $e;
}
}
/**
* get the pentecost sunday for the given year
* @param integer $year
* @return DateTime
* @throws Exception
*/
public static function getPentecost($year = null)
{
try {
$ascention = static::getAscention($year);
$diff = new \DateInterval('P7D');
return $ascention->add($diff);
} catch (Exception $e) {
throw $e;
}
}
/**
* Gets the ordinary time week of a given date or false if is not in ordinary time
* @param mixed $date
* @return boolean|integer
* @throws Exception
*/
public static function getOrdinaryWeek($date)
{
try {
$date = static::_getDateTimeObject($date);
$time = static::getYearTime($date);
if ($time != static::TIME_ORDINARY) {
return false;
}
$first = static::getChristmasTimeEnd($date->format('Y'));
$fromFirst = $first->diff($date);
if ($fromFirst->days < 7) {
return 1; //first ordiinary and last Christmas are the same
}
$ash = static::getAshWednesday($date->format('Y'));
if ($ash->diff($date)->invert == 1) {
return (int)(($first->diff($date)->days / 7) + 1);
}
$beforeAsh = (int)(($first->diff($ash)->days / 7) + 1);
$pentecost = static::getPentecost($date->format('Y'));
return (int)(($pentecost->diff($date)->days / 7) + 2 + $beforeAsh); //+2 because there is always an "empty" week in between
} catch (Exception $e) {
throw $e;
}
}
}