computing_the_date_and_time_corresponding_to_any_jd
Computing the Date and Time Corresponding to Any General JD (Julian Date)
Go to: START | PROGRAMS INDEX | Julian Date Function | Inverse Julian Date Function
This demo PHP program shows how to obtain the date and time from any given JD (Julian Date) value on either the old Julian or the modern Gregorian calendar. To keep the process simple to follow, no specific error checking is done for invalid arguments and the comments help to explain what's going on.
This is the process used within the custom Inverse Julian Date Function.
<?php // -------------------------------------- // Set a general Julian Date demo value. $JD = 2433056.78214713; // ----------------------------------------------------------- // Set calendar mode, where 'J' = Julian and 'G' = Gregorian). // Logic: If not Julian, then Gregorian calendar, by default. $CalMode = 'G'; // --------------------------------------------------- // Compute the Julian Day Number from the Julian Date. $JDNum = floor($JD + 0.5); // --------------------------------------------------------------------- // Compute the month, day and year values corresponding to the JD Number // and store them into their respective variables. $mdy = (StrToUpper($CalMode == 'J'))? JDtoJulian($JDNum) : JDtoGregorian($JDNum); list($m,$d,$y) = PReg_Split("[\/]", $mdy); // ------------------------------------ // Get the 3-letter month abbreviation. $MmmStr = (StrToUpper($CalMode == 'J'))? JDMonthName($JD, CAL_MONTH_JULIAN_SHORT) : JDMonthName($JD, CAL_MONTH_GREGORIAN_SHORT); // ------------------------------------ // Get the 3-letter month abbreviation. $DoWStr = JDDayOfWeek($JDNum, 2); // --------------------------------------------- // Get the number of days in the calendar month. $mDays = (StrToUpper($CalMode == 'J'))? Cal_Days_In_Month(CAL_JULIAN, $m, $y) : Cal_Days_In_Month(CAL_GREGORIAN, $m, $y); $DateStr = "$y-$MmmStr-". SPrintF("%02d", $d) . "-$DoWStr"; // =========================================== // The date elements have now been determined. // =========================================== // The next step is to determine the time of day // from the general JD (Julian Date) value. // ----------------------------------- // Parse the time elements (HH,mm,ss), // where HH = 00 to 24 hours. $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 formatted time elements string 'HH:mm:ss.sss'). $HH = SPrintF("%02d", $HH); $mm = SPrintF("%02d", $mm); $ss = SPrintF("%06.3f", $sec); $TimeStr = "$HH:$mm:$ss"; // --------------------------------------- // Format Julian Date value to 8 decimals. $JDStr = SPrintF("%1.8f", $JD); // ------------------------------------ // Construct full date and time string. $FullDateTimeStr = "$CalMode $JDStr $DateStr $TimeStr"; // *************************** // *************************** // Print out the demo results. print "<pre> CalMode = '$CalMode' JD = $JD JDNum = $JDNum mdy = '$mdy' m = $m d = $d y = $y MmmStr = '$MmmStr' DoWStr = '$DoWStr' mDays = $mDays DateStr = '$DateStr' TimeStr = '$TimeStr' FullDateTimeStr = '$FullDateTimeStr' </pre>"; ?>
Running the demo program above should print the following output:
CalMode = 'G' JD = 2433056.78214713 JDNum = 2433057 mdy = '5/20/1949' m = 5 d = 20 y = 1949 MmmStr = 'May' DoWStr = 'Fri' mDays = 31 DateStr = '1949-May-20-Fri' TimeStr = '06:46:17.512' FullDateTimeStr = 'G 2433056.78214713 1949-May-20-Fri 06:46:17.512'
computing_the_date_and_time_corresponding_to_any_jd.txt · Last modified: 2023/01/09 04:19 by jaywiki