##############################################################################
PHP FUNCTIONS TO COMPUTE SIGNED JULIAN DAY NUMBERS FOR DATES PRIOR TO BC 4713

CUSTOM SYMBOLS USED HERE:

The calendar functions are intended to span the range from BC 20000 up to the
year AD 20000 on both the old Julian and modern Gregorian calendars.

sYmmdd = Signed numerical date string in same format as 'sY/mm/dd'
         VALID EXAMPLES: '1949/5/20'  or  '-15008/05/02'  or  '01066/1/23'

sY = Signed calendar year value. (-20000 ≤ sY ≤ 20000)
mm = Month number (1 to 12)
dd = Day Number   (1 to 31, depending on the month)

JDNum or sJDNum = Signed Julian Day Number (always a signed integer).

JDNumJ = Julian Day Number according to old Julian calendar.
JDNumG = Julian Day Number according to modern Gregorian calendar.

JorG = Calendar Mode ('J' = Julian  or 'G' = Gregorian = Default)

--------------------------------------------
The custom PHP functions included below are:

JD_Num (sYmmdd, JorG='G')

Inv_JD_Num (sJDNum, JorG='G', RetMode=1)

Is_Valid_Date (sYmmdd, JorG='G')

Is_Leap_Year (sYearNum, JorG='G')

JDNum_to_DoW_Str (sJDNum)

GJ_Cal_Days_Diff (sYmmdd)

##############################################################################
##############################################################################
COMPUTING THE JULIAN DAY NUMBER IN PHP

The native Julian Day Number functions of PHP do not handle negative Julian
Day Numbers.  For some astronomical computations, this is problematic for
certain theoretical applications that need to span farther back in time.

This function takes a date argument string and returns the signed JD Number.
Negative years are taken as BC years.
There is no calendar year 0 (zero).

NOTE:
This function does NOT check for invalid date string arguments.

Date string arguments take the general form: $sYmmss = 'sY/mm/dd'

sY = Signed calendar year value. (-20000 ≤ sY ≤ 20000)
mm = Month number (1 to 12)
dd = Day number   (1 to 31, depending on the month)

EXAMPLES:
$JDNum = JD_Num('7589/5/20',   'G'); // --> JDNum =  4493025 Gregorian
$JDNum = JD_Num('-12949/5/20', 'J'); // --> JDNum = -3008059 Julian

The function code below handles negative Julian Day numbers for dates prior
to BC 4713-Jan-01-Mon  on the old Julian calendar  or BC 4714-Nov-24-Mon on
our modern Gregorian calendar.



// ---------------------------------------------------------------------------

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

   $JorG = substr(StrToUpper(trim($JorG)),0,1);
   $JorG = ($JorG == 'J')? 'J' : 'G';

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

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

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

   $w = floor(((($sY < 0)? $sY+1 : $sY) - floor((14-$m)/12)) / 100);
   $JDNum += (floor($w/4) - $w + 2);

   return $JDNum;

} // END OF  JD_Num (...)

// ---------------------------------------------------------------------------





##############################################################################
##############################################################################
Sometimes we may know the Julian Day Number but not the calendar date elements
(Year, Month, Day) corresponding to it.

The function below solves this inverse Julian Day Number problem and handles
negative Julian Day Numbers. Given the signed Julian Day Number, it returns
the calendar date elements (sY,mm,dd) in either one of two string formats
in descending ISO time units size sequence from left to right.

Examples of Returned Date Strings:
(-2000622, 'J', 1) --> '-10191/8/6'
(5443386,  'G', 2) --> 'AD 10191-May-20-Sun'

JDNum = Signed Julian Day Number (Always a Signed Integer)

JorG  = Calendar Mode
        G = Gregorian Calendar = Default
        J = Julian Calendar

RetMode = Return Mode
      1 = Return a numeric date string  (e.g. '-10191/05/20' = Default)
      2 = Return full date string       (e.g. 'BC 10191-May-20-Sun')




