PHP blog, PHP implementation of lunar calendar to lunar calendar, PHP implementation of lunar calendar to the source code, PHP technology blog, PHP date function
The lunar calendar is also called the lunar calendar. it takes a week for the moon to circle the Earth, that is, 29 days, 12 hours, 44 minutes, and 3 seconds. it takes 354 days for 12 months a year.
The Gregorian calendar is also called the Gregorian calendar. it takes 12 months for the Earth to cycle around the sun for one year, that is, 365 days, 5 hours, 48 minutes, and 46 seconds, the Chinese lunar calendar is not a pure lunar calendar, but a combination of yin and yang. it is a calendar that combines the lunar calendar and the Gregorian calendar, because the year of the lunar calendar is about 11 days different, in order to coordinate them, the leap month method is implemented, that is, the 19th year, the seventh day, so that the Yin and Yang calendar can be coordinated.
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. for example, today is the calendar month, month, day, and week.
* @ 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' = & gt; '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;