Skip to content

Instantly share code, notes, and snippets.

@jmsfwk
Created March 25, 2017 15:23
Show Gist options
  • Save jmsfwk/a6d9d167404888a56a07f776ca88098a to your computer and use it in GitHub Desktop.
Save jmsfwk/a6d9d167404888a56a07f776ca88098a to your computer and use it in GitHub Desktop.
A couple of algoritms for calculating the date of Easter
<?php
class Computus
{
public static function gauss(int $year)
{
$a = $year % 19;
$b = $year % 4;
$c = $year % 7;
$k = floor($year / 100);
$p = floor((13 + 8 * $k) / 25);
$q = floor($k / 4);
$M = (15 - $p + $k - $q) % 30;
$N = (4 + $k - $q) % 7;
$d = (19 * $a + $M) % 30;
$e = (2 * $b + 4 * $c + 6 * $d + $N) % 7;
$day = 22 + $d + $e;
$month = '03';
if ($day > 31) {
$day = $d + $e - 9;
$month = '04';
}
if ($d === 29 && $e === 6) {
$day = 19;
}
if ($d === 28 && $e === 6 && (11 * $M + 11) % 30 < 19) {
$day = 18;
}
return sprintf('%s-%s-%02d', $year, $month, $day);
}
public static function anonymous(int $year)
{
$a = $year % 19;
$b = floor($year / 100);
$c = $year % 100;
$d = floor($b / 4);
$e = $b % 4;
$f = floor(($b + 8) / 25);
$g = floor(($b - $f + 1) / 3);
$h = (19 * $a + $b - $d - $g + 15) % 30;
$i = floor($c / 4);
$k = $c % 4;
$l = (32 + 2 * $e + 2 * $i - $h - $k) % 7;
$m = floor(($a + 11 * $h + 22 * $l) / 451);
$month = floor(($h + $l - 7 * $m + 114) / 31);
$day = (($h + $l - 7 * $m + 114) % 31) + 1;
return sprintf('%s-%02s-%02d', $year, $month, $day);
}
/**
* @param int $year
*
* @see https://web.archive.org/web/20150227133210/http://www.merlyn.demon.co.uk/estralgs.txt
* @return string
*/
public static function petrofsky(int $year)
{
$a = $year / 100 | 0;
$b = $a / 4 | 0;
$c = ($a + 1258) * 2267;
$d = $year % 19;
$e = (19 * $d + (($c / 25 | 0) - $b)) % 30;
$f = $e - ((($e * 11 + $d) / 319) | 0);
$g = 120 + $f - ($f + $year + (($year / 4) | 0) + $b + $c) % 7;
$month = $g / 31 | 0;
$day = $g % 31 + 1;
return sprintf('%s-%02s-%02d', $year, $month, $day);
}
}
<?php
use PHPUnit\Framework\TestCase;
class ComputusTest extends TestCase
{
/**
* @test
* @dataProvider easter
*/
public function gauss($date)
{
$year = explode('-', $date)[0];
$this->assertEquals($date, Computus::gauss($year));
}
/**
* @test
* @dataProvider easter
*/
public function anonymous($date)
{
$year = explode('-', $date)[0];
$this->assertEquals($date, Computus::anonymous($year));
}
/**
* @test
* @dataProvider easter
*/
public function petrofsky($date)
{
$year = explode('-', $date)[0];
$this->assertEquals($date, Computus::petrofsky($year));
}
public function easter()
{
return [
["1800-04-13"],
["1801-04-05"],
["1802-04-18"],
["1803-04-10"],
["1804-04-01"],
["1805-04-14"],
["1806-04-06"],
["1807-03-29"],
["1808-04-17"],
["1809-04-02"],
["1810-04-22"],
["1811-04-14"],
["1812-03-29"],
["1813-04-18"],
["1814-04-10"],
["1815-03-26"],
["1816-04-14"],
["1817-04-06"],
["1818-03-22"],
["1819-04-11"],
["1820-04-02"],
["1821-04-22"],
["1822-04-07"],
["1823-03-30"],
["1824-04-18"],
["1825-04-03"],
["1826-03-26"],
["1827-04-15"],
["1828-04-06"],
["1829-04-19"],
["1830-04-11"],
["1831-04-03"],
["1832-04-22"],
["1833-04-07"],
["1834-03-30"],
["1835-04-19"],
["1836-04-03"],
["1837-03-26"],
["1838-04-15"],
["1839-03-31"],
["1840-04-19"],
["1841-04-11"],
["1842-03-27"],
["1843-04-16"],
["1844-04-07"],
["1845-03-23"],
["1846-04-12"],
["1847-04-04"],
["1848-04-23"],
["1849-04-08"],
["1850-03-31"],
["1851-04-20"],
["1852-04-11"],
["1853-03-27"],
["1854-04-16"],
["1855-04-08"],
["1856-03-23"],
["1857-04-12"],
["1858-04-04"],
["1859-04-24"],
["1860-04-08"],
["1861-03-31"],
["1862-04-20"],
["1863-04-05"],
["1864-03-27"],
["1865-04-16"],
["1866-04-01"],
["1867-04-21"],
["1868-04-12"],
["1869-03-28"],
["1870-04-17"],
["1871-04-09"],
["1872-03-31"],
["1873-04-13"],
["1874-04-05"],
["1875-03-28"],
["1876-04-16"],
["1877-04-01"],
["1878-04-21"],
["1879-04-13"],
["1880-03-28"],
["1881-04-17"],
["1882-04-09"],
["1883-03-25"],
["1884-04-13"],
["1885-04-05"],
["1886-04-25"],
["1887-04-10"],
["1888-04-01"],
["1889-04-21"],
["1890-04-06"],
["1891-03-29"],
["1892-04-17"],
["1893-04-02"],
["1894-03-25"],
["1895-04-14"],
["1896-04-05"],
["1897-04-18"],
["1898-04-10"],
["1899-04-02"],
["1900-04-15"],
["1901-04-07"],
["1902-03-30"],
["1903-04-12"],
["1904-04-03"],
["1905-04-23"],
["1906-04-15"],
["1907-03-31"],
["1908-04-19"],
["1909-04-11"],
["1910-03-27"],
["1911-04-16"],
["1912-04-07"],
["1913-03-23"],
["1914-04-12"],
["1915-04-04"],
["1916-04-23"],
["1917-04-08"],
["1918-03-31"],
["1919-04-20"],
["1920-04-04"],
["1921-03-27"],
["1922-04-16"],
["1923-04-01"],
["1924-04-20"],
["1925-04-12"],
["1926-04-04"],
["1927-04-17"],
["1928-04-08"],
["1929-03-31"],
["1930-04-20"],
["1931-04-05"],
["1932-03-27"],
["1933-04-16"],
["1934-04-01"],
["1935-04-21"],
["1936-04-12"],
["1937-03-28"],
["1938-04-17"],
["1939-04-09"],
["1940-03-24"],
["1941-04-13"],
["1942-04-05"],
["1943-04-25"],
["1944-04-09"],
["1945-04-01"],
["1946-04-21"],
["1947-04-06"],
["1948-03-28"],
["1949-04-17"],
["1950-04-09"],
["1951-03-25"],
["1952-04-13"],
["1953-04-05"],
["1954-04-18"],
["1955-04-10"],
["1956-04-01"],
["1957-04-21"],
["1958-04-06"],
["1959-03-29"],
["1960-04-17"],
["1961-04-02"],
["1962-04-22"],
["1963-04-14"],
["1964-03-29"],
["1965-04-18"],
["1966-04-10"],
["1967-03-26"],
["1968-04-14"],
["1969-04-06"],
["1970-03-29"],
["1971-04-11"],
["1972-04-02"],
["1973-04-22"],
["1974-04-14"],
["1975-03-30"],
["1976-04-18"],
["1977-04-10"],
["1978-03-26"],
["1979-04-15"],
["1980-04-06"],
["1981-04-19"],
["1982-04-11"],
["1983-04-03"],
["1984-04-22"],
["1985-04-07"],
["1986-03-30"],
["1987-04-19"],
["1988-04-03"],
["1989-03-26"],
["1990-04-15"],
["1991-03-31"],
["1992-04-19"],
["1993-04-11"],
["1994-04-03"],
["1995-04-16"],
["1996-04-07"],
["1997-03-30"],
["1998-04-12"],
["1999-04-04"],
["2000-04-23"],
["2001-04-15"],
["2002-03-31"],
["2003-04-20"],
["2004-04-11"],
["2005-03-27"],
["2006-04-16"],
["2007-04-08"],
["2008-03-23"],
["2009-04-12"],
["2010-04-04"],
["2011-04-24"],
["2012-04-08"],
["2013-03-31"],
["2014-04-20"],
["2015-04-05"],
["2016-03-27"],
["2017-04-16"],
["2018-04-01"],
["2019-04-21"],
["2020-04-12"],
["2021-04-04"],
["2022-04-17"],
["2023-04-09"],
["2024-03-31"],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment