0

マヤの星占いの記号を計算するための複数のJavaScript関数があり、それを単一のPHPクラスに変換しました。一部の日付を除いて、すべてがうまく機能します。たとえば、1977年6月10日の日付は、PHPクラスを使用してNULLの結果が得られますが、同等のJavaScript関数のこの日付は適切な値を返します。PHPに何か問題があるので、誰かが私のためにこれをチェックできますか?

JAVASCRIPT:

function leap_gregorian(year) {
    return ((year % 4) == 0) && (!(((year % 100) == 0) && ((year % 400) != 0)));
}

//  GREGORIAN_TO_JD  --  Determine Julian day number from Gregorian calendar date
var GREGORIAN_EPOCH = 1721425.5;

function gregorian_to_jd(year, month, day) {
    return (GREGORIAN_EPOCH - 1) +
           (365 * (year - 1)) +
           Math.floor((year - 1) / 4) +
           (-Math.floor((year - 1) / 100)) +
           Math.floor((year - 1) / 400) +
           Math.floor((((367 * month) - 362) / 12) +
           ((month <= 2) ? 0 : (leap_gregorian(year) ? -1 : -2)) +
           day);
}

var MAYAN_COUNT_EPOCH = 584282.5;
//  JD_TO_MAYAN_COUNT  --  Calculate Mayan long count from Julian day

    function jd_to_mayan_count(jd) {
        var d, baktun, katun, tun, uinal, kin;

        jd = Math.floor(jd) + 0.5;
        d = jd - MAYAN_COUNT_EPOCH;
        baktun = Math.floor(d / 144000);
        d = mod(d, 144000);
        katun = Math.floor(d / 7200);
        d = mod(d, 7200);
        tun = Math.floor(d / 360);
        d = mod(d, 360);
        uinal = Math.floor(d / 20);
        kin = mod(d, 20);

        return new Array(baktun, katun, tun, uinal, kin);
    }

 //  JD_TO_MAYAN_HAAB  --  Determine Mayan Haab "month" and day from Julian day
var MAYAN_HAAB_MONTHS = new 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;

    jd = Math.floor(jd) + 0.5;
    lcount = jd - MAYAN_COUNT_EPOCH;
    day = mod(lcount + 8 + ((18 - 1) * 20), 365);

    return new Array(Math.floor(day / 20) + 1, mod(day, 20));
}

//  JD_TO_MAYAN_TZOLKIN  --  Determine Mayan Tzolkin "month" and day from Julian day
var MAYAN_TZOLKIN_MONTHS = new Array("Imix", "Ik", "Akbal", "Kan", "Chicchan",
                                     "Cimi", "Manik", "Lamat", "Muluc", "Oc",
                                     "Chuen", "Eb", "Ben", "Ix", "Men",
                                     "Cib", "Caban", "Etznab", "Cauac", "Ahau");

var MAYAN_TZOLKIN_MONTHS_EN = new Array("Crocodile", "Wind", "House", "Lizard", "Serpent",
                                        "Death", "Deer", "Rabbit", "Water", "Dog",
                                        "Monkey", "Grass", "Reed", "Jaguar", "Eagle", 
                                        "Vulture", "Earth", "Knife", "Storm", "Flower");

function jd_to_mayan_tzolkin(jd) {
    var lcount;

     jd = Math.floor(jd) + 0.5;
     lcount = jd - MAYAN_COUNT_EPOCH;
     return new Array(amod(lcount + 20, 20), amod(lcount + 4, 13));
}

