Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julian day from DateTime

Tags:

php

Is there a better way of converting a DateTime object to a Julian day number than something like this:

$jd = GregorianToJD( $dt->format('n'), $dt->format('j'), $dt->format('Y') );

I'm a bit surprised there isn't either a function to do it directly or a format specifier to print it. Am I missing something obvious?

Edit: Many of the (somewhat sarcastic) replies below seem to be assuming the Julian day number is somehow related to the long-obsolete Julian calendar. It isn't. The Julian day number is one of the most common ways of numbering days from a fairly arbitrary epoch. Its main use is in astronomy where it is the standard way of identifying a date.

like image 344
Richard Smith Avatar asked Jan 17 '15 21:01

Richard Smith


1 Answers

This should do the trick:

// $dt = new DateTime();
$jd = unixtojd( $dt->getTimestamp());

See more on unixtojd on PHP.net

like image 68
moorscode Avatar answered Sep 30 '22 19:09

moorscode