W3docs

easter_date()

Introduction

Introduction

As we approach Easter, it’s important to understand how to calculate the date of the holiday programmatically. In modern PHP, the recommended way to determine Easter Sunday is using the DateTime class. This article explains the calculation methodology and provides step-by-step instructions for both current and legacy PHP implementations.

History of Easter

The date of Easter is based on a lunisolar calendar and is calculated using astronomical and mathematical rules. Historically, this calculation was formalized by the Council of Nicaea in 325 AD to standardize the holiday across different regions.

Calculation of Easter

The date of Easter is determined by calculating the first Sunday following the first full moon that occurs on or after the vernal equinox (the first day of spring). This method of calculation was established by the Council of Nicaea in 325 AD.

In PHP, you can implement this calculation using built-in date functions. Below are the recommended and legacy approaches:

Recommended Approach (PHP 5.3+) Use the DateTime class with the Easter Sunday modifier. It handles the complex lunar calculations internally and respects timezone settings.

  • Signature: new DateTime(string $datetime = "now", ?DateTimeZone $timezone = null)
  • Parameters: Pass "Easter Sunday YYYY" (e.g., "Easter Sunday 2023"). Optionally pass a DateTimeZone object.
  • Return Value: A DateTime object representing Easter Sunday at 00:00:00.
  • Timezone: Respects the provided timezone or the server's default timezone.

Legacy Function: easter_date() (Deprecated/Removed)

⚠️ Important: The easter_date() function was deprecated in PHP 8.1 and removed in PHP 8.2. It is no longer available in modern PHP environments. Use the DateTime approach above instead.

Historically, PHP provided the easter_date() function to calculate Easter.

  • Signature: easter_date(int $timestamp = time()): int
  • Parameters: A Unix timestamp representing the year to calculate Easter for.
  • Return Value: Unix timestamp for Easter Sunday of that year.
  • Timezone: Relies on date_default_timezone_set() and does not accept custom timezone arguments.

Example code

Modern Example (Recommended)

<?php

$year = 2023;
$easter = new DateTime("Easter Sunday $year");
echo $easter->format('F j, Y'); // Outputs: April 9, 2023

Legacy Example (PHP 8.1 and earlier only)

<?php

$year = 2023;
$easter_date = easter_date(mktime(0, 0, 0, 1, 1, $year));
echo date('F j, Y', $easter_date);

This code will output the date of Easter Sunday in the year 2023: April 9, 2023.

Conclusion

In conclusion, understanding how to calculate Easter dates programmatically is useful for calendar applications and holiday scheduling. For current PHP versions, always prefer the DateTime class with the Easter Sunday modifier for reliable, timezone-aware results. The legacy easter_date() function should only be used when maintaining legacy codebases running PHP 8.1 or older.

Practice

Practice

What does the easter_date() function in PHP return?