easter_days

(PHP 4, PHP 5, PHP 7)

easter_days得到指定年份的3月21日到复活节之间的天数

说明

int easter_days ([ int $year [, int $method = CAL_EASTER_DEFAULT ]] )

返回指定年份的3月21日到复活节之间的天数,如果没有指定年份,默认是当年。

这个函数可以用来代替easter_date()函数来计算Unix时间戳以外年份的复活节日期。(比如1970年以前或2037年以后)

复活节的日期是由尼西亚议会在AD325年确定的为每年春分月圆后的第一个星期日。春分一般是在3月21日,这就简化为只要计算满月的日期和紧挨的星期日的日期。这里所用的算法是在532年由Dionysius Exiguus所介绍的,参考了Julian历法和Gregorian历法这两个历法来提高精确度。(在1753年以前用Julian历法计算,该历法是一个以19年为周期来确定月亮的相位的历法。在1753年以后用Gregorian历法计算,该历法由Clavius和Lilius发明,由Pope Gregory 8世在1582年推广)

参数

year

正数形式的年份

method

当设置为CAL_EASTER_ROMAN时可以用Gregorian历法来计算1582-1752之间的复活节日期。更多可用的常量参考calendar constants

返回值

根据给定参数year年份而返回的3月21日至复活节的天数。

更新日志

版本 说明
Since 4.3.0 参数year 可选,缺省默认值是当年。
Since 4.3.0 引入参数 method

范例

Example #1 easter_days() example

<?php

echo easter_days(1999);        // 14, i.e. April 4
echo easter_days(1492);        // 32, i.e. April 22
echo easter_days(1913);        //  2, i.e. March 23

?>

参见

  • easter_date() - 得到指定年份的复活节午夜时的Unix时间戳。

User Contributed Notes

trillian at vollbio dot de 06-Aug-2016 11:34
// example for easter_date() replacement:
function my_easter_date($year) {
  return strtotime(
    '+' . easter_days($year) . ' day',
    mktime(0, 0, 0, 3, 21, $year)
  );
}

// test
echo date('Y-m-d', my_easter_date(2016));
// result: 2016-03-27
p dot rijt at caesar dot nl 02-Jun-2015 12:20
This function returns an array of timestamp corresponding to Dutch National holidays. Liberation Day (Bevrijdingsdag) is added as a National holiday once every five years (2000, 2005, 2010, ...).

<?php
function getHolidays($year = null) {
    if (
$year === null) {
       
$year = intval(date('Y'));
    }
   
   
$easterDate  = easter_date($year);
   
$easterDay   = date('j', $easterDate);
   
$easterMonth = date('n', $easterDate);
   
$easterYear   = date('Y', $easterDate);
 
   
$holidays = array(
       
// Nieuwjaarsdag
       
mktime(0, 0, 0, 11$year), 
       
// 1e Kerstdag
       
mktime(0, 0, 0, 12, 25, $year), 
       
// 2e Kerstdag
       
mktime(0, 0, 0, 12, 26, $year)
    );
     
   
// Bevrijdingsdag
   
if (($year % 5) == 0) {
       
$holidays[] = mktime(0, 0, 0, 5, 5, $year); 
    }
   
   
// Koninginnedag (< 2014) of Koningsdag (>= 2014).
    // Verplaats naar zaterdag als het valt op zondag.
   
if ($year <= 2013) { // Koninginnedag <= 2013
       
if (date('w', mktime(0, 0, 0, 4, 30, $year)) == 0) { // Op zondag?
           
$holidays[] = mktime(0, 0, 0, 4, 29, $year);  // Verplaats naar zaterdag
       
} else {
           
$holidays[] = mktime(0, 0, 0, 4, 30, $year);  // Koninginnedag
       
}
    } else {
// Koningsdag > 2014
       
if (date('w', mktime(0, 0, 0, 4, 27, $year)) == 0) { // Op zondag?
           
$holidays[] = mktime(0, 0, 0, 4, 26, $year);  // Verplaats naar zaterdag
       
} else {
           
$holidays[] = mktime(0, 0, 0, 4, 27, $year);  // Koningsdag
       
}
    }
   
   
// Onderstaande dagen hebben een datum afhankelijk van Pasen
    // Goede Vrijdag (= pasen - 2)
   
$holidays[] = strtotime('-2 days', mktime(0, 0, 0, $easterMonth, $easterDay$easterYear));
   
// 1e Paasdag
   
$holidays[] = mktime(0, 0, 0, $easterMonth, $easterDay$easterYear);
   
// 2e Paasdag (= pasen +1)
   
$holidays[] = strtotime('+1 days', mktime(0, 0, 0, $easterMonth, $easterDay$easterYear));
   
// Hemelvaartsdag (= pasen + 39)
   
$holidays[] = strtotime('+39 days', mktime(0, 0, 0, $easterMonth, $easterDay$easterYear));
   
// 1e Pinksterdag (= pasen + 49)
   
$holidays[] = strtotime('+49 days', mktime(0, 0, 0, $easterMonth, $easterDay$easterYear));
   
// 2e Pinksterdag (= pasen + 50)
   
$holidays[] = strtotime('+50 days', mktime(0, 0, 0, $easterMonth, $easterDay$easterYear));

   
sort($holidays);
 
    return
$holidays;
}

$holidays = getHolidays(2014);

foreach (
$holidays as $holiday) {
    echo
date('d-M-Y', $holiday) . '<br>';
}
?>
ian at eiloart dot com-NOSPAM 19-Nov-2001 08:43
martin at diers dot cc 15-Aug-2001 07:59
This function appears to be Britanno-centric. When attempting to calculate the Gregorian date for Easter for years prior to 1753, the function returns the number or days since March 21st in the Julian Calendar, even though the Gregorian system was in effect in the rest of Europe since 1582. If you wish to calculate the date of easter for a Gregorian date from 1582 onward, use the following function, which duplicates the funcionality of easter_days:

<?php
   
function easter_days2($year) {
       
#First calculate the date of easter using Delambre's algorithm.
       
$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);
       
$n = ($h + $l - 7 * $m + 114);
       
$month = floor($n / 31);
       
$day = $n % 31 + 1;

       
#Return the difference between the JulianDayCount for easter and March 21'st
        #of the same year, in order to duplicate the functionality of the easter_days function
       
return GregorianToJD($month, $day, $year) - GregorianToJD(3,21,$year);
    }
?>