PHP unixtojd() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Calendar

Definition

In PHP, unixtojd() converts a Unix timestamp into a Julian Day number.

It is the reverse of jdtounix(). If you leave out the timestamp, PHP uses the current time, which turns the function into a quick way to get today's Julian Day. The calendar day is worked out using the server's local time zone.

Syntax

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

PHP
$jd = unixtojd(1704110400);
CallWhat it does
unixtojd(1704110400)Returns 2460311, the Julian Day for January 1, 2024.
unixtojd(1704110400 + 86400)Returns 2460312, the next day.
unixtojd()Returns the Julian Day for the current moment.

The time of day is dropped, only the date matters. Because the date is calculated in the server's local time zone, the examples here use a timestamp around noon UTC so the answer is the same in most time zones.

Library declarations
PHP
unixtojd(?int $timestamp = null): int|false

Parameters

ParameterDescription
timestampOptional. A Unix timestamp, in seconds. If it is null or omitted, the current time is used. A negative timestamp throws a ValueError.

Return Value

Return ValueDescription
intThe Julian Day number for the day that the timestamp falls on.
falseReturned if the local time cannot be worked out for the timestamp.

PHP Examples

Example 1 — Timestamp to Julian Day

Convert a fixed timestamp for noon UTC on January 1, 2024 and look at the date it represents.

PHP
<?php
$jd = unixtojd(1704110400);

echo $jd, PHP_EOL;
echo jdtogregorian($jd), PHP_EOL;
echo jddayofweek($jd, CAL_DOW_LONG), PHP_EOL;

How It Works

  1. The timestamp 1704110400 is noon (12:00) UTC on January 1, 2024.
  2. unixtojd() drops the time and returns the Julian Day number 2460311.
  3. jdtogregorian() turns that number into the text 1/1/2024.
  4. jddayofweek() with mode 1 shows that the day was a Monday.

Example 2 — Consecutive Days and Negative Timestamps

Adding 86400 seconds moves the Julian Day forward by exactly one. A negative timestamp is not allowed.

PHP
<?php
$ts = 1704110400;

echo unixtojd($ts), PHP_EOL;
echo unixtojd($ts + 86400), PHP_EOL;

try {
    unixtojd(-1);
} catch (ValueError $e) {
    echo 'Negative timestamp', PHP_EOL;
}

How It Works

  1. A day has 86400 seconds, so adding that many moves to the next calendar day.
  2. The Julian Day number goes up from 2460311 to 2460312.
  3. A negative timestamp is before 1970, which this function does not support.
  4. PHP 8 throws a ValueError, which we catch and answer with a short message.

Example 3 — A Round Trip with jdtounix()

Convert a timestamp to a Julian Day and back. You get midnight UTC of the same day, not the original time.

PHP
<?php
$ts = 1704110400;

$jd = unixtojd($ts);
$midnight = jdtounix($jd);

echo $jd, PHP_EOL;
echo $midnight, PHP_EOL;
echo $ts - $midnight, PHP_EOL;
echo gmdate('Y-m-d H:i', $midnight), PHP_EOL;

How It Works

  1. unixtojd() reduces the timestamp to the Julian Day number for its date.
  2. jdtounix() converts that day back to a timestamp for midnight UTC.
  3. The difference is 43200 seconds, which is the 12 hours that were dropped.
  4. gmdate() confirms that the result is midnight on January 1, 2024.

Frequently Asked Questions

It converts a Unix timestamp into the Julian Day number of the day that the timestamp is in. The time of day is ignored.
PHP uses the current time, so you get today's Julian Day number. This changes daily, so avoid it in tests.
The day is worked out using the server's local time zone. A timestamp close to midnight UTC can fall on the previous or next day in another time zone.
jdtounix() goes the other way. It converts a Julian Day number into a timestamp for midnight UTC of that day.
Yes. If PHP says unixtojd() is undefined, enable extension=calendar in php.ini or install the php-calendar package.

Back to Calendar Functions

Browse all PHP calendar function tutorials.

Calendar functions →

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