PHP - Calendar easter_days() 函式



PHP Calendar 的 easter_days() 函式用於計算給定年份 3 月 21 日到復活節星期日之間的天數。這對於查詢給定年份的復活節日期很有用。如果沒有指定年份,則假定為當前年份。

此函式可以替代 easter_date() 來計算 Unix 時間戳範圍之外的年份(1970 年之前或 2037 年之後)的復活節。

語法

以下是 PHP Calendar 的 easter_days() 函式的語法:

int easter_days ( int $year = null, int $mode = CAL_EASTER_DEFAULT )

引數

以下是 easter_days() 函式的引數:

  • $year − 年份,表示為 1970 年到 2037 年之間的數字。

  • $mode − 允許根據其他日曆計算復活節日期。例如,當設定為 CAL_EASTER_ROMAN 時,它在 1582 年至 1752 年期間使用格里高利曆。

    其他選項包括 CAL_EASTER_ALWAYS_GREGORIAN 和 CAL_EASTER_ALWAYS_JULIAN。

返回值

easter_days() 函式返回復活節星期日落在給定年份 3 月 21 日之後的幾天。

PHP 版本

easter_days() 函式首次引入核心 PHP 4,並在 PHP 5、PHP 7 和 PHP 8 中繼續輕鬆執行。

示例 1

此示例演示如何使用 PHP Calendar 的 easter_days() 函式計算當前年份 3 月 21 日到復活節星期日之間的天數。

<?php
   // Calculate the number of days for the current year
   $days = easter_days();
   
   // Output the result
   echo "Days from March 21st to Easter Sunday: $days";
?>

輸出

以下是以下程式碼的結果:

Days from March 21st to Easter Sunday: 10

示例 2

此示例說明如何使用 easter_days() 函式計算 2025 年 3 月 21 日到復活節星期日之間的天數。

<?php
   // Calculate the number of days for the year 2025
   $days = easter_days(2025);

   // Output the result
   echo "Days from March 21st to Easter Sunday in 2025: $days";
?> 

輸出

這將生成以下輸出:

Days from March 21st to Easter Sunday in 2025: 30

示例 3

現在以下程式碼使用 easter_days() 函式與 CAL_EASTER_ALWAYS_JULIAN 模式,並計算 2023 年的天數。

<?php
   // Calculate the number of days for 2023 using the Julian calendar method.
   $days = easter_days(2023, CAL_EASTER_ALWAYS_JULIAN);

   // Output the result
   echo "Days from March 21st to Easter Sunday in 2023 (Julian): $days";
?> 

輸出

這將建立以下輸出:

Days from March 21st to Easter Sunday in 2023 (Julian): 13

示例 4

在以下示例中,我們使用 easter_days() 函式獲取一年以上覆活節的天數。

<?php
   // Display the number of days in the mentioned years
   echo "Number of Easter days in 1995 - ". easter_days(1995). "\n";
   echo "Number of Easter days in 2012 - ". easter_days(2012). "\n";
   echo "Number of Easter days in 2018 - ". easter_days(2018). "\n";       
   echo "Number of Easter days in 2024 - ". easter_days(2024). "\n";
?> 

輸出

以下是上述程式碼的輸出:

Number of Easter days in 1995 - 26
Number of Easter days in 2012 - 18
Number of Easter days in 2018 - 11
Number of Easter days in 2024 - 10
php_function_reference.htm
廣告