<?php

/*
   Signed 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.

   
$w Filter_Input(INPUT_POST'SubmitButton');

   if (IsSet(
$w))
      {
       
$StartBCAD  StrToUpper(trim(Filter_Input(INPUT_POST'StartBCAD')));
                     if (
$StartBCAD == '') {$StartBCAD 'AD';}
       
$StartBCAD  = (substr($StartBCAD,0,1) == 'B')? 'BC':'AD';

       
$StartYear  trim(Filter_Input(INPUT_POST'StartYear'));
                     if (
$StartYear == '') {$StartYear date('Y');}
                     
$StartYear SPrintF("%05d"abs($StartYear));

       
$StartMonth trim(Filter_Input(INPUT_POST'StartMonth'));
                     if (
$StartMonth == '') {$StartMonth date('m');}
                     
$StartMonth SPrintF("%02d"$StartMonth);

       
$StartDay   trim(Filter_Input(INPUT_POST'StartDay'));
                     if (
$StartDay == '') {$StartDay date('d');}
                     
$StartDay SPrintF("%02d"$StartDay);
      }
  else
      {
       
$StartBCAD  'AD';
       
$StartYear  date('Y');  $StartYear SPrintF("%05d"abs($StartYear));
       
$StartMonth date('m');
       
$StartDay   date('d');
      }

// Fill STOP variables with temporary empty ('') dummy values.
   
$StopBCAD $StopYear $StopMonth $StopDay '';
   
$JorG 'G';  $JorGStr 'Gregorian';

// Get start year value.
   
$StartY = ($StartBCAD == 'BC')? -$StartYear $StartYear;
   
$StartY SPrintF("%05d"abs($StartY));
   
$StartY = ($StartBCAD == 'BC')? "-$StartY$StartY;

// Construct full calendar date string.
   
$DateString "$StartBCAD $StartYear/$StartMonth/$StartDay";

// Construct numerical calendar date string.
   
$YmdString "$StartY/$StartMonth/$StartDay";

// Compute JD numbers for both calendar systems.
   
$JDNumJ JD_Num ($YmdString'J');
   
$JDNumG JD_Num ($YmdString'G');

// Determine calendar Day of Week abbreviation (Sun to Sat).
   
$DOWs    'SunMonTueWedThuFriSat';
   
$DoWStrJ substr($DOWs3*((7+($JDNumJ 1) % 7) %7),3);
   
$DoWStrG substr($DOWs3*((7+($JDNumG 1) % 7) %7),3);

// Compute difference in days between the calendar systems.
   
$JGDiff $JDNumG $JDNumJ;
   
$s = ($JGDiff <> 1)? 's' '';

   if (!
Is_Numeric($JDNumJ) or !Is_Numeric($JDNumG))
      {
       
$OutputJ $JDNumJ;
       
$OutputG $JDNumG;
      }
else
      {
       
$OutputJ "                 Calendar Date: BC|AD Yyyyy/mm/dd = $StartY/$StartMonth/$StartDay

               Gregorian Calendar JD Number = 
$JDNumG\tDoW = $DoWStrG";

       
$OutputG "              Old Julian Calendar JD Number = $JDNumJ\tDoW = $DoWStrJ
               Calendar Difference  (G &minus; J) = 
$JGDiff day$s";

      }

   print <<< END_OF_HTML_PAGE

<!DOCTYPE HTML>

<head>
<title>Signed 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;'>Long-Term Signed 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%;'>Calendar Date (Era, Yyyyy, mm, dd)<br>
<input name="StartBCAD"  type="text" size="3" maxlength="2" value="
$StartBCAD"  style='font-family:monospace; font-size:11pt;'>
<input name="StartYear"  type="text" size="6" maxlength="5" value="
$StartYear"  style='font-family:monospace; font-size:11pt;'><input name="StartMonth" type="text" size="3" maxlength="2" value="$StartMonth" style='font-family:monospace; font-size:11pt;'><input name="StartDay"   type="text" size="3" maxlength="2" value="$StartDay"   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;'>
$OutputJ
$OutputG
</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="79" rows="37" ReadOnly style='border:4px solid red;' OnDblClick='this.select();' OnMouseUp='return true;'>
/*
   ###########################################################################
   This PHP function computes the signed Julian Day Number on the old Julian
   or the modern Gregorian calendar system and can handle negative Julian Day
   numbers outside the range of the native built-in PHP JD number functions.
   Negative years represent BC years.

   NOTE: Year values can range from 1 to 5 digits long.

   Valid Example Arguments: 'sYyyyy/mm/dd'
   YmdStr = '-18191/05/25' or '1999/01/23' or '+17652/12/09' or '1/2/3'
*/
   function JD_Num (\$YmdString, \$JorG='G')
{
   \$YmdStr = trim(\$YmdString);
   list(\$Y,\$m,\$d) = PReg_Split("[\&#47;]", \$YmdStr);
   \$Y = trim(\$Y);   \$m = trim(\$m);     \$d = trim(\$d);

   if (!Is_Numeric(\$Y) or !Is_Numeric(\$m) or !Is_Numeric(\$d))
      {return FALSE;}

   \$JorG  = (StrToUpper(substr(trim(\$JorG),0,1)) == 'J')? 'J' : 'G';
   \$A     = floor((14-\$m) &#47; 12);
   \$B     = ((\$Y &lt; 0)? \$Y+1 : \$Y) - \$A;
   \$C     = floor(\$B&#47;100);
   \$JDNum = floor(30.6001*(12*\$A + \$m+1))+floor(365.25*(\$B+4716))-1524 + \$d;

   if (\$JorG == 'J') {return \$JDNum;}

   \$w = floor((((\$Y &lt; 0)? \$Y+1 : \$Y) - floor((14-\$m)&#47;12)) &#47; 100);
   \$JDNum += (floor(\$w&#47;4) - \$w + 2);

   return \$JDNum;

} &#47;&#47; END of  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 signed Julian Day Number on the old
   Julian or the modern Gregorian calendar system.

   It also handles proleptic dates prior to the mathematical origin of
   either calendar system and can handle negative Julian Day numbers
   outside the range of the native built-in PHP JD number functions.

   This generic function does NOT check for invalid dates except to see
   if the ('sYyyyy/mm/dd') date arguments are all numeric values.

   ARGUMENTS:
   Input argument strings use the format:
   YmdStr = 'sYyyyy/mm/dd'
   Examples: = '-18576/5/01'  or  '2026/05/20'  or  '19949/5/20'

   sYyyyy = Signed calendar year number <> 0
            -Negative = BC  and  +Positive = AD

       Span: '-20000/01/01'  to  '20000/12/31'
       There is no calendar year 0 (zero).

   mm = Month number (1 to 12).
   dd = Day number   (1 to 31).

   JorG = Calendar mode switch.
          'J' = Julian calendar
       or 'G' = Gregorian calendar = Default

   ERRORS:
   Returns FALSE if any of the ('Yyyyy/mm/dd')
   arguments are non-numeric.

   NO EXTERNAL DEPENDENCIES
*/

   
function JD_Num ($YmdString$JorG='G')
{
   
$YmdString trim($YmdString);
   list(
$Y,$m,$d) = PReg_Split("[\/]"$YmdString);
   
$Y trim($Y);    $m trim($m);    $d trim($d);

// -------------------------------------------------------
// Error if any of the (Y, m, d) elements are non-numeric.

   
if (
       !
Is_Numeric($Y)
   or  !
Is_Numeric($m)
   or  !
Is_Numeric($d)
      )
       {return 
FALSE;}


// ---------------------------------------------------
// Compute JD number according to the Julian calendar.

   
$A     floor((14-$m) / 12);
   
$B     = (($Y 0)? $Y+$Y) - $A;
   
$C     floor($B/100);
   
$JDNum floor(30.6001*(12*$A $m+1))+floor(365.25*($B+4716))-1524 $d;

// If the computation is for the Julian calendar, then we
// are done. Return the JD number for the Julian calendar.
   
if ($JorG == 'J') {return $JDNum;}

/* Otherwise,  apply this correction  to convert the JD number on the
   old Julian calendar into  the JD number for our Gregorian calendar.
   This correction may  equate to a negative  or positive value and is
   the difference in days between the two calendar systems on the same
   given date in the context: DiffDays = (GregJDNum - JulJDNum).
*/
   
$w floor(((($Y 0)? $Y+$Y) - floor((14-$m)/12)) / 100);
   
$JDNum += (floor($w/4) - $w 2);

   return 
$JDNum;

// END of  JD_Num(...)






?>