2012-10-31 116 views
0

我有多個JavaScript函數來計算瑪雅星座符號,我將其轉換爲單個PHP類。一切運作良好,除了某些日期。例如,日期06.10.1977使用PHP類給了我一個NULL結果,但是這個日期在等效的JavaScript函數中返回一個合適的值。也許我在PHP中遇到了問題,那麼有人可以檢查我嗎?將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); 
     } 

} 
+0

是不是有點晚了,開始使用瑪雅曆法?在你啓動並運行的時候,如果我沒有弄錯,我們會很快離開 –

+0

你的JavaScript中的'amod'函數在哪裏?在那裏的東西似乎做了一些你在PHP中做的簡單模數。這是腳本失敗的地方,返回0作爲第一個數組值,這對於進一步處理似乎是無效的。 – MiDo

回答

1

第一:始終發佈所有相關的腳本。你失蹤的JavaScript函數AMOD(),這是解決你的問題是至關重要的,我發現它在原有here

你的PHP函數從您的JavaScript函數jd_to_mayan_tzolkin()jd_to_mayan_tzolkin() IST不同:

雖然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進一步處理失敗之前。

以下兩個函數添加到您的類:

/* 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)); 
} 
+0

對不起,我沒有找到這個amod函數。這就是它失敗的原因。非常感謝你! –

+0

因爲我首先誤解了「mod」函數。我的假設是「mod」(「amod」)只是一個「模數」。 –