<?php

/*
   Inverse Julian Day Number Calculator
   For the span: BC 20000  to  AD 20000

   This program handles negative JD Numbers.

   LANGUAGE : PHP v8.2.12
   AUTHOR   : Jay Tanner - 2026
   LICENSE  : Public Domain
*/

   
$cYear date('Y');

   
$TWidth '700'// Table width in pixels.

// Get current PHP date elements.
   
$m date('m');
   
$d date('d');
   
$Y date('Y');

   
$w Filter_Input(INPUT_POST'SubmitButton');

   if (IsSet(
$w))
      {
       
$JDNum StrToUpper(trim(Filter_Input(INPUT_POST'JDNum')));
                if (
$JDNum == '') {$JDNum GregorianToJD($m$d$Y);}
                if (
Is_Numeric($JDNum)) {$JDNum floor($JDNum 0.5);}
      }
  else
      {
       
$JDNum GregorianToJD($m$d$Y);
      }

// Compute JD numbers for both calendar systems.
   
$DateStrJ Inv_JD_Num ($JDNum'J'TRUE);
   
$DateStrG Inv_JD_Num ($JDNum'G'TRUE);

// Determine calendar Day of Week abbreviation (Sun to Sat).
// The DoW is the same on both calendars for the same JD Number.
   
$DOWs     'SunMonTueWedThuFriSat';
   
$DoWStrJG substr($DOWs3*((7+($JDNum 1) % 7) %7),3);

   if (!
Is_Numeric($JDNum) or $DateStrJ === FALSE or $DateStrG === FALSE)
      {
$Output "ERROR: Invalid or out-of-range JD Number argument.";}

 else
      {
       
$Output "                      For Signed Julian Day Number: $JDNum

                Gregorian  Calendar Date  =  
$DateStrG
                Old Julian Calendar Date  =  
$DateStrJ

         The DoW is the same for the same JD Number on either calendar."
;
      }

   print <<< END_OF_HTML_PAGE

<!DOCTYPE HTML>
<HTML lang="en-us">

<head>
<title>Inverse Julian Day Number Calculator</title>

