PHP gregoriantojd() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Calendar

Definition

In PHP, gregoriantojd() converts a Gregorian date into a Julian Day number.

A Julian Day number is just a count of days, so once your dates are numbers you can subtract them, add days to them, or convert them into other calendars. This is the most common starting point for all the other calendar functions.

Syntax

Call gregoriantojd() with the values you need. Start with this line.

PHP
$jd = gregoriantojd(1, 1, 2000);
CallWhat it does
gregoriantojd(1, 1, 2000)Returns 2451545, the Julian Day for January 1, 2000.
gregoriantojd(12, 25, 2024)Returns 2460670, the Julian Day for December 25, 2024.
gregoriantojd(13, 1, 2024)Returns 0 because month 13 is not valid.

Mind the order: month, day, year. This is not the same as the day-month-year order used in many countries. Use checkdate() first if the numbers come from a user.

Library declarations
PHP
gregoriantojd(int $month, int $day, int $year): int

Parameters

ParameterDescription
monthRequired. The month as a number from 1 to 12.
dayRequired. The day of the month as a number from 1 to 31.
yearRequired. The year, such as 2024. Year 0 does not exist, and negative years count as BC.

Return Value

Return ValueDescription
intThe Julian Day number for the date. It returns 0 if the month, day or year is not valid.

PHP Examples

Example 1 — Convert Gregorian Dates

Turn a few ordinary dates into Julian Day numbers and convert one back.

PHP
<?php
echo gregoriantojd(1, 1, 2000), PHP_EOL;
echo gregoriantojd(1, 1, 2024), PHP_EOL;
echo gregoriantojd(12, 25, 2024), PHP_EOL;
echo jdtogregorian(gregoriantojd(1, 1, 2000)), PHP_EOL;

How It Works

  1. Each call takes the month first, then the day, then the year.
  2. January 1, 2000 becomes 2451545, and January 1, 2024 becomes 2460311.
  3. December 25, 2024 is 359 days after January 1, 2024, so its number is 2460670.
  4. jdtogregorian() reverses the conversion and returns the text 1/1/2000.

Example 2 — Invalid Input Returns 0

When the month or year is not valid, gregoriantojd() returns 0. Use checkdate() to test the full date first.

PHP
<?php
var_dump(gregoriantojd(13, 1, 2024));
var_dump(gregoriantojd(1, 1, 0));
var_dump(gregoriantojd(0, 10, 2024));
var_dump(checkdate(2, 29, 2023));

How It Works

  1. Month 13 is not a real month, so the result is 0.
  2. The Gregorian calendar has no year 0, so this call also returns 0.
  3. Month 0 is not valid either.
  4. checkdate() is a safer test for real dates: February 29, 2023 does not exist, so it returns false.

Example 3 — Date Math with Julian Days

Subtract Julian Day numbers to count days, and see how a leap year changes the result.

PHP
<?php
$leap = gregoriantojd(3, 1, 2024) - gregoriantojd(2, 28, 2024);
$normal = gregoriantojd(3, 1, 2023) - gregoriantojd(2, 28, 2023);

echo '2024: ', $leap, PHP_EOL;
echo '2023: ', $normal, PHP_EOL;

$days = gregoriantojd(1, 1, 2024) - gregoriantojd(1, 1, 2000);
echo 'Days from 2000 to 2024: ', $days, PHP_EOL;
echo 'Years: ', round($days / 365.25), PHP_EOL;

How It Works

  1. In 2024 there is a February 29, so March 1 is two days after February 28.
  2. In 2023 there is no February 29, so March 1 is only one day after February 28.
  3. The difference between two Julian Days is the exact number of days between the dates: 8766.
  4. Dividing by 365. 25 (the average year length) and rounding gives 24 years.

Frequently Asked Questions

It converts a Gregorian date (month, day, year) into a Julian Day number. That number is a simple count of days, so it is great for date math.
The order is month, day, year. If you pass the day first, you will get the wrong result, or 0 if the day is above 12 and treated as a month.
Use jdtogregorian(). It returns text in the form month/day/year, for example 1/1/2000.
The month, day or year is out of range. For example, month 13 and year 0 are not valid. Always check your input, and consider checkdate() for a full check.
Yes. It is part of the calendar extension. Enable extension=calendar in php.ini or install the php-calendar package if the function is undefined.

Next: jddayofweek()

jddayofweek() returns the day of the week for a Julian Day number, either as a number or a.

jddayofweek() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful