2012-10-31 2 views
0

나는 하나의 PHP 클래스로 변환 한 마야 별자리 기호를 계산하는 여러 개의 JavaScript 함수를 가지고 있습니다. 일부 날짜는 제외하고 모든 것이 잘 작동합니다. 예를 들어, 날짜 06.10.1977은 PHP 클래스를 사용하여 NULL 결과를 제공하지만 동등한 JavaScript 함수에서이 날짜가 적절한 값을 반환합니다. 어쩌면 내가 PHP에서 뭔가 잘못 됐어, 누군가가 나를 위해 이것을 확인할 수 있을까?자바 스크립트 함수를 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

먼저 : 항상 모든 관련 스크립트를 게시 할 수 있습니다.

자바 스크립트는 별도의 함수를 사용하는 동안 : 당신이 당신의 문제를 해결하기 위해 매우 중요합니다 자바 스크립트 기능 amod()을 실종됐다, 나는 당신의 자바 스크립트 기능 jd_to_mayan_tzolkin()에서 jd_to_mayan_tzolkin() IST 다른 원래 here

귀하의 PHP 함수에서 발견 amod()는 계수를 계산하고 계수-결과가 당신의 PHP 함수 그냥 0 아플리케를 할 수있는 계수를 반환 0

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

경우 분자를 반환합니다 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; 
} 

을 다음과 같이 jd_to_mayan_tzolkin()를 PHP 방법을 변경 :

는 클래스에 다음 두 가지 기능을 추가

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")는 단지 "모듈"입니다. –

관련 문제