The PHP cal_from_jd() function is one of PHP’s built-in calendar conversion tools. It can convert a Julian Day (JD) into a specified calendar format. The syntax is as follows:
<span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-title function_ invoke__">cal_from_jd</span></span><span> ( </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$jd</span></span><span> , </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$calendar</span></span><span> )
</span></span>
$jd: The Julian Date to convert. This is a floating-point number representing the number of days since January 1, 4713 BCE, at noon.
$calendar: The type of calendar to convert into. PHP supports multiple calendar systems, including:
CAL_GREGORIAN (Gregorian calendar)
CAL_JULIAN (Julian calendar)
CAL_FAJI (Islamic calendar)
CAL_HEBREW (Hebrew calendar)
The function returns an associative array containing the converted date elements, such as year, month, and day.
To better understand how to use the cal_from_jd() function, let’s look at an example. Suppose you have a Julian Day value of 2459373.5 (which corresponds to July 16, 2022), and you want to convert it to the Gregorian calendar date.
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$jd</span></span><span> = </span><span><span class="hljs-number">2459373.5</span></span><span>; </span><span><span class="hljs-comment">// Julian Day</span></span><span>
</span><span><span class="hljs-variable">$calendar</span></span><span> = CAL_GREGORIAN; </span><span><span class="hljs-comment">// Gregorian</span></span><span>
</span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-title function_ invoke__">cal_from_jd</span></span><span>(</span><span><span class="hljs-variable">$jd</span></span><span>, </span><span><span class="hljs-variable">$calendar</span></span><span>);
<p></span>// Output result<br>
echo "Year: " . $date['year'] . </span>"\n";<br>
</span>echo "Month: " . $date['month'] . </span>"\n";<br>
</span>echo "Day: " . $date['day'] . </span>"\n";<br data-is-only-node="">
</span>?><br>
</span>
<span><span><span class="hljs-section">Year: 2022</span></span><span>
</span><span><span class="hljs-section">Month: 7</span></span><span>
</span><span><span class="hljs-section">Day: 16</span></span><span>
</span></span>
In this example, cal_from_jd() converts the Julian Day 2459373.5 into the Gregorian date July 16, 2022.
The Julian Day (JD) is a floating-point value where the integer part represents the number of days since January 1, 4713 BCE at noon, and the fractional part represents the time of day. For example, JD 2459373.5 indicates noon on that day, while JD 2459373.25 indicates midnight of the same day. For different calendar systems, cal_from_jd() adjusts date calculations according to their specific rules.
If you want to convert a Julian Day to a Julian calendar date, you can use the following code:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-variable">$jd</span></span><span> = </span><span><span class="hljs-number">2459373.5</span></span><span>;
</span><span><span class="hljs-variable">$calendar</span></span><span> = CAL_JULIAN; </span><span><span class="hljs-comment">// Julian Calendar</span></span><span>
</span><span><span class="hljs-variable">$date</span></span><span> = </span><span><span class="hljs-title function_ invoke__">cal_from_jd</span></span><span>(</span><span><span class="hljs-variable">$jd</span></span><span>, </span><span><span class="hljs-variable">$calendar</span></span><span>);
<p></span>echo "Year: " . $date['year'] . </span>"\n";<br>
</span>echo "Month: " . $date['month'] . </span>"\n";<br>
</span>echo "Day: " . $date['day'] . </span>"\n";<br>
</span>?><br data-is-only-node="">
</span>
Output:
<span><span><span class="hljs-section">Year: 2022</span></span><span>
</span><span><span class="hljs-section">Month: 7</span></span><span>
</span><span><span class="hljs-section">Day: 3</span></span><span>
</span></span>
As you can see, the Julian calendar date differs from the Gregorian one because the two systems calculate dates differently.
Precision: The cal_from_jd() function only supports precision up to whole days. The fractional part of the JD value does not affect the date output (unless you need time down to hours or minutes). For more precise time handling, combine it with other time functions.
Date Range: Different calendar systems have different valid ranges. For instance, the Hebrew and Islamic calendars have different starting points and valid ranges compared to the Gregorian and Julian calendars. Choose the appropriate calendar system depending on your needs.
Error Handling: Make sure the JD value passed in is valid. If an invalid JD is provided, the cal_from_jd() function will return false. Proper exception handling should be implemented when using it.