function getMayanSign(sign, month, day, year) {

    var isValidated = true;
    var result = "";

    if (!IsNumeric(month.value) || month.value <= 0 || month.value > 12) {
        month.value = "MM";
        isValidated = false;
    }

    if (!IsNumeric(day.value) || day.value <= 0 || day.value > 31) {
        day.value = "DD";
        isValidated = false;
    }

    if (!IsNumeric(year.value) || year.value < 1900 ) {
        year.value = "YYYY";
        isValidated = false;
    }

    if (isValidated) {
        year = new Number(year.value);
        mon = new Number(month.value);
        mday = new Number(day.value);
        hour = min = sec = 0;

        //  Update Julian day
        j = gregorian_to_jd(year, mon + 0, mday) +
           (Math.floor(sec + 60 * (min + 60 * hour) + 0.5) / 86400.0);

            maytzolkincal = jd_to_mayan_tzolkin(j);
            result = MAYAN_TZOLKIN_MONTHS_EN[maytzolkincal[0] - 1];
        } else result = "INVALID BIRTHDAY";

        sign.value = result;
    }

PHPクラス:

class MayanCalculator {

        //  JD_TO_MAYAN_TZOLKIN  --  Determine Mayan Tzolkin "month" and day from Julian day
        private $MAYAN_TZOLKIN_MONTHS = array("Imix", "Ik", "Akbal", "Kan", "Chicchan",
                                             "Cimi", "Manik", "Lamat", "Muluc", "Oc",
                                             "Chuen", "Eb", "Ben", "Ix", "Men",
                                             "Cib", "Caban", "Etznab", "Cauac", "Ahau");

        private $MAYAN_TZOLKIN_MONTHS_EN = array("Crocodile", "Wind", "House", "Lizard", "Serpent",
                                        "Death", "Deer", "Rabbit", "Water", "Dog",
                                        "Monkey", "Grass", "Reed", "Jaguar", "Eagle", 
                                        "Vulture", "Earth", "Knife", "Storm", "Flower");

        private $GREGORIAN_EPOCH = 1721425.5;

        private $MAYAN_COUNT_EPOCH = 584282.5;

        private $MAYAN_HAAB_MONTHS = array("Pop", "Uo", "Zip", "Zotz", "Tzec", "Xul",
                                  "Yaxkin", "Mol", "Chen", "Yax", "Zac", "Ceh",
                                  "Mac", "Kankin", "Muan", "Pax", "Kayab", "Cumku", "Uayeb");

        private $_day;
        private $_month;
        private $_year;
        private $sign;
        private $signm;

        function __construct($day, $month, $year) {
                $this->_day = $day;
                $this->_month = $month;
                $this->_year = $year;
        }

        public function getMayanSign() {

            $this->sign = $this->getSign();
            $this->signm = $this->getMayanSignOnMayan();    

            $ids = new getIDS(null,null,$this->sign);
            $id = $ids->returnIDS();
            return array("mayan_sign" => $this->sign, "mayan_sign_on_mayan" => $this->signm, "mayan_sign_id" => $id["mayan_id"]);

        }

        private function getSign() {
            $isValidated = true;
            $result = null;

            if (!is_numeric($this->_month) || $this->_month <= 0 || $this->_month > 12) :
                $isValidated = false;
            endif;  

            if (!is_numeric($this->_day) || $this->_day <= 0 || $this->_day > 31) :
                $isValidated = false;
            endif;  

            if (!is_numeric($this->_year) || $this->_year < 1900) :
                $isValidated = false;
            endif;  

            if ($isValidated) : 

                $hour = 0;
                $min = 0;
                $sec = 0;

                //update julian day
                $j = $this->gregorian_to_jd($this->_year, $this->_month+0, $this->_day) + (floor($sec + 60 * ($min + 60 * $hour) + 0.5) / 86400.0);
                $maytzolkincal = $this->jd_to_mayan_tzolkin($j);
                $result = $this->MAYAN_TZOLKIN_MONTHS_EN[$maytzolkincal[0]-1];
            else :

            $result = 'Wrong date '.  $this->_day . '.' . $this->_month . '.' . $this->_year;

            endif;

            return $result; 
        }

