gregoriantojd()
The Gregorian calendar, also known as the Western calendar or the Christian calendar, is a solar calendar that was introduced in 1582 by Pope Gregory XIII. It
Introduction
The PHP gregoriantojd() function converts a date in the Gregorian calendar to a Julian Day Count — a single integer that represents a date by counting the days elapsed since a fixed starting point. This page explains what those terms mean, shows the function's signature and return values, walks through a working example, and links to the inverse and related calendar functions.
gregoriantojd() is part of PHP's bundled Calendar extension. It is not deprecated, but most modern projects only need Julian Day Counts for astronomy, interoperability with scientific data, or converting between calendar systems. For everyday date arithmetic and formatting, reach for the DateTime class instead.
History of the Gregorian Calendar
The Gregorian calendar was introduced as a way to correct the errors in the Julian calendar. The Julian calendar was based on the idea that a year was exactly 365.25 days long. However, this was not entirely accurate, and the calendar gradually fell out of sync with the solar year. By the time the Gregorian calendar was introduced, the Julian calendar was off by 10 days.
The Gregorian calendar solved this problem by introducing a new system for calculating leap years. In the Gregorian calendar, a year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400. This means that years like 1700, 1800, and 1900 are not leap years, but 1600 and 2000 are.
Syntax
gregoriantojd(int $month, int $day, int $year): intIt takes three integer arguments in month, day, year order — note that the month comes first, not the day:
$month— the month,1(January) through12(December).$day— the day of the month,1through31.$year— the year. The valid range is-4714to9999. The year0is not valid.
The function returns the Julian Day Count as an integer, or 0 if the date is out of range.
What Is a Julian Day Count?
A Julian Day Count is a way to represent any calendar date as a single, ever-increasing integer. The count starts from noon on January 1, 4713 BCE (in the proleptic Julian calendar). Because every date becomes one number, you can find the number of days between two dates with simple subtraction, regardless of months, leap years, or calendar boundaries — which is exactly why astronomers and scientific data formats use it.
Converting a Gregorian Date
To convert February 14, 2023 to a Julian Day Count, pass the month, day, and year:
$jd = gregoriantojd(2, 14, 2023);
echo $jd; // 2459990The $jd variable now holds 2459990, the Julian Day Count for that date.
Handling invalid dates
If the date is out of the supported range, gregoriantojd() returns 0 rather than throwing an error. Treat a 0 result as "invalid input":
$jd = gregoriantojd(13, 40, 2023); // month 13, day 40 — invalid
if ($jd === 0) {
echo "Invalid date.";
}Converting back
To go the other way — from a Julian Day Count back to a Gregorian date string — use the inverse function jdtogregorian():
$jd = gregoriantojd(2, 14, 2023);
echo jdtogregorian($jd); // 2/14/2023When Would I Use This?
Reach for gregoriantojd() when you need to:
- Count days between two dates by subtracting their Julian Day Counts.
- Convert between calendar systems (for example, Gregorian to Julian or Jewish dates) using PHP's other Calendar functions.
- Find the day of the week for a date with
jddayofweek().
For ordinary application work — parsing user input, formatting output, adding intervals — prefer the DateTime class, which is timezone-aware and far more convenient.
Conclusion
gregoriantojd() converts a Gregorian date into a single Julian Day Count integer, making date math and cross-calendar conversion straightforward. Remember the month-first argument order, check for a 0 return on invalid input, and pair it with jdtogregorian() to convert back. For general-purpose date handling, the DateTime class remains the right tool.