PHP - 日曆 cal_from_jd() 函式



PHP 日曆cal_from_jd()函式用於將 jd 中給定的儒略日轉換為指定日曆的日期。

您可以使用儒略日計數工具找到格里高利、猶太或法蘭西等日曆中的可比日期。儒略日計數是從特定日期開始的持續不斷的日期計數。

語法

以下是 PHP 日曆cal_from_jd()函式的語法:

array cal_from_jd (int $jd, int $calendar)

引數

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

  • $jd - 它是一個儒略日計數(整數)。

  • $calendar - 它是要轉換到的日曆,支援的日曆值為:CAL_GREGORIAN、CAL_JULIAN、CAL_JEWISH 和 CAL_FRENCH。

返回值

cal_from_jd()函式返回一個數組,其中包含日曆資訊,如月份、日期、年份、星期幾、星期幾和月份的縮寫和完整名稱以及字串形式的日期“月/日/年”。

PHP 版本

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

示例 1

以下是 PHP 日曆cal_from_jd()函式的基本示例,其中我們將給定日期透過計算儒略日轉換回格里高利日曆格式。

<?php
   // Set the Julian Day Count
   $jd = 2459580; 

   // Specify the calendar type
   $calendar = CAL_GREGORIAN;

   // Convert the Julian Day Count to the specified calendar format
   $result = cal_from_jd($jd, $calendar);

   // Print the resulting date information
   print_r($result);
?>

輸出

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

Array
(
   [date] => 8/16/2024
   [month] => 8
   [day] => 16
   [year] => 2024
   [dow] => 2
   [abbrevdayname] => Tue
   [dayname] => Tuesday
   [abbrevmonth] => Aug
   [monthname] => August
)

示例 2

在下面的 PHP 程式碼中,我們將使用cal_from_jd()函式並將相同的儒略日計數轉換為猶太日曆日期。

<?php
   // Set the Julian Day Count
   $jd = 2460532;
   $calendar = CAL_JEWISH;
   $result = cal_from_jd($jd, $calendar);
   print_r($result);
?> 

輸出

這將生成以下輸出:

Array
(
   [date] => 12/5/5784
   [month] => 12
   [day] => 5
   [year] => 5784
   [dow] => 5
   [abbrevdayname] => Fri
   [dayname] => Friday
   [abbrevmonth] => Av
   [monthname] => Av
)

示例 3

現在下面的程式碼使用cal_from_jd()函式將儒略日計數轉換為法蘭西日曆日期,並打印出來。

<?php
   // Set the Julian Day Count
   $jd = 2460166;
   $calendar = CAL_FRENCH;
   $result = cal_from_jd($jd, $calendar);
   print_r($result);
?> 

輸出

這將建立以下輸出:

Array
(
   [date] => 0/0/0
   [month] => 0
   [day] => 0
   [year] => 0
   [dow] => 3
   [abbrevdayname] => Wed
   [dayname] => Wednesday
   [abbrevmonth] => 
   [monthname] => 
)

示例 4

該程式透過計算儒略日將給定日期(2016 年 8 月 16 日)轉換回格里高利日曆表示法。這在處理多個日曆系統時很有用。

<?php
   // Convert the given date into a Julian Day Count   
   $input = unixtojd(mktime(0, 0, 0, 8, 16, 2016));

   // Convert the Julian Day Count back to the Gregorian calendar format
   // And print the result
   print_r(cal_from_jd($input, CAL_GREGORIAN));
?> 

輸出

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

Array
(
    [date] => 8/16/2016
    [month] => 8
    [day] => 16
    [year] => 2016
    [dow] => 2
    [abbrevdayname] => Tue
    [dayname] => Tuesday
    [abbrevmonth] => Aug
    [monthname] => August
)
php_function_reference.htm
廣告