0) { $j += previous_weekday($weekday, $jd); } else { $j += next_weekday($weekday, $jd); } return $j; } function iso_to_julian($year, $week, $day) { return $day + n_weeks(0, gregorian_to_jd($year - 1, 12, 28), $week); } // JD_TO_ISO -- Return array of ISO (year, week, day) for Julian day function jd_to_iso($jd) { // var year, week, day; $year_jd_to_gregorian = jd_to_gregorian($jd - 3); $year = $year_jd_to_gregorian[0]; if ($jd >= iso_to_julian($year + 1, 1, 1)) { $year++; } $week = floor(($jd - iso_to_julian($year, 1, 1)) / 7) + 1; $day = jwday($jd); if ($day == 0) { $day = 7; } return array($year, $week, $day); } // ISO_DAY_TO_JULIAN -- Return Julian day of given ISO year, and day of year function iso_day_to_julian($year, $day) { return ($day - 1) + gregorian_to_jd($year, 1, 1); } // JD_TO_ISO_DAY -- Return array of ISO (year, day_of_year) for Julian day function jd_to_iso_day($jd) { // var year, day; $year_jd_to_gregorian = jd_to_gregorian($jd); $year = $year_jd_to_gregorian[0]; $day = floor($jd - gregorian_to_jd($year, 1, 1)) + 1; return array($year, $day); } /* PAD -- Pad a string to a given length with a given fill character. */ function pad($str, $howlong, $padwith) { $s = $str.toString(); //$s = string($str);?? while ($s.length < $howlong) { $s = $padwith + $s; } return $s; } // JULIAN_TO_JD -- Determine Julian day number from Julian calendar date $JULIAN_EPOCH = 1721423.5; function leap_julian($year) { return mod($year, 4) == (($year > 0) ? 0 : 3); } function julian_to_jd($year, $month, $day) { /* Adjust negative common era years to the zero-based notation we use. */ if ($year < 1) { $year++; } /* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */ if ($month <= 2) { $year--; $month += 12; } return ((floor((365.25 * ($year + 4716))) + floor((30.6001 * ($month + 1))) + $day) - 1524.5); } // JD_TO_JULIAN -- Calculate Julian calendar date from Julian day function jd_to_julian($td) { // var z, a, alpha, b, c, d, e, year, month, day; $td += 0.5; $z = floor($td); $a = $z; $b = $a + 1524; $c = floor(($b - 122.1) / 365.25); $d = floor(365.25 * $c); $e = floor(($b - $d) / 30.6001); $month = floor(($e < 14) ? ($e - 1) : ($e - 13)); $year = floor(($month > 2) ? ($c - 4716) : ($c - 4715)); $day = $b - $d - floor(30.6001 * $e); /* If year is less than 1, subtract one to convert from a zero based date system to the common era system in which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.). */ if ($year < 1) { $year--; } return array($year, $month, $day); } // HEBREW_TO_JD -- Determine Julian day from Hebrew date $HEBREW_EPOCH = 347995.5; // Is a given Hebrew year a leap year ? function hebrew_leap($year) { return mod((($year * 7) + 1), 19) < 7; } // How many months are there in a Hebrew year (12 = normal, 13 = leap) function hebrew_year_months($year) { return hebrew_leap($year) ? 13 : 12; } // Test for delay of start of new year and to avoid // Sunday, Wednesday, and Friday as start of the new year. function hebrew_delay_1($year) { // var months, days, parts; $months = floor(((235 * $year) - 234) / 19); $parts = 12084 + (13753 * $months); $day = ($months * 29) + floor($parts / 25920); if (mod((3 * ($day + 1)), 7) < 3) { $day++; } return $day; } // Check for delay in start of new year due to length of adjacent years function hebrew_delay_2($year) { // var last, present, next; $last = hebrew_delay_1($year - 1); $present = hebrew_delay_1($year); $next = hebrew_delay_1($year + 1); return (($next - $present) == 356) ? 2 : ((($present - $last) == 382) ? 1 : 0); } // How many days are in a Hebrew year ? function hebrew_year_days($year) { return hebrew_to_jd($year + 1, 7, 1) - hebrew_to_jd($year, 7, 1); } // How many days are in a given month of a given year function hebrew_month_days($year, $month) { // First of all, dispose of fixed-length 29 day months if ($month == 2 || $month == 4 || $month == 6 || $month == 10 || $month == 13) { return 29; } // If it's not a leap year, Adar has 29 days if ($month == 12 && !hebrew_leap($year)) { return 29; } // If it's Heshvan, days depend on length of year if ($month == 8 && !(mod(hebrew_year_days($year), 10) == 5)) { return 29; } // Similarly, Kislev varies with the length of year if ($month == 9 && (mod(hebrew_year_days($year), 10) == 3)) { return 29; } // Nope, it's a 30 day month return 30; } // Finally, wrap it all up into... function hebrew_to_jd($year, $month, $day) { // var jd, mon, months; global $HEBREW_EPOCH; $months = hebrew_year_months($year); $jd = $HEBREW_EPOCH + hebrew_delay_1($year) + hebrew_delay_2($year) + $day + 1; if ($month < 7) { for ($mon = 7; $mon <= $months; $mon++) { $jd += hebrew_month_days($year, $mon); } for ($mon = 1; $mon < $month; $mon++) { $jd += hebrew_month_days($year, $mon); } } else { for ($mon = 7; $mon < $month; $mon++) { $jd += hebrew_month_days($year, $mon); } } return $jd; } /* JD_TO_HEBREW -- Convert Julian date to Hebrew date This works by making multiple calls to the inverse function, and is this very slow. */ function jd_to_hebrew($jd) { // var year, month, day, i, count, first; global $HEBREW_EPOCH; $jd = floor($jd) + 0.5; $count = floor((($jd - $HEBREW_EPOCH) * 98496.0) / 35975351.0); $year = $count - 1; for ($i = $count; $jd >= hebrew_to_jd($i, 7, 1); $i++) { $year++; } $first = ($jd < hebrew_to_jd($year, 1, 1)) ? 7 : 1; $month = $first; for ($i = $first; $jd > hebrew_to_jd($year, $i, hebrew_month_days($year, $i)); $i++) { $month++; } $day = ($jd - hebrew_to_jd($year, $month, 1)) + 1; return array($year, $month, $day); } /* EQUINOXE_A_PARIS -- Determine Julian day and fraction of the September equinox at the Paris meridian in a given Gregorian year. */ function equinoxe_a_paris($year) { // var equJED, equJD, equAPP, equParis, dtParis; // September equinox in dynamical time $equJED = equinox($year, 2); // Correct for delta T to obtain Universal time $equJD = $equJED - (deltat($year) / (24 * 60 * 60)); // Apply the equation of time to yield the apparent time at Greenwich $equAPP = $equJD + equationOfTime($equJED); /* Finally, we must correct for the constant difference between the Greenwich meridian and that of Paris, 2°20'15" to the East. */ $dtParis = (2 + (20 / 60.0) + (15 / (60 * 60.0))) / 360; $equParis = $equAPP + $dtParis; return $equParis; } /* PARIS_EQUINOXE_JD -- Calculate Julian day during which the September equinox, reckoned from the Paris meridian, occurred for a given Gregorian year. */ function paris_equinoxe_jd($year) { // var ep, epg; $ep = equinoxe_a_paris($year); $epg = floor($ep - 0.5) + 0.5; return $epg; } /* ANNEE_DE_LA_REVOLUTION -- Determine the year in the French revolutionary calendar in which a given Julian day falls. Returns an array of two elements: [0] Année de la Révolution [1] Julian day number containing equinox for this year. */ $FRENCH_REVOLUTIONARY_EPOCH = 2375839.5; function annee_da_la_revolution($jd) { global $TropicalYear, $FRENCH_REVOLUTIONARY_EPOCH; $guess_jd_to_gregorian = jd_to_gregorian($jd); $guess = $guess_jd_to_gregorian[0] - 2; // lasteq, nexteq, adr; $lasteq = paris_equinoxe_jd($guess); while ($lasteq > $jd) { $guess--; $lasteq = paris_equinoxe_jd($guess); } $nexteq = $lasteq - 1; while (!(($lasteq <= $jd) && ($jd < $nexteq))) { $lasteq = $nexteq; $guess++; $nexteq = paris_equinoxe_jd($guess); } $adr = round(($lasteq - $FRENCH_REVOLUTIONARY_EPOCH) / $TropicalYear) + 1; return array($adr, $lasteq); } /* JD_TO_FRENCH_REVOLUTIONARY -- Calculate date in the French Revolutionary calendar from Julian day. The five or six "sansculottides" are considered a thirteenth month in the results of this function. */ function jd_to_french_revolutionary($jd) { // var an, mois, decade, jour, // adr, equinoxe; $jd = floor($jd) + 0.5; $adr = annee_da_la_revolution($jd); $an = $adr[0]; $equinoxe = $adr[1]; $mois = floor(($jd - $equinoxe) / 30) + 1; $jour = ($jd - $equinoxe) % 30; $decade = floor($jour / 10) + 1; $jour = ($jour % 10) + 1; return array($an, $mois, $decade, $jour); } /* FRENCH_REVOLUTIONARY_TO_JD -- Obtain Julian day from a given French Revolutionary calendar date. */ function french_revolutionary_to_jd($an, $mois, $decade, $jour) { // var adr, equinoxe, guess, jd; global $TropicalYear, $FRENCH_REVOLUTIONARY_EPOCH; $guess = $FRENCH_REVOLUTIONARY_EPOCH + ($TropicalYear * (($an - 1) - 1)); $adr = array($an - 1, 0); while ($adr[0] < $an) { $adr = annee_da_la_revolution($guess); $guess = $adr[1] + ($TropicalYear + 2); } $equinoxe = $adr[1]; $jd = $equinoxe + (30 * ($mois - 1)) + (10 * ($decade - 1)) + ($jour - 1); return $jd; } // LEAP_ISLAMIC -- Is a given year a leap year in the Islamic calendar ? function leap_islamic($year) { return ((($year * 11) + 14) % 30) < 11; } // ISLAMIC_TO_JD -- Determine Julian day from Islamic date $ISLAMIC_EPOCH = 1948439.5; $ISLAMIC_WEEKDAYS = array("al-'ahad", "al-'ithnayn", "ath-thalatha'", "al-'arb`a'", "al-khamis", "al-jum`a", "as-sabt"); function islamic_to_jd($year, $month, $day) { global $ISLAMIC_EPOCH; return ($day + ceil(29.5 * ($month - 1)) + ($year - 1) * 354 + floor((3 + (11 * $year)) / 30) + $ISLAMIC_EPOCH) - 1; } // JD_TO_ISLAMIC -- Calculate Islamic date from Julian day function jd_to_islamic($jd) { // var year, month, day; global $ISLAMIC_EPOCH; $jd = floor($jd) + 0.5; $year = floor(((30 * ($jd - $ISLAMIC_EPOCH)) + 10646) / 10631); $month = min(12, ceil(($jd - (29 + islamic_to_jd($year, 1, 1))) / 29.5) + 1); $day = ($jd - islamic_to_jd($year, $month, 1)) + 1; return array($year, $month, $day); } // LEAP_PERSIAN -- Is a given year a leap year in the Persian calendar ? function leap_persian($year) { return (((((($year - (($year > 0) ? 474 : 473)) % 2820) + 474) + 38) * 682) % 2816) < 682; } // PERSIAN_TO_JD -- Determine Julian day from Persian date $PERSIAN_EPOCH = 1948320.5; $PERSIAN_WEEKDAYS = array("Yekshanbeh", "Doshanbeh", "Seshhanbeh", "Chaharshanbeh", "Panjshanbeh", "Jomeh", "Shanbeh"); function persian_to_jd($year, $month, $day) { // var epbase, epyear; global $PERSIAN_EPOCH; $epbase = $year - (($year >= 0) ? 474 : 473); $epyear = 474 + mod($epbase, 2820); return $day + (($month <= 7) ? (($month - 1) * 31) : ((($month - 1) * 30) + 6) ) + floor((($epyear * 682) - 110) / 2816) + ($epyear - 1) * 365 + floor($epbase / 2820) * 1029983 + ($PERSIAN_EPOCH - 1); } // JD_TO_PERSIAN -- Calculate Persian date from Julian day function jd_to_persian($jd) { // var year, month, day, depoch, cycle, cyear, ycycle, // aux1, aux2, yday; $jd = floor($jd) + 0.5; $depoch = $jd - persian_to_jd(475, 1, 1); $cycle = floor($depoch / 1029983); $cyear = mod($depoch, 1029983); if ($cyear == 1029982) { $ycycle = 2820; } else { $aux1 = floor($cyear / 366); $aux2 = mod($cyear, 366); $ycycle = floor(((2134 * $aux1) + (2816 * $aux2) + 2815) / 1028522) + $aux1 + 1; } $year = $ycycle + (2820 * $cycle) + 474; if ($year <= 0) { $year--; } $yday = ($jd - persian_to_jd($year, 1, 1)) + 1; $month = ($yday <= 186) ? ceil($yday / 31) : ceil(($yday - 6) / 30); $day = ($jd - persian_to_jd($year, $month, 1)) + 1; return array($year, $month, $day); } // MAYAN_COUNT_TO_JD -- Determine Julian day from Mayan long count $MAYAN_COUNT_EPOCH = 584282.5; function mayan_count_to_jd($baktun, $katun, $tun, $uinal, $kin) { global $MAYAN_COUNT_EPOCH; return $MAYAN_COUNT_EPOCH + ($baktun * 144000) + ($katun * 7200) + ($tun * 360) + ($uinal * 20) + $kin; } // JD_TO_MAYAN_COUNT -- Calculate Mayan long count from Julian day function jd_to_mayan_count($jd) { // var d, baktun, katun, tun, uinal, kin; global $MAYAN_COUNT_EPOCH; $jd = floor($jd) + 0.5; $d = $jd - $MAYAN_COUNT_EPOCH; $baktun = floor($d / 144000); $d = mod($d, 144000); $katun = floor($d / 7200); $d = mod($d, 7200); $tun = floor($d / 360); $d = mod($d, 360); $uinal = floor($d / 20); $kin = mod($d, 20); return array($baktun, $katun, $tun, $uinal, $kin); } // JD_TO_MAYAN_HAAB -- Determine Mayan Haab "month" and day from Julian day $MAYAN_HAAB_MONTHS = array("Pop", "Uo", "Zip", "Zotz", "Tzec", "Xul", "Yaxkin", "Mol", "Chen", "Yax", "Zac", "Ceh", "Mac", "Kankin", "Muan", "Pax", "Kayab", "Cumku", "Uayeb"); function jd_to_mayan_haab($jd) { // var lcount, day; global $MAYAN_COUNT_EPOCH; $jd = floor($jd) + 0.5; $lcount = $jd - $MAYAN_COUNT_EPOCH; $day = mod($lcount + 8 + ((18 - 1) * 20), 365); return array (floor($day / 20) + 1, mod($day, 20)); } // JD_TO_MAYAN_TZOLKIN -- Determine Mayan Tzolkin "month" and day from Julian day $MAYAN_TZOLKIN_MONTHS = array("Imix", "Ik", "Akbal", "Kan", "Chicchan", "Cimi", "Manik", "Lamat", "Muluc", "Oc", "Chuen", "Eb", "Ben", "Ix", "Men", "Cib", "Caban", "Etznab", "Cauac", "Ahau"); function jd_to_mayan_tzolkin($jd) { // var lcount; global $MAYAN_COUNT_EPOCH; $jd = floor($jd) + 0.5; $lcount = $jd - $MAYAN_COUNT_EPOCH; return array (amod($lcount + 20, 20), amod($lcount + 4, 13)); } // BAHAI_TO_JD -- Determine Julian day from Bahai date $BAHAI_EPOCH = 2394646.5; $BAHAI_WEEKDAYS = array("Jamál", "Kamál", "Fidál", "Idál", "Istijlál", "Istiqlál", "Jalál"); function bahai_to_jd($major, $cycle, $year, $month, $day) { // var gy; global $BAHAI_EPOCH; $gy_jd_to_gregorian = jd_to_gregorian($BAHAI_EPOCH); $gy = (361 * ($major - 1)) + (19 * ($cycle - 1)) + ($year - 1) + $gy_jd_to_gregorian[0]; return gregorian_to_jd($gy, 3, 20) + (19 * ($month - 1)) + (($month != 20) ? 0 : (leap_gregorian($gy + 1) ? -14 : -15)) + $day; } // JD_TO_BAHAI -- Calculate Bahai date from Julian day function jd_to_bahai($jd) { // var major, cycle, year, month, day, // gy, bstarty, bys, days, bld; global $BAHAI_EPOCH; $jd = floor($jd) + 0.5; $gy_jd_to_gregorian = jd_to_gregorian($jd); $gy = $gy_jd_to_gregorian[0]; $bstarty_jd_to_gregorian = jd_to_gregorian($BAHAI_EPOCH); $bstarty = $bstarty_jd_to_gregorian[0]; $bys = $gy - ($bstarty + (((gregorian_to_jd($gy, 1, 1) <= $jd) && ($jd <= gregorian_to_jd($gy, 3, 20))) ? 1 : 0)); $major = floor($bys / 361) + 1; $cycle = floor(mod($bys, 361) / 19) + 1; $year = mod($bys, 19) + 1; $days = $jd - bahai_to_jd($major, $cycle, $year, 1, 1); $bld = bahai_to_jd($major, $cycle, $year, 20, 1); $month = ($jd >= $bld) ? 20 : (floor($days / 19) + 1); $day = ($jd + 1) - bahai_to_jd($major, $cycle, $year, $month, 1); return array($major, $cycle, $year, $month, $day); } // INDIAN_CIVIL_TO_JD -- Obtain Julian day for Indian Civil date $INDIAN_CIVIL_WEEKDAYS = array( "ravivara", "somavara", "mangalavara", "budhavara", "brahaspativara", "sukravara", "sanivara"); function indian_civil_to_jd($year, $month, $day) { // var Caitra, gyear, leap, start, jd, m; $gyear = $year + 78; $leap = leap_gregorian($gyear); // Is this a leap year ? $start = gregorian_to_jd($gyear, 3, $leap ? 21 : 22); $Caitra = $leap ? 31 : 30; if ($month == 1) { $jd = $start + ($day - 1); } else { $jd = $start + $Caitra; $m = $month - 2; $m = min($m, 5); $jd += $m * 31; if ($month >= 8) { $m = $month - 7; $jd += $m * 30; } $jd += $day - 1; } return $jd; } // JD_TO_INDIAN_CIVIL -- Calculate Indian Civil date from Julian day function jd_to_indian_civil($jd) { // var Caitra, Saka, greg, greg0, leap, start, year, yday, mday; $Saka = 79 - 1; // Offset in years from Saka era to Gregorian epoch $start = 80; // Day offset between Saka and Gregorian $jd = floor($jd) + 0.5; $greg = jd_to_gregorian($jd); // Gregorian date for Julian day $leap = leap_gregorian($greg[0]); // Is this a leap year? $year = $greg[0] - $Saka; // Tentative year in Saka era $greg0 = gregorian_to_jd($greg[0], 1, 1); // JD at start of Gregorian year $yday = $jd - $greg0; // Day number (0 based) in Gregorian year $Caitra = $leap ? 31 : 30; // Days in Caitra this year if ($yday < $start) { // Day is at the end of the preceding Saka year $year--; $yday += $Caitra + (31 * 5) + (30 * 3) + 10 + $start; } $yday -= $start; if ($yday < $Caitra) { $month = 1; $day = $yday + 1; } else { $mday = $yday - $Caitra; if ($mday < (31 * 5)) { $month = floor($mday / 31) + 2; $day = ($mday % 31) + 1; } else { $mday -= 31 * 5; $month = floor($mday / 30) + 7; $day = ($mday % 30) + 1; } } return array($year, $month, $day); } /* updateFromGregorian -- Update all calendars from Gregorian. "Why not Julian date?" you ask. Because starting from Gregorian guarantees we're already snapped to an integral second, so we don't get roundoff errors in other calendars. */ function updateFromGregorian() { // var j, year, mon, mday, hour, min, sec, // weekday, julcal, hebcal, islcal, hmindex, utime, isoweek, // may_countcal, mayhaabcal, maytzolkincal, bahcal, frrcal, // indcal, isoday, xgregcal; global $document_julianday_day, $document_modifiedjulianday_day, $document_gregorian_year, $document_gregorian_month, $document_gregorian_day, $document_gregorian_hour, $document_gregorian_min, $document_gregorian_sec, $document_gregorian_wday, $document_gregorian_leap, $document_juliancalendar_year, $document_juliancalendar_month, $document_juliancalendar_day, $document_juliancalendar_leap, $document_juliancalendar_wday, $document_hebrew_month_options, $document_hebrew_year, $document_hebrew_month, $document_hebrew_day, $document_hebrew_hebmonth_src, $document_hebrew_leap, $document_islamic_year, $document_islamic_month, $document_islamic_day, $document_islamic_wday, $document_islamic_leap, $document_persian_year, $document_persian_month, $document_persian_day, $document_persian_wday, $document_persian_leap, $document_mayancount_baktun, $document_mayancount_katun, $document_mayancount_tun, $document_mayancount_uinal, $document_mayancount_kin, $document_mayancount_haab, $document_mayancount_tzolkin, $document_bahai_kull_i_shay, $document_bahai_vahid, $document_bahai_year, $document_bahai_month, $document_bahai_day, $document_bahai_weekday, $document_bahai_leap, $document_indiancivilcalendar_year, $document_indiancivilcalendar_month, $document_indiancivilcalendar_day, $document_indiancivilcalendar_weekday, $document_indiancivilcalendar_leap, $document_french_an, $document_french_mois, $document_french_decade, $document_french_jour, $document_gregserial_day, $document_excelserial1900_day, $document_excelserial1904_day, $document_unixtime_time, $document_isoweek_year, $document_isoweek_week, $document_isoweek_day, $document_isoday_year, $document_isoday_day, $Weekdays, $J0000, $J1970, $JMJD, $J1900, $J1904, $NormLeap, $ISLAMIC_WEEKDAYS, $PERSIAN_WEEKDAYS, $MAYAN_HAAB_MONTHS, $MAYAN_TZOLKIN_MONTHS, $BAHAI_WEEKDAYS, $INDIAN_CIVIL_WEEKDAYS; $year = $document_gregorian_year; //$mon = $document_gregorian_month-1; $mon = $document_gregorian_month; $mday = $document_gregorian_day; $hour = $min = $sec = 0; $hour = $document_gregorian_hour; $min = $document_gregorian_min; $sec = $document_gregorian_sec; // Update Julian day $j = gregorian_to_jd($year, $mon + 1, $mday) + (floor($sec + 60 * ($min + 60 * $hour) + 0.5) / 86400.0); $document_julianday_day = $j; $document_modifiedjulianday_day = $j - $JMJD; // Update day of week in Gregorian box $weekday = jwday($j); $document_gregorian_wday = $Weekdays[$weekday]; /* echo '