// ---------------------------------------------------------------------------

   function Inv_JD_Num ($sJDNum, $JorG='G', $RetMode=1 )
{
   if (!Is_Numeric(trim($sJDNum))) {return FALSE;}

   $JDNum = floor(trim($sJDNum) + 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 ($RetMode == 2)
 {
  $AbsY   = SPrintF("%05d", abs($Y));
  $Y      = ($Y < 0)? "BC $AbsY" : "AD $AbsY";
  $d      = SPrintF("%02d", $d);
  $Mmm    = substr('JanFebMarAprMayJunJulAugSepOctNovDec', 3*($m-1), 3);
  $DoW    = (7 + ($JDNum + 1) % 7) % 7;
  return "$Y-$Mmm-$d-" . substr('SunMonTueWedThuFriSat', 3*$DoW, 3);
 }
  return "$Y/$m/$d";

} // END of  Inv_JD_Num()

// ---------------------------------------------------------------------------





##############################################################################
##############################################################################
The Julian Day Number function does NOT check for invalid date arguments. So,
a special function was designed for this purpose and is listed below.

The following boolean function will check a date string argument for validity
and return TRUE if valid or FALSE if invalid.  It requires both the JD Number
and Inverse JD Number functions defined above as sub-dependencies.

sYmmdd = 'sY/mm/dd' argument = Signed numerical calendar date string.

JorG  = Calendar Mode
        G = Gregorian Calendar = Default
        J = Julian Calendar





// ---------------------------------------------------------------------------

   function Is_Valid_Date ($sYmmdd, $JorG='G')
{
   $sYmmdd = trim($sYmmdd);

   $JorG = ((substr(StrToUpper(trim($JorG)),0,1)) == 'J')? 'J':'G';

   $JDNum = JD_Num($sYmmdd, $JorG);
            if ($JDNum === FALSE) {return FALSE;}

   $TestDate = Inv_JD_Num($JDNum, $JorG);
               if ($TestDate === FALSE) {return FALSE;}

   list($Y,$m,$d) = PReg_Split("[\/]", $sYmmdd);
   $Y = FloatVal(trim($Y)); $m = FloatVal(trim($m)); $d = FloatVal(trim($d));

   list($a,$b,$c) = PReg_Split("[\/]", $TestDate);
   $a = FloatVal(trim($a)); $b = FloatVal(trim($b)); $c = FloatVal(trim($c));

   if ($Y==$a and $m==$b and $d==$c) {return TRUE;} else return FALSE;
}

// ---------------------------------------------------------------------------





##############################################################################
##############################################################################
To check if a certain calendar year is a leap year, the boolean function below
addresses this problem.

It works by checking if February 29th in the given year is a valid date for
the given calendar mode.  If so, then the year is a leap year, otherwise it
is a common year.

This function is dependent on the following three functions defined above.
JD_Num()
Inv_JD_Num()
Is_Valid_Date()

sY = Signed Year Value.
     Negative years are taken as BC years.
     There is no calendar year 0 (zero).

JorG  = Calendar Mode String
        'G' = Gregorian Calendar = Default
        'J' = Julian Calendar

Returns TRUE  if year IS a leap year.
Returns FALSE if year is NOT a leap year.



// --------------------------------------------------------
   function Is_Leap_Year ($sYearNum, $JorG='G')
{
   $sY = trim($sYearNum);

   return (Is_Valid_Date ("$sY/2/29", $JorG))? TRUE:FALSE;
}
// --------------------------------------------------------





##############################################################################
##############################################################################
If we know the signed JD Number for any date on the calendar, a very simple
algorithm can tell us the corresponding day of the week (DoW) on either the
Julian or Gregorian calendar. The same JD Number refers to the same weekday
on either calendar system, since the same algorithm is used for both.

Let the 7 days of the week be indexed from DoW = 0 to 6 (Sun. to Sat.):

Then, given the signed JD Number:
DoW = (7 + (JDNum + 1) mod 7) mod 7

Where for DoW:
0=Sun, 1=Mon, 2=Tue, 3=Wed, 4=Thu, 5=Fri and 6=Sat

In PHP, this can be easily coded as:

// -----------------------------------
   $DoW = (7 + ($JDNum + 1) % 7) % 7;
// -----------------------------------



If we want the three-letter abbreviation for the weekday instead, the simple
PHP function below can provide this.

// ---------------------------------------------------------------------------
   function JDNum_to_DoW_Str ($sJDNum)
(
   $JDNum = trim($sJDNum);

   return substr('SunMonTueWedThuFriSat', 3*((7 + ($JDNum + 1) % 7) % 7), 3);
}
// ---------------------------------------------------------------------------





##############################################################################
##############################################################################
Finding the difference between the Julian and Gregorian calendar systems.

The final official date on the old Julian Calendar system was:
AD 1582-Oct-04-Thu

On the above date, 10 days were dropped from the calendar to bring the date
of spring back to March 21st starting the following year.  Then, the rule
used to determine leap years was changed to prevent the same error from
recurring again for the same reason.

People (Catholics) went to bed on Thursday, October 4th, 1582 and woke up 10
days later on Friday, October 15th, 1582 to a new calendar system.

The first official date on our modern Gregorian calendar was:
AD 1582-Oct-15-Fri

Over the long-term, the Julian calendar has more leap years than the modern
Gregorian calendar, so the calendars differ as time progresses.

To determine the difference between the calendars in days, we first compute
the JD Number according to the old Julian calendar.  This is a relatively
simple computation.

JDNumJ = Julian Day Number according to the Julian calendar.
JDNumG = Julian Day Number according to the Gregorian calendar.

GJDiff = JDNumJ − JDNumG

If we know the Julian calendar JD Number for a given date, the JD Number for
that same date on the Gregorian calendar is:

JDNumG = JDNumJ + (GJDiff)

Where the (GJDiff) value could be positive or negative.

EXAMPLE:
For ('1949/5/20') -->  -13 days

This means that we apply (-13 days) to the Julian calendar JD Number to obtain
the JD Number according to the Gregorian calendar on the same calendar date.

EXAMPLE:
For '2026-11-25' on the Julian calendar:
JDNumJ = 2461383

The GJDiff for '2026-11-25' equates to -13 days.

This means that we have to apply -13 days (or subtract 13) days from the
Julian calendar JD Number value (JDNumJ) to obtain the value of the JD
Number according to the Gregorian calendar (JDNumG).

In this example case:
JDNumG = JDNumJ + (GJDiff)

or, by the numbers:
JDNumG = 2461383 + (-13) = 2461370

If we changed from the Julian to the Gregorian calendar today, we would have
to drop 13 days from the calendar, rather than 10 days, as we did in 1582.

NOTE:
The day (dd) value is not needed or used for this difference computation.

Below is a PHP function to return the difference in days between the Gregorian
calendar and the Julian calendar in the context (G minus J).

(GJDiff) = (JDNumG − JDNumJ)



// ---------------------------------------------------------------------------

   function GJ_Cal_Days_Diff ($sYmmdd)
{
   list($sY, $mm) = PReg_Split("[\/]", trim($sYmmdd));
   $sY = trim($sY); if (!Is_Numeric($sY)) {return FALSE;}
   $mm = trim($mm); if (!Is_Numeric($mm)) {return FALSE;}
   $ww = floor(((($sY < 0)? $sY+1 : $sY) - floor((14-$mm)/12)) / 100);
   $GJDiff = (floor($ww/4) - $ww + 2);

   return $GJDiff;
}

// ---------------------------------------------------------------------------





##############################################################################
##############################################################################

xxxxxxxxxxxxxxxxx