<style>
 BODY
{background:white; color:b; font-family:Verdana; font-size:11pt;}

 INPUT[type='text']::-ms-clear {width:0; height:0;}

 INPUT[type='text']
{
 background:white; color:black; font-family:monospace; font-size:13pt;
 font-weight:bold; text-align:center;
 border:2px solid black; border-radius:4px; box-shadow:2px 2px 5px #444444;
}

 INPUT[type='text']:focus
{
 font-family:monospace; background:white;
 font-size:13pt; border:2px solid blue; text-align:center; font-weight:bold;
 border-radius:4px; box-shadow:2px 2px 5px #444444;
}

 TD
{text-align:center; line-height:150%; background:white; color:black; padding:8px;}

 PRE, TEXTAREA
{
 background:white; color:black; font-family:monospace; font-size:11pt;
 font-weight:bold; text-align:left; line-height:125%; padding:6px;
 border:4px solid black; border-radius:8px; box-shadow:2px 2px 5px #444444;
 page-break-before:page;
}

 INPUT[type='submit']:hover
{background:GreenYellow; box-shadow:2px 2px 5px #444444; border-radius:4px;}

</style>

</head>

<body style='background:black;'>

<table width="
$TWidth" align='top'>

<tr><td style='color:white; background:#000066; border:2px solid white;
               border-radius:8px 8px 0px 0px;'>
<span style='font-size:16pt;'>Inverse Julian Day Number Calculator</span><br>
<span style='font-size:11pt;'>Span:   BC 20000 &nbsp;to&nbsp; AD 20000</span><br>
<span style='font-size:9pt;'>PHP Program by Jay Tanner - 
$cYear</span>
</td></tr>

</table>

<form name="Form1" method="post" action="">

<table bgcolor="black" align="top" border="0" cellspacing="1" width="
$TWidth">
<tr><td title='' style='line-height:175%;'>ENTER Signed JD Number<br>
<input name="JDNum"  type="text" size="20" maxlength="18" value="
$JDNum"  style='font-family:monospace; font-size:11pt;'>
</td></tr></table>


<table width="
$TWidth">
<tr><td style='text-align:center; line-height:200%;'><input name="SubmitButton" type="submit" value="S U B M I T"><br>
<b><a href="View-Source-Code.php" target="_blank"
style='font-family:Verdana; color:black; background:yellow;
       text-decoration:none; border:1px solid black; padding:4px;
       border-radius:4px; box-shadow:2px 2px 5px #444444;
       font-size:9pt; font-weight:normal;'>
&nbsp;View/Copy Source Code&nbsp;</a></b>
</td></tr>
</table>


<table align="top" border="0" width="
$TWidth">
<tr><td colspan="5">
<pre style='text-align:left;'>
$Output
</pre></td></tr>

<tr><td colspan="5" style='background:LightYellow;'>
The Julian Day Number numerically equates to the Julian Date at 12:00:00 Noon<br>and is always a signed integer value.
<pre>
Mathematical Calendar Origins:
BC 4713-Jan-01-Mon 12:00:00 = Origin date of Old Julian Calendar    JDNum = 0
BC 4714-Nov-24-Mon 12:00:00 = Origin date of  Gregorian Calendar    JDNum = 0

Julian Day Number  =  floor(Julian Date + 0.5)

For the calendar day of the week index (DoW = 0 to 6):
DoW = (7 + (JDNum + 1) mod 7) mod 7
Applies equally to signed JD numbers on either calendar system.

Where for DoW : 0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri and 6=Sat
</pre>
</td></tr></table>
</form>


<div style='color:GreenYellow; font-size:10pt; text-align:center; width:700px; line-height:150%;'><br>
<span style='font-size:10pt;'>Double-Click Within Text Area to Select ALL Code</span></div>
<textarea cols="81" rows="59" ReadOnly style='border:4px solid red;' OnDblClick='this.select();' OnMouseUp='return true;'>
/*
   ###########################################################################
   This function is the inverse of the JD_Num() function and returns the
   calendar date string in two possible 'Ymd' string formats as:
   '-1949/5/7'  or  'BC 01949-May-07'

   J|G Calendar Mode: &#39;J&#39; = Julian  or  &#39;G&#39; = Gregorian = Default

   FullDateFlag = FALSE = Ymd Format: '-1949/5/7' = Default format
                = TRUE  = Ymd Format: 'BC 01949-May-07-Tue'

   ERRORS:
   Returns FALSE on error = Non-numeric JD number or out of range JD number.
   ###########################################################################
*/

   function Inv_JD_Num (\$JDNumber, \$JorG=&#39;G&#39;, \$FullDateFlag=FALSE)
{
   if (!Is_Numeric(trim(\$JDNumber))) {return FALSE;}

   \$JDNum = floor(trim(\$JDNumber) + 0.5);
   \$JorG  = substr(StrToUpper(trim(\$JorG)),0,1);
   \$JorG  = (\$JorG == &#39;J&#39;)? &#39;J&#39;:&#39;G&#39;;
   \$CAdj  = (\$JorG == &#39;J&#39;)?   0:1;

   if (\$JorG == &#39;J&#39;)
      {if (\$JDNum &lt; -5583576 or \$JDNum &gt; 9026423) {return FALSE;}}

   if (\$JorG == &#39;G&#39;)
      {if (\$JDNum &lt; -5583424 or \$JDNum &gt; 9026275) {return FALSE;}}

  \$A = floor(\$JDNum + 0.5);
  \$B = \$CAdj*floor((\$A - 1867216.25) / 36524.25);
  \$C = \$A + \$CAdj*(\$B - floor(\$B/4) + 1);
  \$D = \$C + 1524;
  \$E = floor((\$D - 122.1) / 365.25);
  \$F = floor(365.25 * \$E);
  \$G = floor((\$D - \$F) / 30.6001);
  \$d = \$D - \$F - floor(30.6001 * \$G);     // Day num   (1 to 31)
  \$m = \$G - 12*floor(\$G/14) - 1;          // Month num (1 to 12)
  \$y = \$E - 4716 + floor((14 - \$m) / 12); // Mathematical year (y)
  \$Y = (\$y &gt; 0)? \$y : \$y-1;               // Calendar year (Y) -Neg = BC

  if (\$FullDateFlag)
 {
  \$AbsY   = SPrintF("%05d", abs(\$Y));
  \$Y      = (\$Y &lt; 0)? "BC \$AbsY" : "AD \$AbsY";
  \$d      = SPrintF("%02d", \$d);
  \$Mmm    = substr(&#39;JanFebMarAprMayJunJulAugSepOctNovDec&#39;, 3*(\$m-1), 3);
  \$DoW    = (7 + (\$JDNum + 1) % 7) % 7;
  return "\$Y-\$Mmm-\$d-" . substr(&#39;SunMonTueWedThuFriSat&#39;, 3*\$DoW, 3);
 }
  return "\$Y/\$m/\$d";

} // END of  Inv_JD_Num()



</textarea>

<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>
</body>

</HTML>

END_OF_HTML_PAGE;






/*
   This PHP function computes the calendar date elements
   corresponding to any given signed Julian Day Number.

   NO EXTERNAL DEPENDENCIES

   ERRORS:
   Returns FALSE on error = Non-numeric JD number or out of range JD number.
   ###########################################################################
*/

   
function Inv_JD_Num ($JDNumber$JorG='G'$FullDateFlag=FALSE)
{
   if (!
Is_Numeric(trim($JDNumber))) {return FALSE;}

   
$JDNum floor(trim($JDNumber) + 0.5);
   
$JorG  substr(StrToUpper(trim($JorG)),0,1);
   
$JorG  = ($JorG == 'J')? 'J':'G';
   
$CAdj  = ($JorG == 'J')?   0:1;

   if (
$JorG == 'J')
      {if (
$JDNum < -5583576 or $JDNum 9026423) {return FALSE;}}

   if (
$JorG == 'G')
      {if (
$JDNum < -5583424 or $JDNum 9026275) {return FALSE;}}

  
$A floor($JDNum 0.5);
  
$B $CAdj*floor(($A 1867216.25) / 36524.25);
  
$C $A $CAdj*($B floor($B/4) + 1);
  
$D $C 1524;
  
$E floor(($D 122.1) / 365.25);
  
$F floor(365.25 $E);
  
$G floor(($D $F) / 30.6001);
  
$d $D $F floor(30.6001 $G);     // Day num   (1 to 31)
  
$m $G 12*floor($G/14) - 1;          // Month num (1 to 12)
  
$y $E 4716 floor((14 $m) / 12); // Mathematical year (y)
  
$Y = ($y 0)? $y $y-1;               // Calendar year (Y) -Neg = BC

  
if ($FullDateFlag)
 {
  
$AbsY   SPrintF("%05d"abs($Y));
  
$Y      = ($Y 0)? "BC $AbsY"AD $AbsY";
  
$d      SPrintF("%02d"$d);
  
$Mmm    substr('JanFebMarAprMayJunJulAugSepOctNovDec'3*($m-1), 3);
  
$DoW    = (+ ($JDNum 1) % 7) % 7;
  return 
"$Y-$Mmm-$d-" substr('SunMonTueWedThuFriSat'3*$DoW3);
 }
  return 
"$Y/$m/$d";

// END of  Inv_JD_Num()






?>