Inverse Julian Date Function

When we have the JD (Julian Date) of an event, we need a tool to translate the JD value into the corresponding calendar date and time.  This is the opposite of the Julian Date function.

Go to: START  |  PROGRAMS INDEX  |  Julian Date Function


NOTE:
There is a glitch in the native PHP inverse JD Number functions that returns '0/0/0' as the date ('m/d/Y') when the JD value equates to zero. The function compensates for that glitch and returns the correct origin dates when (JD == 0).

The returned date string format will be similar to: 

'G 1949-May-20-Fri 08:52:07.713'

Given the Julian Date value and calendar mode, this function will return the corresponding date and time string.

/*
   ----------------------------------------------------------------
   Given a general JD (Julian Date) and the calendar mode where
   'J' = Julian, 'G'=Gregorian, this function will return the
   corresponding date and time elements (y-Mmm-d-DoW HH:mm:ss.sss).

   Julian Calendar Origin
   BC 4713-Jan-01-Mon = JD Number 0  - On the Julian Calendar

   Gregorian Calendar Origin
   BC 4714-Nov-24-Mon = JD Number 0  - On the Gregorian Calendar
   
   JD = Julian Date
   CalMode = 'J' for Julian or 'G' for Gregorian = Default 
   ----------------------------------------------------------------   
*/

   function Inv_JD ($JD, $CalMode='G')
{
   $JD = trim($JD);
   $JG = substr(StrToUpper(trim($CalMode)),0,1);

// Compute JD Number from Julian Date.
   $JDNum = floor($JD + 0.5);

// Get calendar date elements (m,d,Y) according to calendar mode.
   $mdY = ($JG == 'J')? JDtoJulian($JDNum) : JDtoGregorian($JDNum);

   list($m,$d,$Y) = Preg_Split("[\/]", $mdY);

// Construct calendar date string.
   $DoW = JDDayOfWeek($JDNum, 2);

   if ($JG == 'J')
      {$Mmm = JDMonthName($JDNum, CAL_MONTH_JULIAN_SHORT);}
   else
      {$Mmm = JDMonthName($JDNum, CAL_MONTH_GREGORIAN_SHORT);}
      
   $DateStr = "$Y-$Mmm-". SPrintF("%02d", $d) . "-$DoW";

// Handle special case where (JDNum == 0).
   if ($JDNum == 0 and $JG == 'J') {$DateStr = '-4713-Jan-01-Mon';}
   if ($JDNum == 0 and $JG == 'G') {$DateStr = '-4714-Nov-14-Mon';}

// Parse the time elements (HH,mm,ss):
   $hrs = 24*($JD - floor($JD + 0.5) + 0.5);
   $HH  = floor($hrs);
   $min = 60*($hrs - $HH);
   $mm  = floor($min);
   $sec = 60*($min - $mm);

// Account for that blasted (ss == 60) glitch.
   $ss = SPrintf("%1.3f", $sec);
   if ($ss == 60.0) {$mm += 1; $ss=0;}
   if ($mm == 60.0) {$HH += 1; $mm=0;}

// Construct time elements ('HH:mm:ss.sss') string.
   $HH = SPrintF("%02d", $HH);
   $mm = SPrintF("%02d", $mm);
   $ss = SPrintF("%06.3f", $sec);
   $TimeStr = "$HH:$mm:$ss";

   $JDStr = SPrintF("%1.8f", $JD);

   return "$JG $JDStr $DateStr $TimeStr";
}