W3docs

jdtojulian()

Introduction

Converting Julian and Gregorian Dates

Julian dates are a continuous count of days and fractions since noon Universal Time on January 1, 4713 BC. The Gregorian calendar, on the other hand, is the calendar used by most of the world. Converting between these two date systems can be confusing, but with PHP, it is a straightforward process.

Note: The Julian Day Count used here refers to the astronomical Julian Day Number (a continuous count of days), which is distinct from the Julian calendar and modern astronomical Julian dates.

Converting Julian Dates to Gregorian Dates with PHP

To convert a Julian date to a Gregorian date in PHP, use the jdtogregorian() function. This function takes a single parameter: the Julian Day Count. It returns an associative array containing the month, day, and year.

Example

<?php

$julianDate = 2459472.5;
$gregorianDate = jdtogregorian($julianDate);

print_r($gregorianDate); // Outputs: Array ( [month] => 3 [day] => 2 [year] => 2021 )

As you can see, the function returns the Gregorian date components in an array. You can format it as YYYY-MM-DD using the following code:

$formattedDate = sprintf('%04d-%02d-%02d', $gregorianDate['year'], $gregorianDate['month'], $gregorianDate['day']);
echo $formattedDate; // Outputs: 2021-03-02

Note: jdtogregorian() truncates the fractional part of the Julian Day Count, as it only calculates the calendar date. The fractional part represents the time of day (e.g., 0.5 equals noon).

Converting Gregorian Dates to Julian Dates with PHP

To convert a Gregorian date to a Julian date in PHP, use the gregoriantojd() function. This function takes three parameters: the month, the day, and the year.

Example

<?php

$month = 3;
$day = 2;
$year = 2021;

$julianDate = gregoriantojd($month, $day, $year);

echo $julianDate; // Outputs: 2459472

As you can see, the function returns the Julian Day Count as an integer.

Conclusion

Converting between Julian dates and Gregorian dates is a common task in PHP development. With the jdtogregorian() and gregoriantojd() functions, this task becomes easy and straightforward. We hope this article has been helpful in explaining how to convert between Julian and Gregorian dates using PHP. For more details, see the jdtogregorian() and gregoriantojd() documentation.

Practice

Practice

What is the role of the jdtojulian() function in PHP?