PHP converts the Gregorian calendar to lunar calendar

Source: Internet
Author: User
PHP class

 lunarinfo[$year-$this->min_year]; if ($year = = $this->min_year&& $month <=2&& $date <=9) return Array (1891, ' Lunar month ', ' Junior ', ' Xin Mao ', 1, 1, '        Rabbit '); return $this->getlunarbybetween ($year, $this->getdaysbetweensolar ($year, $month, $date, $yearData [1],$    YEARDATA[2])); The Public Function Convertsolarmonthtolunar ($year, $month, $date) {$yearData = $this->lunarinfo[$year-$this-        >MIN_YEAR]; if ($year = = $this->min_year&& $month <=2&& $date <=9) return Array (1891, ' Lunar month ', ' Junior ', ' Xin Mao ', 1, 1, '        Rabbit ');        $month _days_ary = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);        $DD = $month _days_ary[$month];        if ($this->isleapyear ($year) && $month = = 2) $dd + +;        $lunar _ary = Array (); for ($i = 1; $i < $DD; $i + +) {$array = $this->getlunarbybetween ($year, $this->getdaysbetweensol            AR ($year, $month, $i, $yearData [1], $yearData [2]); $array [] = $year. '-' . $month. '-' . $i;            $lunar _ary[$i] = $array;    } return $lunar _ary; }/** * Converts the lunar calendar to the Gregorian calendar * @param year Lunar calendar-years * @param month lunar calendar-months, Leap month processing: For example, if the year of the leap May, then the second May on the June, the equivalent of the lunar calendar has 13 months, but sometimes the 13th month of the day The number is 0 * @param date lunar-day */Public Function convertlunartosolar ($year, $month, $date) {$yearData = $this        ->lunarinfo[$year-$this->min_year];        $between = $this->getdaysbetweenlunar ($year, $month, $date);        $res = Mktime (0,0,0, $yearData [1], $yearData [2], $year);        $res = Date (' y-m-d ', $res + $between *24*60*60);        $day = Explode ('-', $res);        $year = $day [0];        $month = $day [1];        $day = $day [2];    Return Array ($year, $month, $day); }/** * Determines if a leap year is @param year */Public Function isleapyear ($year) {return ($year%4==0 & & $year%100!=0) | |    ($year%400==0)); }/** * Get Ganzhi Calendar * @param year */Public Function Getlunaryearname ($year) {$sky = array (' g ', ' symplectic ' , ' Ren ', ' decyl ', ' A ', ' B ', 'C ', ' ding ', ' e ', ' own ');        $earth = Array (' shen ', ' unitary ', ' Xu ', ' hai ', ' zi ', ' ugly ', ' Yin ', ' Mao ', ' Chen ', ' have ', ' noon ', ' not ');        $year = $year. ";    return $sky [$year {3}]. $earth [$year%12]; */** * @param year lunar Year */Public Function Getyearzodiac ($year) {$zodiac = array (        ' Monkey ', ' chicken ', ' dog ', ' pig ', ' rat ', ' cow ', ' tiger ', ' Rabbit ', ' dragon ', ' snake ', ' horse ', ' sheep ';    return $zodiac [$year%12]; }/** * Gets the number of days of the Gregorian month * @param year Gregorian-years * @param month Solar calendar-month */Public function getsolarmonthdays ($year, $month) {$monthHash = array (' 1 ' =>31, ' 2 ' = = $this->isleapyear ($year)? 29:28, ' 3 ' =>31, ' 4 ' =>30, ' 5 ' =        6 ' =>30, ' 7 ' =>31, ' 8 ' =>31, ' 9 ' =>30, ' =>31 ', ' One ' =>30, ' one ' =>31 ';    return $monthHash ["$month"]; }/** * Gets the number of days of the lunar month * @param year lunar calendar-years * @param month lunar calendar-month, starting from January */Public Function getlunarmonthdays (        $year, $month) {$monthData = $this->getlunarmonths ($year);    return $monthData [$month-1]; /** * Gets an array of days of the lunar month     * @param year */Public Function getlunarmonths ($year) {$yearData = $this->lunarinfo[$year-$th        is->min_year];        $leapMonth = $yearData [0];        $bit = Decbin ($yearData [3]);        for ($i = 0; $i < strlen ($bit); $i + +) $bitArray [$i] = substr ($bit, $i, 1);        For ($k =0, $klen =16-count ($bitArray), $k < $klen, $k + +) Array_unshift ($bitArray, ' 0 ');        $bitArray = Array_slice ($bitArray, 0, ($leapMonth ==0?12:13)); for ($i =0; $i
 
  
lunarinfo[$year-$this->min_year];        $monthArray = $this->getlunaryearmonths ($year);        $len = count ($monthArray);    Return ($monthArray [$len -1]==0? $monthArray [$len-2]: $monthArray [$len-1]);        The Public Function getlunaryearmonths ($year) {$monthData = $this->getlunarmonths ($year);        $res =array ();        $temp = 0;        $yearData = $this->lunarinfo[$year-$this->min_year];        $len = ($yearData [0]==0?12:13);            for ($i =0; $i < $len; $i + +) {$temp = 0;            for ($j =0; $j <= $i; $j + +) $temp + = $monthData [$j];        Array_push ($res, $temp);    } return $res; }/** * Get leap Month * @param year lunar years */Public Function Getleapmonth ($year) {$yearData = $this-&gt        ; lunarinfo[$year-$this->min_year];    return $yearData [0]; /** * Calculates the number of days between lunar dates and the day of the month * @param year * @param month * @param date */Public function Getdaysbe   Tweenlunar ($year, $month, $date) {     $yearMonth = $this->getlunarmonths ($year);        $res = 0;        for ($i =1; $i < $month; $i + +) $res + = $yearMonth [$i-1];        $res + = $date-1;    return $res;     */** * Calculates the number of days between 2 Gregorian dates * @param year Gregorian * @param cmonth * @param cdate * @param the Gregorian month corresponding to the lunar month of Dmonth        * @param ddate Lunar Calendar Days * */Public Function getdaysbetweensolar ($year, $cmonth, $cdate, $dmonth, $ddate) {        $a = Mktime (0,0,0, $cmonth, $cdate, $year);        $b = Mktime (0,0,0, $dmonth, $ddate, $year);    Return Ceil (($a-$b)/24/3600); /** * Calculates the lunar date based on the number of days from the day of the month * @param year Gregorian * @param between days * * Public Function Getlunarbybetween    ($year, $between)        {$lunarArray = array ();        $yearMonth =array ();        $t = 0;        $e = 0;        $leapMonth = 0;        $m = ";            if ($between ==0) {Array_push ($lunarArray, $year, ' The lunar month ', ' the Junior ');            $t = 1;        $e = 1; } else {$year = $between >0? $yeAR: ($year-1);            $yearMonth = $this->getlunaryearmonths ($year);            $leapMonth = $this->getleapmonth ($year);            $between = $between >0? $between: ($this->getlunaryeardays ($year) + $between); for ($i =0; $i <13; $i + +) {if ($between = = $yearMonth [$i]) {$t = $i                    +2;                    $e = 1;                Break                    }else if ($between < $yearMonth [$i]) {$t = $i +1;                    $e = $between-(Empty ($yearMonth [$i-1])? 0: $yearMonth [$i-1]) +1;                Break }} $m = ($leapMonth!=0&& $t = = $leapMonth + 1)? (' Leap '. $this->getcapitalnum ($t -1,true)): $this->getcapitalnum (($leapMonth!=0&& $leapMonth +1< $t? (            $t-1): $t), true);        Array_push ($lunarArray, $year, $m, $this->getcapitalnum ($e, false)); } array_push ($lunarArray, $this->getlunaryearname ($year));//Zodiac Terrestrial ARRay_push ($lunarArray, $t, $e); Array_push ($lunarArray, $this->getyearzodiac ($year));//12 Zodiac Array_push ($lunarArray, $leapMonth);//LEAP months RET    Urn $lunarArray; /** * Gets the number of the lunar term * @param num number * @param ismonth is the number of months * * Public Function Getcapitalnum ($num, $i        Smonth) {$isMonth = $isMonth | | false; $dateHash =array (' 0 ' = ', ' 1 ' = ' one ', ' 2 ' = ' two ', ' 3 ' = ' three ', ' 4 ' = ' Four ', ' 5 ' = ' five ', ' 6 ' = ' Six ', ' 7 ' = ' Seven '        , ' 8 ' = ' eight ', ' 9 ' = ' nine ', ' ten ' = ' x '; $monthHash =array (' 0 ' = ', ' 1 ' = ' The ' Lunar Month ', ' 2 ' = ' February ', ' 3 ' = ' March ', ' 4 ' = ' April ', ' 5 ' = = ' May ', ' 6 ' + ' June ', ' 7 ' =        > ' July ', ' 8 ' = ' August ', ' 9 ' = ' September ', ' ten ' + ' October ', ' one ' ' = ' ' Winter months ', ' one ' = ' ' Lunar ';        $res = ";        if ($isMonth) $res = $monthHash [$num];            else {if ($num <=10) $res = ' early '. $dateHash [$num];            else if ($num >10&& $num <20) $res = ' ten '. $dateHash [$num-10];            else if ($num ==20) $res = "20"; else if ($num >20&& $num <30) $res = "20". $dateHash [$num-20];        else if ($num ==30) $res = "30";    } return $res;        }/** * Solar terms general algorithm */Public function Getjieqi ($_year, $month, $day) {$year = substr ($_year,-2) +0; $coefficient = Array (Array (5.4055,2019,-1),//Xiaohan Array (20.12,2082,1),//Big Chill Array (3.87) ,//spring Array (18.74,2026,-1),//Rainwater Array (5.63),//Jingzhe Array (20.646,2084,1),//Equinox ARR            Ay (4.81),//Ching Ming Array (20.1),//gu yu array (5.52,1911,1),//Li Xia Zhong Array (21.04,2008,1),//Xiaoman Array (5.678,1902,1),//Mang Chong Array (21.37,1928,1),//Summer solstice Array (7.108,2016,1),//Slight heat array (22.83,            1922,1),//Great Heat Array (7.5,2002,1),//The beginning of Autumn Array (23.13),//Chushu Array (7.646,1927,1),//White Dew Array (23.042,1942,1),//Autumnal Equinox Array (8.318),//Dew Array (23.438,2089,1),//Frost Array (7.438,2089,1 ),//winter Array (22.36,1978,1),//Snow Array (7.18,1954,1),//Snow Array (21.94,2021,-1)//winter solstice); $term _name = Array ("Xiaohan", "Big Chill", "Spring", "Rain", "Jingzhe", "Vernal Equinox", "Qingming", "Gu Yu", "Li Xia Zhong", "Xiaoman", "Mang Chong", "Summer solstice", "slight heat", "Great Heat", "Autumn", "Chushu"        , "White dew", "Autumnal Equinox", "Dew", "Frost", "Winter", "Snow", "Snow", "Winter Solstice");        $idx 1 = ($month-1) * *;        $_leap_value = Floor (($year-1)/4);        $day 1 = Floor ($year *0.2422+ $coefficient [$idx 1][0])-$_leap_value;        if (Isset ($coefficient [$idx 1][1]) && $coefficient [$idx 1][1]==$_year) $day 1 + = $coefficient [$idx];        $day 2 = Floor ($year *0.2422+ $coefficient [$idx 1+1][0])-$_leap_value;        if (Isset ($coefficient [$idx 1+1][1]) && $coefficient [$idx 1+1][1]==$_year) $day 1 + = $coefficient [$idx 1+1][2];        if ($day = = $day 1) return $term _name[$idx 1];        if ($day = = $day 2) return $term _name[$idx];    Return ';        }/** * Get festivals: Special Festivals You can only modify this function to calculate the */Public function getfestival ($today, $nl _info = False, $config = 1) {      if ($config = = 1)  {$arr _lunar=array (' 01-01 ' = ' Spring Festival ', ' 01-15 ' and ' Lantern Festival ', ' 02-02 ' = ' two ', ' 05-05 ' = ' dragon Boat Festival ', ' 07-07 ' and ' Tanabata festival ', '            08-15 ' + ' Mid-Autumn Festival ', ' 09-09 ' + ' Chung Yeung Festival ', ' 12-08 ' and ' Laba Festival ', ' 12-23 ' = ' off-the-moon '; $arr _solar=array (' 01-01 ' = ' New Year ', ' 02-14 ' and ' Valentine's Day ', ' 03-12 ' + ' Arbor Day ', ' 04-01 ' = ' April Fool ', ' 05-01 ' and ' Labour ', '        06-01 ' = ' children's Day ', ' 10-01 ' and ' National holiday ', ' 10-31 ' and ' Halloween ', ' 12-24 ' and ' Eve ', ' 12-25 ' and ' Christmas ';        }//need different festivals, with different $config, then configure $arr_lunar and $arr_solar $festivals = Array ();        List ($y, $m, $d) = Explode ('-', $today);        if (! $nl _info) $nl _info = $this->convertsolartolunar ($y, Intval ($m), Intval ($d));        if ($nl _info[7]>0&& $nl _info[7]< $nl _info[4]) $nl _info[4]-=1; $MD _lunar = substr (' 0 '. $nl _info[4],-2). '        -'. substr (' 0 '. $NL _info[5],-2);        $MD _solar=substr_replace ($today, ", 0,5);        Isset ($arr _lunar[$md _lunar]) Array_push ($festivals, $arr _lunar[$md _lunar]): "; Isset ($arr _solar[$md _solar]) Array_push ($festivals, $arr _solar[$md _solar]): ";       $glweek = Date ("W", Strtotime ($today));        0-6 if ($m ==5&& ($d >7) && ($d <15) && ($glweek ==0)) Array_push ($festivals, "Mother's Day");        if ($m ==6&& ($d >14) && ($d <22) && ($glweek ==0)) Array_push ($festivals, "Father's Day");        $jieqi = $this->getjieqi ($y, $m, $d);        if ($jieqi) Array_push ($festivals, $jieqi);    Return implode ('/', $festivals); }}
 

Call Method:

$lunar = new Lunar (), $month = $lunar->convertsolartolunar (the date (' Y '), date (' m '), date (' d '));p Rint_r ($month);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.