PHP - Calendar cal_from_jd() Function
The PHP Calendar cal_from_jd() function is used to convert the Julian day given in jd into a date of the specified calendar.
You can find the comparable date in calendars like Gregorian, Jewish, or French by using the Julian Day Count tool. A Julian Day Count is an ongoing count of days that starts on a particular day.
Syntax
Below is the syntax of the PHP Calendar cal_from_jd() function −
array cal_from_jd (int $jd, int $calendar)
Parameters
Below are the parameters of the cal_from_jd() function −
$jd − It is the Julian Day Count (an integer).
$calendar − It is the calendar to convert to supported calendar. Supported calendar values are − CAL_GREGORIAN, CAL_JULIAN, CAL_JEWISH and CAL_FRENCH.
Return Value
The cal_from_jd() function returns an array containing calendar information like month, day, year, day of week, abbreviated and full names of weekday and month and the date in string form "month/day/year".
PHP Version
First introduced in core PHP 4.1.0, the cal_from_jd() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP Calendar cal_from_jd() function in which we converts a given date back to Gregorian calendar format by counting the Julian Days.
<?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); ?>
Output
Here is the outcome of the following code −
Array ( [date] => 8/16/2024 [month] => 8 [day] => 16 [year] => 2024 [dow] => 2 [abbrevdayname] => Tue [dayname] => Tuesday [abbrevmonth] => Aug [monthname] => August )
Example 2
In the below PHP code we will use the cal_from_jd() function and convert the same Julian Day Count to a Jewish calendar date.
<?php // Set the Julian Day Count $jd = 2460532; $calendar = CAL_JEWISH; $result = cal_from_jd($jd, $calendar); print_r($result); ?>
Output
This will generate the below output −
Array ( [date] => 12/5/5784 [month] => 12 [day] => 5 [year] => 5784 [dow] => 5 [abbrevdayname] => Fri [dayname] => Friday [abbrevmonth] => Av [monthname] => Av )
Example 3
Now the below code converts the Julian Day Count to an French calendar date using the cal_from_jd() function, and prints it.
<?php // Set the Julian Day Count $jd = 2460166; $calendar = CAL_FRENCH; $result = cal_from_jd($jd, $calendar); print_r($result); ?>
Output
This will create the below output −
Array ( [date] => 0/0/0 [month] => 0 [day] => 0 [year] => 0 [dow] => 3 [abbrevdayname] => Wed [dayname] => Wednesday [abbrevmonth] => [monthname] => )
Example 4
The program converts a given date (August 16, 2016) back to Gregorian calendar notation by counting the Julian Days. This is useful when working with many calendar systems.
<?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)); ?>
Output
Following is the output of the above code −
Array
(
[date] => 8/16/2016
[month] => 8
[day] => 16
[year] => 2016
[dow] => 2
[abbrevdayname] => Tue
[dayname] => Tuesday
[abbrevmonth] => Aug
[monthname] => August
)