PHP 8.4.0 RC4 available for testing

easter_date

(PHP 4, PHP 5, PHP 7, PHP 8)

easter_date得到指定年份的复活节午夜时的 Unix 时间戳

说明

easter_date(?int $year = null, int $mode = CAL_EASTER_DEFAULT): int

返回指定年份的复活节午夜时的 Unix 时间戳。

复活节的日期是由尼西亚议会在公元 325 年确定的为每年春分月圆后的第一个星期日。春分一般是在 3 月 21 日,这就简化为只要计算满月的日期和紧挨的星期日的日期。这里所用的算法是在 532 年由 Dionysius Exiguus 引入。在 1753 年以前用儒略历计算,一个简单的 19 年周期用于追踪月相。在 1753 年之后公历(由 Clavius 和 Lilius 设计,1582 年 10 月由教皇 Gregory 十三世引入,并于 1752 年 9 月进入英国及其当时的殖民地)添加了两个校正因子以使周期更准确。

参数

year

对于 32 位系统,年份必须是 1970 至 2037 之间的数字,对于 64 位系统,年份必须是 1970 到 2,000,000,000 之间的数字。如果省略或者为 null,则根据本地时间默认为当前年份。

mode

当设置为 CAL_EASTER_ALWAYS_JULIAN 时,允许基于儒略历计算复活节时间。参见 calendar 常量

返回值

复活节日期的 Unix 时间戳。

错误/异常

如果年份早于 1970,或者晚于 32 位系统的 2037 以及 64 位系统上的 2,000,000,000 时会抛出 ValueError

更新日志

版本 说明
8.3.0 在 64 位系统上,year 参数现在接受范围在 1970 到 2,000,000,000 的值。
8.0.0 year 现在可为空(nullable)。
8.0.0 year 超出允许的范围现在抛出 ValueError。之前函数会返回 false 会触发 E_WARNING

示例

示例 #1 easter_date() 示例

<?php

echo date("M-d-Y", easter_date(1999)); // Apr-04-1999
echo date("M-d-Y", easter_date(2000)); // Apr-23-2000
echo date("M-d-Y", easter_date(2001)); // Apr-15-2001

?>

示例 #2 使用带 DateTimeeaster_date()

<?php

$timestamp
= easter_date(2023);

$datetime = new \DateTime();
$datetime->setTimestamp($timestamp);

echo
$datetime->format('M-d-Y'); // Apr-09-2023

?>

注释

注意:

easter_date() 依赖于系统的 C 库 time 函数,而不是使用 PHP 内置的 date 和 time 函数。因此 easter_date() 使用 TZ 环境变量来确定应该运行的时区,而不是使用 PHP 默认时区 ,此函数与 PHP 其他 date 函数一起使用时会出现意外行为。

有一种解决方案,可以使用带 DateTimeDateIntervaleaster_days() 来计算 PHP 时区中的复活节开始时间,如下所示:

<?php
function get_easter_datetime($year) {
$base = new DateTime("$year-03-21");
$days = easter_days($year);

return
$base->add(new DateInterval("P{$days}D"));
}

foreach (
range(2012, 2015) as $year) {
printf("Easter in %d is on %s\n",
$year,
get_easter_datetime($year)->format('F j'));
}
?>

以上示例会输出:

Easter in 2012 is on April 8
Easter in 2013 is on March 31
Easter in 2014 is on April 20
Easter in 2015 is on April 5

参见

  • easter_days() - 得到指定年份的 3 月 21 日到复活节之间的天数 用于计算 1970 年之前或 2037 年之后的复活节。

添加备注

用户贡献的备注 6 notes

up
28
maxie
16 years ago
To compute the correct Easter date for Eastern Orthodox Churches I made a function based on the Meeus Julian algorithm:

<?php
function orthodox_eastern($year) {
$a = $year % 4;
$b = $year % 7;
$c = $year % 19;
$d = (19 * $c + 15) % 30;
$e = (2 * $a + 4 * $b - $d + 34) % 7;
$month = floor(($d + $e + 114) / 31);
$day = (($d + $e + 114) % 31) + 1;

$de = mktime(0, 0, 0, $month, $day + 13, $year);

return
$de;
}
?>
up
8
Alexander Bliznyuk
7 years ago
Thank you, @Maxie, for algorythm for computing Orthodox Easter date.
It can be improved though. You added 13 days in order to map Julian calendar to Gregorian. But 13 days is not a constant. It's an accumulated error fixed in Gregorian and should be calculated with this formula: (int)($year / 100) - (int)($year / 400) - 2
up
11
py dot lebecq at gmail dot com
14 years ago
I recently had to write a function that allows me to know if today is a holiday.

And in France, we have some holidays which depends on the easter date. Maybe this will be helpful to someone.

Just modify in the $holidays array the actual holidays dates of your country.

