PHP achieves mutual conversion between the Gregorian calendar date and the lunar calendar date.
PHP achieves mutual conversion between Gregorian and lunar dates
Preface:
Today, according to the customer's needs, the time was converted, that is, the customer requested to increase the display of the lunar date, copied a section on the Internet, and slightly modified it to run successfully, it is not difficult, few changes.
Sample Code:
<? Php/*** *** // lunar. php creates this php file, and all the Code does not need to be changed. The following must be modified: teacher. php class Lunar {private $ _ SMDay = array (1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 30, 31); // defines the number of days in a calendar month. private $ _ LStart = 1950; // the calendar year starts from January 1, 1950. private $ _ LMDay = array (// difference: the number of days from January 1, lunar January 1 to January 1, January 1 ~ 12: the number of days of the lunar month; Second: if there is a leap month, record the number of days of the month. // The difference is 1 2 3 4 5 6 7 8 9 10 11 12 hour array (47,29, 30,30, 29,30, 30,29, 29,30, 29,30, 29), array (36,30, 29,30, 30,29, 30,29, 30,29, 30,29, 30), array (6,29, 30,29, 30,59, 29,30, 30,29, 30,29, 30,29), // May 29 May 30 array (44,29, 30,29, 29,30, 30,29, 30,30, 29,30, 29), array (, 29,30, 29,29, 30,29, 30,30, 30,30, 29,30, 30), array (, 30, 59, 29,29, 30,29, 30,29, 30,29, 30,30, 30,29), // December 31, bytes array (, 3 0, 29, 30, 29, 30), array (30, 30, 29, 30, 29, 29, 29, 29, 30, 29, 30), // August 30 values array (, 30, 30, 30,29, 30,29, 30), array (38,29, 30,30, 29,30, 29,30, 29,30, 29,30, 29), array (27,30, 29,30, 29,30, 59,30, 29,30, 29,30, 29,30), // August 29, array (45,30, 29,30, 29,30, 29,30, 30,29, 30,29, 30), array (35,29, 30,29, 29,30, 29,30, 30,29, 30,30, 29), array (24,30, 29,30, 58,30, 29,30, 29,30, 30,30, 29,29), // August 15, arra Y (, 29, 30, 29, 29, 30, 29, 30, 30), array (, 29, 30, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 30, 29), array (20, 30, 30, 59, 30,29, 29,30, 29,29, 30,30, 29,30), // March 30 values array (39,30, 30,29, 30,30, 29,29, 30,29, 30,29, 30,29, 30), array (29,29, 30,29, 29, 30,30, 29,59, 30,29, 30,29, 30,30), // July 30 values July 29 array (47,29, 30,29, 30,29, 30,30, 29,30, 29,30, 29), array (36,30, 29,29, 29, 30,29, 30,30, 29,30, 30,29, 30), array (26,29, 30,29, 29,59, 30,29, 30,30, 30,29, 3 0, 30), // December 31, May 30 array (45, 29, 30, 29, 30, 29, 30, 29, 30, 30), array (May 29, 30, 29, 30, 29, 29, 30, 29, 29, 29, 29, 30, 30, 30, 29, 29, 30), array (, 30, 29, 59,29, 30, 29, 29,30, 30,29, 30,30), // August 30, bytes array (, 30, 30,29, 30,29, 29,30, 29,29, 30,29, 30), array (30,30, 30,29, 30,29, 30,29, 59,29, 30,29, 30,30), // August 30, August 30 bytes array (August 29, 29,30, 30,29, 30,29, 30,29, 30,29, 29), array (37,30, 29,30, 30,29, 30,30, 29,30, 29,30, 29, 29), array (27,30, 29,29, 3 0, 29, 60, 29, 30, 30, 29, 30), // October 31, December 31, array (, 30, 30, 30, 30, 30), array (, 29, 29, 29,30, 29,29, 30,30, 29,30, 30), array (24,30, 29,30, 58,30, 29,29, 30,29, 30,30, 30,29), // August 29, April 29, array (, 29,30, 30, 29,29, 30,29, 29,30, 29,30, 30), array (32,30, 29,30, 30,29, 29,30, 29,29, 59,30, 30,30), // August 29, array (50,29, 30,30, 30, 29,30, 29,30, 29,29, 30,29, 30), array (39,29, 30,30, 29,30, 30,29, 30,29, 30,29, 29), array (28,30, 29,30, 29,30, 59,30, 30,29, 30,29, 29,30), // August 30, bytes array (47,30, 29,30, 29,30, 29,30, 30,30, 30,30, 29), array (36,30, 29,29, 30,29, 30,29, 30,30, 30, 30), array (26,29, 30,29, 29,59, 29,30, 29,30, 30,30, 30,30 ), // May 30 values array (, 30, 29, 29, 29, 30, 30, 30), array (, 30, 30, 30, 29, 29, 29, 29, 30, 30, 30, 29, 30, 29, 30, 30 ), array (,), // March 30 March 29 array, 29,30, 29,30, 29,29, 30,29, 30), array (30,29, 30,30, 29,30, 29,30, 59,29, 30,29, 30,30), // August 30, August 30, August 29, array (49,29, 30,29, 30,30, 29,30, 29,30, 30,29, 29), array (37,30, 29,30, 29,30, 29,30, 30,29, 30,30, 29), array (27,30, 29,29, 30,58, 30,30, 29,30, 30,29, 30,29), // May 29 May 29 array (, 29,29, 30,29, 29,30, 29,30, 30,30, 29), array (35,30, 30,29, 29,30, 29,29, 29,29, 30,29, 30,30, 29), array (,), // April 30 April 29 array (, 29, 29, 29), array (, 30, 30, 30, 30, 30, 30, 29, 29, 29, 29, 29, 29, 29, 30, 29, 29, 29, 30, 30), array (, 29, 59,30, 30,29, 30,29, 30,29, 30,29, 30,30), // February 30 values array (39,29, 30,29, 30,29, 30,30, 29,30, 29,30, 29), array (28,30, 30, 29,30, 29,30, 29,59, 30,30, 29,30, 30,30), // July 30 values array (, 29,30, 29,29, 30,29, 30,29, 30,30, 30,29, 30), array (, 30, 29,29, 30,29, 29,30, 29,30, 30,29, 30), array (25,30, 30,29, 29,59, 29,30, 29, 30,29, 30,30, 30), // August 29, array (, 29,30, 29,30, 29,29, 30,29, 30,29, 30), array (, 29,30, 30,29, 30,29, 30,29, 29,30, 29,30, 29), array (, 29,30, 59,30, 29,30, 29,30, 29,30, 29,30, 29,30), // April 30 values array (, 29,30, 29,30, 29,30, 30,29, 30,29, 30,29, 30), array (30,29, 30,29, 30,29, 30,29, 30,29, 30,59, 30,29, 30,30), // October September 30 runtime array (49,29, 30,29, September 29, 29,30, 30,30, 29,30, 29), array (38,30, 29,30, 29,29, 30,29, 30,30, 29,30, 30), array (27, 29,30, 29,30, 29,59, 29,30, 29,30, 30,30, 29), // August 29, June 29, June 30, array (, 30, 30 ), array (35,30, 29,30, 29,30, 29,29, 30,29, 29,30, 30), array (24,29, 30,30, 59,30, 29,29, 30,29, 30,29, 30,30 ), // April 30 April 29 array (, 30, 30, 29,30, 29,30, 29,30, 29,30, 29,30, 29), array (, 29,30, 29,30, 30,29, 30,29, 30,29, 30,29, 30 ), array (, 30, 30, 30, 30, 29, 30, 30, 30), // February 30 February 29 array (, 30, 29,30, 30,29), array (28, 30, 29,30, 29,29, 59,30, 29,30, 30,30, 29,30), // June 30 values array (47,30, 29,30, 29,29, 30,29, 29,30, 30,30, 29), array (36,30, 30,29, 30,29, 29,30, 29,29, 30,30, 29), array (25,30, 30,30, 29,59, 29,30, 29,29, 30,30, 29,30 ), // May 30 May 29 array (, 30, 29, 30, 29, 29, 29, 30, 30), array (, 29, 30, 30, 30, 29, 30, 29 ), array (,), // March 30 March 29 array (41, 30,29, 29,30, 29,30, 30,29, 30,30, 29,30), array (30,29, 30,29, 29,30, 29,30, 29,30, 30,59, 30,30), // November 30 November 29 array (, 29, 30,29, 29,30, 29,30, 29,30, 30,29, 30), array (38,30, 29,30, 29,29, 30,29, 29,30, 30,29, 30), array (27,30, 30,29, 30,29, 59,29, 29,30, 29,30, 30,29), // August 29, June 29 array (June 30, 30,29, 30,29, 29,30, 29,29, 30,29, 30), array (34,30, 30,29, 30,29, 30,29, 30,29, 30,29, 29,30, 29), array (, 30,29, 30,59, 30,29, 30,29, 30,29, 30,29, 30), // December 31, array (, 30, 29, 30, 30, 30, 29, 29), array (, 30, 29, 30, 30, 29, 30, 30, 30, 30, 30, 30, 29, 30), array (, 30, 29, 30, 30, 30, 30, 30), // August 30, bytes array (, 29,30, 29,29, 30,30, 29,30, 29,30, 30), array (29,30, 29,30, 29,29, 30,58, 30,29, 30,30, 30,29), // August 29, bytes array (47,30, 29,30, 29,29, 30,29, 29,30, 29,30, 30), array (36,30, 29,30, 29,30, 29,30, 29,29, 30,29, 30), array (25,30, 29,30, 30, 59,29, 30,29, 29,30, 29,30, 29), // May 29, December 29, array (, 29, 29, 29, 29, 30, 29), array (, 30, 29,30, 30,29, 30,30, 29,30, 29), array (March 29, 30,59, 29,30, 29,30, 30,29, 30,30, 29,29), // August 1, March 30 ); // whether to perform the private function IsLeapYear ($ AYear) {return ($ AYear % 4 = 0) & ($ AYear % 100! = 0) | ($ AYear % 400 = 0);} // calendar the number of days of the month (year: year; month: month) private function GetSMon ($ year, $ month) {if ($ this-> IsLeapYear ($ year) & $ month = 2) return 29; else return $ this-> _ SMDay [$ month];} // convert the lunar Name to private function LYearName ($ year) {$ Name = array ("zero", "one", "two", "three", "four ", "5", "6", "7", "8", "9"); for ($ I = 0; $ I <4; $ I ++) for ($ k = 0; $ k <10; $ k ++) if ($ year [$ I] = $ k) $ tmp. = $ Name [$ k]; return $ tmp;} private functio N LMonName ($ month) {if ($ month >=1 & $ month <= 12) {$ Name = array (1 => "positive", "2 ", "3", "4", "5", "6", "7", "8", "9", "10", "11 ", "12"); return $ Name [$ month];} return $ month;} private function LDayName ($ day) {if ($ day >=1 & $ day <= 30) {$ Name = array (1 => "", "", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th ", "14", "15", "16", "17", "18", "19", "20", "yi", "Yi 2 ", "Jun 3", "Jun 4", "Jun 5", "Jun 6", "Jun 7", "Jun 8", "Jun 9", "Thirty" ); Return $ Name [$ day];} return $ day;} // convert the Gregorian calendar to the lunar calendar (Sdate: Gregorian date) public function S2L ($ date) {list ($ year, $ month, $ day) = explode ("-", $ date ); if ($ year <= 1951 | $ month <= 0 | $ day <= 0 | $ year> = 2051) return false; // obtain the number of days from the query date to January 1, January 1 of the current year. $ date1 = strtotime ($ year. "-01-01"); // January 1 $ date2 = strtotime ($ year. "-". $ month. "-". $ day); $ days = round ($ date2-$ date1)/3600/24); $ days + = 1; // obtain the lunar data of the corresponding year and convert it into an array Larray $ Larray = $ this-> _ LMDay [$ year-$ this-> _ LStart]; if ($ days <= $ Larray [0]) {$ Lyear = $ year-1; $ days = $ Larray [0]-$ days; $ Larray = $ this-> _ LMDay [$ Lyear-$ this-> _ LStart]; if ($ days <$ Larray [12]) {$ Lmonth = 12; $ Lday = $ Larray [12]-$ days;} else {$ Lmonth = 11; $ days = $ days-$ Larray [12]; $ Lday = $ Larray [11]-$ days ;}} else {$ Lyear = $ year; $ days = $ days-$ Larray [0]; for ($ I = 1; $ I <= 12; $ I ++) {If ($ days> $ Larray [$ I]) $ days = $ days-$ Larray [$ I]; else {if ($ days> 30) {$ days = $ days-$ Larray [13]; $ Ltype = 1 ;}$ Lmonth = $ I; $ Lday = $ days; break ;}}} return mktime (0, 0, 0, $ Lmonth, $ Lday, $ Lyear); // $ Ldate = $ Lyear. "-". $ Lmonth. "-". $ Lday; // $ Ldate = $ this-> LYearName ($ Lyear ). "year ". $ this-> LMonName ($ Lmonth ). "month ". $ this-> LDayName ($ Lday); // if ($ Ltype) $ Ldate. = "(period)"; // return $ Ldate;} // agricultural Convert a calendar to a Gregorian calendar (date: Lunar date; type: whether it is a leap month) public function L2S ($ date, $ type = 0) {list ($ year, $ month, $ day) = split ("-", $ date); if ($ year <= 1951 | $ month <= 0 | $ day <= 0 | $ year> = 2051) return false; $ Larray = $ this-> _ LMDay [$ year-$ this-> _ LStart]; if ($ type = 1 & count ($ Larray) <= 12) return false; // query required, however, no leap month is found. // if the queried calendar month is a leap month and the year's calendar array contains the leap month data, you can obtain if ($ Larray [$ month]> 30 & $ type = 1 & & count ($ Larray)> = 13) $ day = $ Larray [13] + $ day; // obtain the number of days from the lunar calendar day of the year to the Gregorian calendar of the month. $ days = $ day; for ($ I = 0; $ I <= $ month-1; $ I ++) $ days + = $ Larray [$ I]; // if ($ days> January 1 | ($ this-> GetSMon ($ month, 2) when the query date is more than one year from the Gregorian calendar on January 1, 366 )! = 29 & $ days> 365) {$ Syear = $ year + 1; if ($ this-> GetSMon ($ month, 2 )! = 29) $ days-= 366; else $ days-= 365; if ($ days> $ this-> _ SMDay [1]) {$ Smonth = 2; $ Sday = $ days-$ this-> _ SMDay [1];} else {$ Smonth = 1; $ Sday = $ days ;}} else {$ Syear = $ year; for ($ I = 1; $ I <= 12; $ I ++) {if ($ days> $ this-> GetSMon ($ Syear, $ I )) $ days-= $ this-> GetSMon ($ Syear, $ I); else {$ Smonth = $ I; $ Sday = $ days; break ;}}} return mktime (0, 0, 0, $ Smonth, $ Sday, $ Syear); // $ Sdate = $ Syear. "-". $ Smonth. "-". $ Sday; // return $ Sdate ;}}?>
Teacher. php
// Convert the Gregorian calendar to require_once (CORE _. "Lunar. php "); // load lunar. PHP File $ today = date ("Y-m-d"); $ lunar = new Lunar (); // instantiate class $ nonglitime = array (); $ nl = date ("Y-n-d", $ lunar-> S2L ($ today); echo $ nl; exit; // convert to lunar calendar:
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!