php

What is easter_date() Function in PHP

There are numerous built-in functions in PHP that allow you to finish difficult tasks efficiently. One of the php functions is the “easter_date()”. It computes a Unix timestamp for the Easter holiday(Sunday). Developers can programmatically establish the date of Easter Sunday by using this function, rather than relying on other sources or laborious calculations.

In this tutorial, we will analyze the usage of the “easter_date()” function in PHP along with its syntax.

Syntax

The general syntax of the “easter_date()” function is provided below:

According to the above code, it takes an optional parameter “$year” as an integer value between 1970 to 2037. If there is no year provided as a parameter, it gets the current year. Moreover, it will return the date of easter according to the year provided as a parameter into the Unix timestamp.

Algorithms “easter_date()” Function Uses

The Meeus/Jones/Butcher algorithm is used by the “easter_date()” function to determine the date of the Easter holiday. The Gregorian month calendar, which is currently one of the most commonly used calendar systems, served as the foundation for this algorithm’s mathematical calculation. Using the specified year, it establishes the exact day of Easter Sunday.

Example 1: Calculate Easter Date Using the Current Timezone with the “easter_date()” Function in PHP

In the following code, to get the Unix timestamp for Easter Sunday, we used the “easter_date()” function. Then, format the timestamp into a date format that is in the human-readable form using the notation “YYYY-MM-DD” inside the “date()” function. Finally, we used the “echo” statement to display the formatted date:

<?php

$easter_Timestamp = easter_date();

$easter_Date = date("Y-m-d", $easter_Timestamp);

echo "Sunday of Easter falls on: ". $easter_Date;

?>

Output

Example 2: Find the Date of Easter According to the Year Provided With “easter_date()” Function

In the below illustration, the variable “$my_year” is initialized with the “2023”. Then, call the “easter_date()” function, which determines the Unix timestamp for that year of Easter Sunday. After this, convert the timestamp into a presentable date string using the “date()” method and save the result in a variable named “$easter_Date”. Lastly, used the “echo” to get the resultant value:

<?php

$my_year = 2023;

$easterTimestamp = easter_date($my_year);

$easter_Date = date("Y-m-d", $easterTimestamp);

echo "Date of Easter in $my_year is: $easter_Date";

?>

As you can see, the Easter date for the provided year has been shown in the below-given output:

We have provided an easy explanation of the “easter_date()” function in PHP.

Conclusion

PHP has the “easter_date()” built-in function that can easily discover the Unix timestamp of Easter Sunday(a major day in the Christian calendar). Developers can programmatically get the date using this function without using outside resources or making tedious computations. In this guide, we have explained the “easter_date()” function in PHP.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.