        private function getMayanSignOnMayan() {
            $isValidated = true;
            $result = null;

            if (!is_numeric($this->_month) || $this->_month <= 0 || $this->_month > 12) :
                $isValidated = false;
            endif;  

            if (!is_numeric($this->_day) || $this->_day <= 0 || $this->_day > 31) :
                $isValidated = false;
            endif;  

            if (!is_numeric($this->_year) || $this->_year < 1900) :
                $isValidated = false;
            endif;  

            if ($isValidated) : 

                $hour = 0;
                $min = 0;
                $sec = 0;

                //update julian day
                $j = $this->gregorian_to_jd($this->_year, $this->_month+0, $this->_day) + (floor($sec + 60 * ($min + 60 * $hour) + 0.5) / 86400);
                $maytzolkincal = $this->jd_to_mayan_tzolkin($j);
                $result = $this->MAYAN_TZOLKIN_MONTHS[$maytzolkincal[0]]-1;
            else :

            $result = 'Wrong date '.  $this->_day . '.' . $this->_month . '.' . $this->_year;

            endif;

            return $result; 
        }

        private function leap_gregorian($year) {
                    return (($year % 4) == 0) && (!((($year % 100) == 0) && (($year % 400) != 0)));
        }

        private function gregorian_to_jd($year, $month, $day) {
            $result = ($this->GREGORIAN_EPOCH - 1) +
                   (365 * ($year - 1)) +
                   floor(($year - 1) / 4) +
                   (-floor(($year - 1) / 100)) +
                   floor(($year - 1) / 400) +
                   floor((((367 * $month) - 362) / 12) +
                   (($month <= 2) ? 0 : ($this->leap_gregorian($year) ? -1 : -2)) +
                   $day);
            return $result;
        }

        private function jd_to_mayan_count($jd) {
        $jd = floor($jd) + 0.5;
        $d = $jd - $this->MAYAN_COUNT_EPOCH;
        $baktun = floor($d / 144000);
        $d = $d % 144000;
        $katun = floor($d / 7200);
        $d = $d % 7200;
        $tun = floor($d / 360);
        $d = $d % 360;
        $uinal = floor($d / 20);
        $kin = $d % 20;

        return array($baktun, $katun, $tun, $uinal, $kin);
        }

        private function jd_to_mayan_haab($jd) {

            $jd = floor($jd) + 0.5;
            $lcount = $jd - $this->MAYAN_COUNT_EPOCH;
            $day = ($lcount + 8 + ((18 - 1) * 20)) % 365;

            return array(floor($day / 20) + 1, $day % 20);
        }

        private function jd_to_mayan_tzolkin($jd) {
             $jd = floor($jd) + 0.5;
             $lcount = $jd - $this->MAYAN_COUNT_EPOCH;
             return array(($lcount + 20) % 20, ($lcount + 4) % 13);
        }

}
4

1 に答える 1

1

最初:関連するすべてのスクリプトを常に投稿してください。あなたの問題を解決するために重要なJavaScript関数amod()がありませんでした、私はここでオリジナルでそれを見つけました

PHP関数jd_to_mayan_tzolkin()はJavaScript関数と違いはありませんjd_to_mayan_tzolkin()

JavaScriptは別の関数amod()を使用してモジュラスを計算し、モジュラスの結果が次の場合は分子を返します。0

//  AMOD  --  Modulus function which returns numerator if modulus is zero
function amod($a, $b) {
  return $this->mod($a - 1, $b) + 1;
}

php関数はモジュラスを返すだけな0ので、PHPのそれ以上の処理は失敗します。

次の2つの関数をクラスに追加します。

/*  MOD  --  Modulus function which works for non-integers.  */
private function mod($a, $b) {
  return $a - ($b * floor($a / $b));
}

//  AMOD  --  Modulus function which returns numerator if modulus is zero
private function amod($a, $b) {
  return $this->mod($a - 1, $b) + 1;
}   

そして、PHPメソッドjd_to_mayan_tzolkin()を次のように変更します。

private function jd_to_mayan_tzolkin($jd) {
     $jd = floor($jd) + 0.5;
     $lcount = $jd - $this->MAYAN_COUNT_EPOCH;
     return array($this->amod(($lcount + 20),20), $this->amod(($lcount + 4),13));
}
于 2012-10-31T13:34:37.417 に答える