Php Lunar Calendar-conversion code

Source: Internet
Author: User
It's a good source for GOOGLE on the Internet. If you need it, you can refer to it.

It's a good source for GOOGLE on the Internet. If you need it, you can refer to it.

The Code is as follows:
Class Lunar {
Var $ MIN_YEAR = 1891;
Var $ MAX_YEAR = 2100;
Var $ lunarInfo = array (
Array (,), array, 13,59728 ),
Array (,), array (), array, 8, 42352 ),
Array (,), array, 2, 32 ),
Array (,), array, 26,54568 ),
Array (,), array, 20,18800 ),
Array (,), array, 13,11104 ),
Array (,), array, 6 ),
Array (,), array, 31,51560 ),
Array (,), array (, 1, 25,43736 ),
Array (,), array, 17,27808 ),
Array (,), array, 12,21168 ),
Array (,), array, 5, 19296 ),
Array (,), array, 30,23208 ),
Array (,), array, 23,53864 ),
Array (,), array, 16,38320 ),
Array (,), array, 9,27968 ),
Array (,), array, 4,25776 ),
Array (,), array, 28,37736 ),
Array (,), array, 22,23208 ),
Array (,), array, 14,43344 ),
Array (,), array, 8, 16 ),
Array (,), array, 1, 44368 ),
Array (,), array, 26,59696 ),
Array (,), array, 19,19152 ),
Array (,), array, 12,46496 ),
Array (, rows), array, 6 ),
Array (,), array, 1, 18808 ),
Array (,), array, 24,43872 ),
Array (,), array, 17,54432 ),
Array (,), array, 11,21200 ),
Array (,), array (), array, 5, 21920 ),
Array (, 24, 48), array (, 12,), array (,), array, 29,29336 ),
Array (,), array, 24,21104 ),
Array (,), array, 15,22133 ),
Array (,), array, 9,54560)
);
/**
* Convert a solar calendar to a lunar calendar
* @ Param year-year
* @ Param month: Calendar-month
* @ Param date: 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, 'August 11', '1st 1', '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, 'August 11', '1st 1', 'sinmao ', lunar January, 'rabbit ');
}
$ Month_days_ary = array (31, 28, 31, 30, 31, 30, 31, 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 a calendar to a calendar
* @ Param year: Lunar calendar-year
* @ Param month: Lunar Calendar-month, leap month processing: for example, if the current year expires on January 1, then the second month will be transferred on January 1, 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 );
}
/**
* Determines whether a leap year is used.
* @ Param year
*/
Function isLeapYear ($ year ){
Return ($ year % 4 = 0 & $ year % 100! = 0) | ($ year % 400 = 0 ));
}
/**
* Obtain the credit year
* @ Param year
*/
Function getLunarYearName ($ year ){
$ Sky = array ('geng', 'sin', 'hangzhou', 'cap', 'A', 'B', 'bing', 'ding ', 'pente', 'ji ');
$ Earth = array ('shen', 'uni', 'shanghai', 'sub', 'Ugly ', 'yin', 'mao ', 'chen ', 'si', 'wu ', 'wei ');
$ Year = $ year .'';
Return $ sky [$ year {3}]. $ earth [$ year % 12];
}
/**
* Obtain the Chinese zodiac Based on the lunar calendar year
* @ Param year: Lunar calendar year
*/
Function getYearZodiac ($ year ){
$ Zodiac = array ('Monkey ', 'chicken', 'Dog', 'pig', 'rat ', 'ox', 'tiger ', 'rabbit ', 'long', 'snake ', 'Ma', 'yang ');
Return $ zodiac [$ year % 12];
}
/**
* Obtain the number of days of the calendar month.
* @ Param year: 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"];
}
/**
* Obtain the number of days in 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 <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 $ BitArray [$ I] = $ bitArray [$ I] + 29;
}
Return $ bitArray;
}
/**
* Obtain the number of days of the lunar calendar year
* @ Param year: Lunar year
*/
Function getLunarYearDays ($ year ){
$ YearData = $ this-> 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;
}
/**
* Obtain a leap month
* @ Param year indicates the year of the lunar calendar.
*/
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
* @ 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 of the lunar calendar
*/
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 );
}
/**
* Calculate the calendar date based on the number of days from January 1, lunar January 1.
* @ Param year
* @ Param between number of days
*/
Function getLunarByBetween ($ year, $ ){
// Debugger;
$ LunarArray = array ();
$ YearMonth = array ();
$ T = 0;
$ E = 0;
$ LeapMonth = 0;
$ M = '';
If ($ between = 0 ){
Array_push ($ lunarArray, $ year, 'October 11', '1st year ');
$ 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) + $ );
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); // daily support
Array_push ($ lunarArray, $ t, $ e );
Array_push ($ lunarArray, $ this-> getYearZodiac ($ year); // 12 zodiac
Array_push ($ lunarArray, $ leapMonth); // months of renewal
Return $ lunarArray;
}
/**
* Obtain the calendar name of a number
* @ 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' => '20180101', '2' => '20180101', '3' => '20180101 ', '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 = "logs". $ dateHash [$ num-20];
} Else if ($ num = 30 ){
$ Res = "Thirty ";
}
}
Return $ res;
}
}
$ Lunar = new Lunar ();
$ Month = $ lunar-> convertLunarToSolar (2012 );
Print_r ($ month );
Exit;
Related Article

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.