<?php
/**
* This function returns an array of timestamp corresponding to french holidays
*/
protected static 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(
// These days have a fixed date
mktime(0, 0, 0, 1, 1, $year), // 1er janvier
mktime(0, 0, 0, 5, 1, $year), // Fête du travail
mktime(0, 0, 0, 5, 8, $year), // Victoire des alliés
mktime(0, 0, 0, 7, 14, $year), // Fête nationale
mktime(0, 0, 0, 8, 15, $year), // Assomption
mktime(0, 0, 0, 11, 1, $year), // Toussaint
mktime(0, 0, 0, 11, 11, $year), // Armistice
mktime(0, 0, 0, 12, 25, $year), // Noel

// These days have a date depending on easter
mktime(0, 0, 0, $easterMonth, $easterDay + 2, $easterYear),
mktime(0, 0, 0, $easterMonth, $easterDay + 40, $easterYear),
mktime(0, 0, 0, $easterMonth, $easterDay + 50, $easterYear),
);

sort($holidays);

return
$holidays;
}
?>
up
1
Guillaume Dufrene
11 years ago
I found a problem with holidays timestamp computation and daylight saving time.
An article about it at http://goo.gl/76t31 (in french only, sorry).

In summary, this year (2013) easter begins before adding an hour for daylight saving time (occured sunday at 3:00). It means that if you do $easter + X, where x is a number of seconds equivalent to one day, 39 days or 50 days, the result is not equals to a midnight timestamp...

Here a function to check if a midnight timestamp is equals to an holiday :

function isHoliday( $ts ) {
// Licence : Creative Commons (BY)
// By Webpulser - http://goo.gl/76t31
$fixed_holidays = array( ’01-01′, ’01-05′, ’08-05′, ’14-07′, ’15-08′, ’11-11′, ’25-12′ );
$format = ‘d-m’;

$dm = date($format, $ts);
if ( in_array($dm, $fixed_holidays) ) return true;

$easter = easter_date( date(‘Y’, $ts) );
if ( date($format, $easter + 86400) == $dm ) return true;
if ( date($format, $easter + 3369600) == $dm ) return true;
if ( date($format, $easter + 4320000) == $dm ) return true;

return false;
}

feel free to use / modify.
up
-1
phpuser
20 years ago
The algorithm from Bigtree is correct if you add some (int) cast
<?php
function easter_date ($Year) {

/*
G is the Golden Number-1
H is 23-Epact (modulo 30)
I is the number of days from 21 March to the Paschal full moon
J is the weekday for the Paschal full moon (0=Sunday,
1=Monday, etc.)
L is the number of days from 21 March to the Sunday on or before
the Paschal full moon (a number between -6 and 28)
*/


$G = $Year % 19;
$C = (int)($Year / 100);
$H = (int)($C - (int)($C / 4) - (int)((8*$C+13) / 25) + 19*$G + 15) % 30;
$I = (int)$H - (int)($H / 28)*(1 - (int)($H / 28)*(int)(29 / ($H + 1))*((int)(21 - $G) / 11));
$J = ($Year + (int)($Year/4) + $I + 2 - $C + (int)($C/4)) % 7;
$L = $I - $J;
$m = 3 + (int)(($L + 40) / 44);
$d = $L + 28 - 31 * ((int)($m / 4));
$y = $Year;
$E = mktime(0,0,0, $m, $d, $y);

return
$E;

}
?>
up
-3
adwil at live dot com
10 years ago
Hey, recently I needed a function to get realization dates in online shop, so here it is (ready to go for polish users, please adjust your dates for any other country):

<?php
function getWorkday($date1,$workDays) {
$workDays = (int)$workDays;
if (
$workDays <= 0)
return
null;

$date1=strtotime('-1 day',strtotime($date1));

$lastYear = null;
$hol=array('01-01','01-06','05-01','05-03','08-15','11-01','11-11','12-25','12-26'); //array of month-date of static holidays (these are from Poland)
$i = 0;
while (
$i<=$workDays) {
$year = date('Y', $date1);
if (
$year !== $lastYear){
$lastYear = $year;
$easter = date('m-d', easter_date($year));
$date = strtotime($year . '-' . $easter); // easter
$easterSec = date('m-d', strtotime('+1 day', $date)); // easter monday
$greens = date('m-d', strtotime('+49 days', $date)); // zielone swiatki
$cc = date('m-d', strtotime('+60 days', $date)); // boze cialo
$hol[] = $easter;
$hol[] = $easterSec;
$hol[] = $greens;
$hol[] = $cc;
}
$weekDay=date('w',$date1);
if (!(
$weekDay==0 || $weekDay==6 || in_array(date('m-d',$date1),$hol)))
$i++;

$date1=strtotime('+1 day',$date1);
}
return
date('Y-m-d',$date1);
}
?>
To Top