Php implementation of the conversion of the Gregorian and lunar calendars

Source: Internet
Author: User
This article mainly introduces how php achieves the conversion of the Gregorian calendar and the Chinese name of the calendar.

This article mainly introduces how php achieves the conversion of the Gregorian calendar and the Chinese name of the calendar.

Recently, I was interested in the algorithm used to convert the Gregorian calendar to the lunar calendar, so I searched the internet. Yes, I found a pretty good php class. I realized the algorithm of converting the Gregorian calendar to the lunar calendar, and converting the Gregorian calendar to the Gregorian calendar. At the same time, I also got the algorithms, and the corresponding Chinese name of the lunar calendar. The functions are quite complete and we will share with you here.
The specific class code is as follows:

Class Lunar {var $ MIN_YEAR = 1891; var $ MAX_YEAR = 2100; var $ lunarInfo = array (,), array, 17,9584), array (,), array ), array (,), array, 16, 53856), array (,) 6), array (, rows 32), array ), array (,), array, 11,38320), array (,), array ), array (,), arra Y (,), array (,), array (), array, 26,27976), array (,), array ), array (,), array (, 1, 25, 43736), array (,), array, ), Array ), array (,), array, 8, 27296), array (6, 1, 28, 44368), array ), array (30,232,), array (,) 08), array (,), array ), array (,), array (6, 1, 28,), array (), array (,), array ), array (,), Rray (,), array (,), array, 10, 59984), array ), array (,), array (, 24, 54440), array (,), array, 9, 22176), array (,), array () ,), Array (), array ), array (,), array, 25, 29864), array ), array (, 17,42208), array (,) 4576), array (,), array ), array (,), array, 12, 46496), array (, rows), array ), array (,), array), Array (,), array (), array (,), array (,), array (6, 1, 28, 27216), array ), array (,), array, 26,23208), array (,), array (,), Array ), array (,), array (, 24, 48), array (, 12,), array (,), array, ), array ), array (,), array (, 30, 59696), array (,), array ), array (,), array )); /*** convert the Gregorian calendar to the Gregorian calendar * @ param year Gregorian calendar-year * @ param month Gregorian calendar-month * @ param date Gregorian calendar-day */function convertSolarToLunar ($ year, $ month, $ date) {// debugger; $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; if ($ year = $ this -> MIN_YEAR & $ month <= 2 & $ date <= 9) {return array (1891, 'july 1', '1n1', 'sinmao ', lunar January, rabbit);} return $ this-> getLunarByBetween ($ year, $ this-> getDaysBetweenSolar ($ year, $ month, $ date, $ yearData [1], $ yearData [2]);} function convertSolarMonthToLunar ($ year, $ month) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; if ($ year = $ this-> MIN_YEAR & $ month <= 2 & $ date <= 9) {return array (1891, 'October 11', '1e ', 'simmo', 'rabbit ');} $ month_days_ary = array (, 28, 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-> getDaysBetweenSolar ($ year, $ month, $ I, $ yearData [1], $ yearData [2]); $ array [] = $ year. '-'. $ month. '-'. $ I; $ lunar_ary [$ I] = $ array;} return $ lunar_ary ;} /*** convert the lunar calendar to the Gregorian calendar * @ param year the lunar calendar-year * @ param month the lunar calendar-month, for example, if the current year is renewed In May, the second month of May is passed to June, which is equivalent to 13 months in the calendar, sometimes, the number of days in the first month is 0 * @ param date: day */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);}/*** determine whether it is a leap year * @ param year */function isLeapYear ($ year) {return ($ year % 4 = 0 & $ year % 100! = 0) | ($ year % 400 = 0);}/*** obtain the year of the credit calculation * @ param year */function getLunarYearName ($ year) {$ sky = array ('geng ', 'sin', 'shen', 'cap', 'A', 'B', 'bing', 'ding ', 'pente', 'jid'); $ earth = array ('shen', 'uni', 'shanghai', 'sub', 'ug ', 'yin ', 'mao', 'chen ', 'si', 'wu ', 'wei'); $ year = $ year. ''; return $ sky [$ year {3}]. $ earth [$ year % 12];}/*** obtain the zodiac sign based on the lunar year * @ param year lunar year */function getYearZodiac ($ year) {$ zodiac = array ('Monkey ', 'chicken', 'Dog', 'pig', 'rat ', 'ox', 'tiger ', 'rabbit ', 'long', 'snake ', 'Ma', 'yang'); return $ zodiac [$ year % 12]; }/*** Get the number of days of the calendar month * @ param year calendar-year * @ param month calendar-month */function getSolarMonthDays ($ year, $ month) {$ monthHash = array ('1' => 31, '2' => $ this-> isLeapYear ($ year )? 29:28, '3' => 31, '4' => 30, '5' => 31, '6' => 30, '7' => 31, '8' => 31, '9' => 30, '10' => 31, '11' => 30, '12' => 31 ); return $ monthHash ["$ month"];}/*** get the number of days of the calendar month * @ param year lunar calendar-year * @ param month Lunar Calendar-month, starting from January 1, January */function getLunarMonthDays ($ year, $ month) {$ monthData = $ this-> getLunarMonths ($ year); return $ monthData [$ month-1];} /*** obtain the array of the number of days of the calendar month * @ param year */function getLunarMonths ($ year) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; $ leapMonth = $ yearData [0]; $ bit = decbin ($ yearData [3]); 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]);} function getLunarYearMonths ($ year) {// debugger; $ 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 a leap month * @ param year lunar year */function getLeapMonth ($ year) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; return $ yearData [0];}/*** calculate the number of days between the lunar calendar date and January 1, lunar January 1 * @ param year * @ param month * @ param date */function getDaysBetweenLunar ($ year, $ month, $ date) {$ yearMonth = $ this-> getLunarMonths ($ year ); $ Res = 0; for ($ I = 1; $ I <$ month; $ I ++) {$ res + = $ yearMonth [$ I-1];} $ res + = $ date-1; return $ res ;} /*** calculate the number of days between two calendar dates * @ param year calendar year * @ param cmonth * @ param cdate * @ param dmonth the calendar month corresponding to January 1, lunar January * @ param ddate the number of calendar days corresponding to the first day */function getDaysBetweenSolar ($ year, $ cmonth, $ cdate, $ dmonth, $ ddate) {$ a = mktime (0, 0, $ cmonth, $ cdate, $ year); $ B = mktime (0, 0, 0, $ dmonth, $ ddate, $ year); return ceil ($ a-$ B)/24/3600 );} /*** calculate the lunar calendar date based on the number of days from January 1, lunar January 1 * @ p Aram year yangnian * @ param between days */function getLunarByBetween ($ year, $ between) {// debugger; $ lunarArray = array (); $ yearMonth = array (); $ t = 0; $ e = 0; $ leapMonth = 0; $ m = ''; if ($ between = 0) {array_push ($ lunarArray, $ year, 'february 11', '1day'); $ 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 )? ('Authorization'. $ 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); // array_push ($ lunarArray, $ t, $ e); array_push ($ lunarArray, $ this-> getYearZodiac ($ year); // 12 zodiac array_push ($ lunarArray, $ leapMonth); // returns $ lunarArray ;} /*** obtain the digit's lunar name * @ param num Number * @ param isMonth is the number of the month */function getCapitalNum ($ num, $ isMonth) {$ isMonth = $ isMonth | false; $ dateHash = array ('0' => '', '1' => '1 ', '2' => '2', '3' => '3', '4' => '4', '5' => '5 ', '6' => '6', '7' => '7', '8' => '8', '9' => '9 ', '10' => '10'); $ monthHash = array ('0' => '', '1' => 'August 1 ', '2' => 'August 11', '3' => 'August 11', '4' => 'August 11', '5' => 'August 11 ', '6' => 'August 11', '7' => 'August 11', '8' => 'August 11', '9' => 'August 11 ', '10' => 'August 11', '11' => 'August 11', '12' => 'August 11'); $ res = ''; if ($ isMonth) {$ res = $ monthHash [$ num];} else {if ($ num <= 10) {$ res = 'chu '. $ dateHash [$ num];} else if ($ num> 10 & $ num <20) {$ res = '10 '. $ dateHash [$ num-10];} else if ($ num = 20) {$ res = "20";} else if ($ num> 20 & $ num <30) {$ res = "Hangzhou ". $ dateHash [$ num-20];} else if ($ num = 30) {$ res = "Thirty" ;}} return $ res ;}}

The usage is as follows:

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.