W3docs

jdtogregorian()

In this article, we will discuss the function 'jdtogregorian()' in PHP and its usage for converting Julian dates to Gregorian dates. We will also delve into the

Introduction

In this article, we will discuss the function 'jdtogregorian()' in PHP and its usage for converting Julian dates to Gregorian dates. We will also delve into the history and significance of Julian and Gregorian calendars and the difference between the two.

Julian and Gregorian Calendars

The Julian calendar was introduced by Julius Caesar in 45 BCE and was based on the concept of a solar year, which is the time taken by the Earth to complete one orbit around the sun. However, the Julian calendar had a minor flaw that caused it to drift away from the solar year by approximately 11 minutes per year. This might not seem like a significant difference, but over time, it led to a noticeable shift in the calendar dates.

To rectify this problem, the Gregorian calendar was introduced by Pope Gregory XIII in 1582 CE. The Gregorian calendar was a refinement of the Julian calendar and aimed to align the calendar dates with the solar year. The difference between the two calendars is that the Julian calendar had 365.25 days in a year, while the Gregorian calendar has 365.2425 days. This adjustment resulted in the elimination of leap years in years ending in "00," unless they were divisible by 400.

Julian Dates

Julian dates are a system of counting days since January 1, 4713 BCE, which was the date of the start of the Julian period. Julian dates are commonly used in astronomy and are based on the number of days that have elapsed since the start of the Julian period.

Converting Julian Dates to Gregorian Dates

The jdtogregorian() function in PHP converts a Julian Day Count into a Gregorian calendar date. It is part of PHP's calendar extension, which is enabled by default in most builds.

Syntax

jdtogregorian(int $julian_day): string

$julian_day is the Julian Day Count to convert. The function returns the matching Gregorian date as a string in the format month/day/year — for example 10/4/2021. Note that the parts are not zero-padded (you get 10/4/2021, not 10/04/2021), and a Julian day of 0 returns the sentinel string 0/0/0.

A Julian Day Count is a plain integer counting days since January 1, 4713 BCE. Don't confuse it with a date on the Julian calendar — those are two different things. To get a Julian Day Count from a Gregorian date in the first place, use gregoriantojd() or cal_to_jd().

Basic example

Let's convert the Julian Day Count 2459492 to its Gregorian date:

<?php

echo jdtogregorian(2459492);

Output:

10/4/2021

Reformatting the output to ISO 8601

Because the returned string uses month/day/year with no zero-padding, parse it with the n/j/Y format characters (n = month without leading zeros, j = day without leading zeros) when you need a standard YYYY-MM-DD value:

<?php

$gregorian = jdtogregorian(2459492);          // "10/4/2021"
$date = date_create_from_format('n/j/Y', $gregorian);
echo $date->format('Y-m-d');

Output:

2021-10-04

See date_create_from_format() for more on parsing dates from custom formats.

Round-tripping a date

jdtogregorian() is the inverse of gregoriantojd(). Converting a date to a Julian Day Count and back returns the original value:

<?php

$jd = gregoriantojd(10, 4, 2021); // month, day, year
echo $jd, "\n";                   // 2459492
echo jdtogregorian($jd);          // 10/4/2021

Output:

2459492
10/4/2021

When working with Julian Day Counts you will often reach for these companions:

Conclusion

In conclusion, the 'jdtogregorian()' function in PHP is a useful tool for converting Julian dates to Gregorian dates. Understanding the difference between the two calendars and the significance of their introduction can provide insights into the development of timekeeping systems. We hope this article has provided you with a comprehensive understanding of the topic and helped you in your search for information.

Practice

Practice
What does the jdtoGregorian() function in PHP do?
What does the jdtoGregorian() function in PHP do?
Was this page helpful?