-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEaster.php
80 lines (62 loc) · 1.86 KB
/
Easter.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
<?php declare(strict_types=1);
namespace h4kuna\DataType\Date;
use DateTimeImmutable;
use h4kuna\Memoize\MemoizeStatic;
use Nette\StaticClass;
final class Easter
{
use StaticClass;
use MemoizeStatic;
public static ?bool $useNative = null;
/**
* @param ?int<1970, 2037> $year
*/
public static function monday(?int $year = null): DateTimeImmutable
{
return self::sunday($year)->modify('+1 day');
}
/**
* @param ?int<1970, 2037> $year
*/
public static function sunday(?int $year = null): DateTimeImmutable
{
if (self::$useNative === null) {
self::$useNative = function_exists('easter_date');
}
if ($year === null) {
$year = (int) date('Y');
}
return self::memoize([__METHOD__, $year], static fn (
): DateTimeImmutable => self::$useNative === true ? Convert::timestampToImmutable(self::native($year))->modify('today') : Convert::timestampToImmutable(self::counted($year)));
}
private static function native(int $year): int
{
return easter_date($year);
}
/**
* Based on https://github.com/steinger/easter-date
*/
private static function counted(int $year): int
{
$K = floor($year / 100);
$M = 15 + floor((3 * $K + 3) / 4) - floor((8 * $K + 13) / 25);
$S = 2 - floor((3 * $K + 3) / 4);
$A = $year % 19;
$D = (19 * $A + $M) % 30;
$R = floor($D / 29) + (floor($D / 28) - floor($D / 29)) * floor($A / 11);
$OG = 21 + $D - $R; // March date of Easter full moon (= 14. days of the first month in the moon calendar, called Nisanu)
$SZ = 7 - (($year + floor($year / 4) + $S) % 7); // Date first Sunday of March
$OE = 7 - (($OG - $SZ) % 7);
$OS = $OG + $OE;
$result = mktime(0, 0, 0, 3, (int) $OS, $year);
assert($result !== false);
return $result;
}
/**
* @param ?int<1970, 2037> $year
*/
public static function friday(?int $year = null): DateTimeImmutable
{
return self::sunday($year)->modify('-2 days');
}
}