In PHP, cal_to_jd() converts a date in a chosen calendar into a Julian Day number, which is a simple count of days.
It works as a single entry point for all four supported calendars. You pass the calendar, then the month, day and year, and you get a number back. Once you have Julian Day numbers, comparing dates or counting the days between them is just subtraction.
Syntax
Call cal_to_jd() with the values you need. Start with this line.
PHP
$jd = cal_to_jd(CAL_GREGORIAN, 1, 1, 2000);
Call
What it does
cal_to_jd(CAL_GREGORIAN, 1, 1, 2000)
Returns 2451545, the Julian Day for January 1, 2000.
cal_to_jd(CAL_JULIAN, 12, 19, 1999)
Returns 2451545 too, because it is the same day in the Julian calendar.
cal_to_jd(CAL_GREGORIAN, 13, 1, 2024)
Returns 0 because month 13 is not valid.
Notice the argument order: calendar, month, day, year. Month comes before day, which is the American order. For Gregorian-only code, gregoriantojd() does the same job with one argument less.
Library declarations
PHP
cal_to_jd(int $calendar, int $month, int $day, int $year): int
Parameters
Parameter
Description
calendar
Required. The calendar of the input date: CAL_GREGORIAN (0), CAL_JULIAN (1), CAL_JEWISH (2) or CAL_FRENCH (3). Any other value throws a ValueError.
month
Required. The month number in that calendar, such as 1 to 12 for the Gregorian calendar.
day
Required. The day of the month.
year
Required. The year in that calendar. Year 0 is not valid for the Gregorian and Julian calendars.
Return Value
Return Value
Description
int
The Julian Day number for the date. It returns 0 if the date is not valid for that calendar.
PHP Examples
Example 1 — Convert Dates from Four Calendars
Convert a Gregorian date first, then the same day in the Julian calendar, and then one date each from the Jewish and French calendars.
Start JD: 2460311
End JD: 2460670
Days between: 359
Full weeks: 51
How It Works
Each date is converted to a Julian Day number, which is just an integer.
Subtracting the two integers gives the number of days between the dates.
From January 1 to December 25 in a leap year is 359 days.
intdiv() divides by 7 and drops the remainder, giving 51 full weeks.
Frequently Asked Questions
It converts a date from a chosen calendar into a Julian Day number. This number counts days, so it is easy to compare, add and subtract.
The order is calendar, month, day, year. Many beginners put the day first by mistake, which gives a wrong date or a zero result.
A result of 0 means the date is not valid in that calendar, for example month 13 in the Gregorian calendar or year 0. Check your numbers before calling the function.
gregoriantojd() only handles Gregorian dates. cal_to_jd() handles Gregorian, Julian, Jewish and French dates by taking an extra calendar argument.
Yes. Convert both dates to Julian Day numbers and subtract the smaller one from the larger one. The result is the number of days between them.