PHP jdtounix() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Calendar

Definition

In PHP, jdtounix() converts a Julian Day number into a Unix timestamp, the number of seconds since January 1, 1970.

The Unix timestamp that you get is for midnight (00:00:00) UTC at the start of that day. This lets you move from calendar-style date math to normal PHP functions like date() and gmdate().

Syntax

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

PHP
$timestamp = jdtounix($jd);
CallWhat it does
jdtounix(2440588)Returns 0, the Unix epoch (January 1, 1970).
jdtounix(gregoriantojd(1, 1, 2024))Returns 1704067200, midnight UTC on January 1, 2024.
jdtounix(2440587)Throws a ValueError because the day is before 1970.

Use gmdate() to print the result, because the timestamp is based on UTC midnight. Using date() can show a different time depending on your time zone.

Library declarations
PHP
jdtounix(int $julian_day): int

Parameters

ParameterDescription
julian_dayRequired. The Julian Day number to convert. It must be 2440588 (January 1, 1970) or later, up to a limit that depends on your PHP build.

Return Value

Return ValueDescription
intThe Unix timestamp for midnight UTC of that day. A Julian Day outside the valid range throws a ValueError in PHP 8.

PHP Examples

Example 1 — Julian Day to Timestamp

Convert the Unix epoch day and January 1, 2024, then print them with gmdate().

PHP
<?php
echo jdtounix(2440588), PHP_EOL;

$timestamp = jdtounix(gregoriantojd(1, 1, 2024));

echo $timestamp, PHP_EOL;
echo gmdate('Y-m-d H:i:s', $timestamp), PHP_EOL;

How It Works

  1. Julian Day 2440588 is the Unix epoch, January 1, 1970, so the timestamp is 0.
  2. gregoriantojd() turns January 1, 2024 into a Julian Day number.
  3. jdtounix() returns 1704067200, the number of seconds since 1970.
  4. gmdate() prints the timestamp as 2024-01-01 00:00:00 in UTC.

Example 2 — Before the Epoch

Julian Days before 1970 cannot become a Unix timestamp. Catch the ValueError to handle it.

PHP
<?php
echo jdtounix(2440588), PHP_EOL;

try {
    echo jdtounix(2440587), PHP_EOL;
} catch (ValueError $e) {
    echo 'Day is before 1970', PHP_EOL;
}

How It Works

  1. 2440588 is the first valid Julian Day, and it returns 0.
  2. 2440587 is one day earlier, which is December 31, 1969.
  3. PHP 8 throws a ValueError for that Julian Day.
  4. The catch block prints a short message instead of stopping the script.

Example 3 — Seconds Between Two Days

Convert two Julian Days to timestamps and subtract them to get seconds, then divide to get days.

PHP
<?php
$start = jdtounix(gregoriantojd(1, 1, 2024));
$end = jdtounix(gregoriantojd(12, 31, 2024));

echo 'Seconds: ', $end - $start, PHP_EOL;
echo 'Days: ', ($end - $start) / 86400, PHP_EOL;

How It Works

  1. Each date is converted to a Julian Day and then to a timestamp.
  2. Subtracting the timestamps gives the time between the two midnights in seconds.
  3. A day has 86400 seconds, so dividing by 86400 gives the number of days.
  4. From January 1 to December 31 in a leap year is 365 days.

Frequently Asked Questions

It converts a Julian Day number into a Unix timestamp for midnight UTC on that day. You can then use normal PHP date functions on it.
The Julian Day is outside the allowed range. It must be 2440588 (January 1, 1970) or later, because Unix timestamps start at that moment.
Use gmdate(). The timestamp is for midnight UTC, and date() may show a different date or time if your server is in another time zone.
unixtojd() goes the other way. It takes a Unix timestamp and returns the Julian Day number for that day.
Yes. If PHP says jdtounix() is undefined, enable extension=calendar in php.ini or install the php-calendar package.

Next: jewishtojd()

jewishtojd() converts a Jewish (Hebrew) date into a Julian Day number.

jewishtojd() 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