PHP Lunar Calendar Conversion
- /*
- School of Computer Science and Engineering, Qujing Normal University, Yunnan province-Edited by Yang Haixi
- 9-9-3
- */
- Class Lunar
- {
- Private $ _ SMDay = array (1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 ); // define the calendar month/day
- Private $ _ LStart = 1950; // the lunar calendar starts from January 1, 1950.
- Private $ _ LMDay = array (
- // Difference: the number of days from January 1, lunar January 1 to January 1, January 1 of the year ~ 12: The number of days of the lunar month; second: if a leap month exists, record the number of days of the month.
- // Difference 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, 29, 30, 29, 30, 29), // August 15, May 29, May 30
- Array (44,29, 30,29, 29,30, 30,29, 30,30, 29,30, 29 ),
- Array (, 30, 30, 30 ),
- Array (,), // August 15
- Array (, 30, 30 ),
- Array (,), // August 15
- Array (, 30, 30, 29, 29, 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 15, June 30, June 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, April 29, April 29
- Array (, 30, 30, 30 ),
- Array (32, 29, 30, 29, 30, 29, 29, 29, 29, 30, 29 ),
- Array (,), // August 15
- Array (39,30, 30,29, 30,30, 29,29, 30,29, 30,29, 30 ),
- Array (29,29, 30,29, 30,30, 29,59, 30,29, 30,29, 30,30), // August 15, July 30, July 29
- Array (47,29, 30,29, 30,29, 30,30, 29,30, 29,30, 29 ),
- Array (36,30, 29,29, 30,29, 30,30, 29,30, 30,29, 30 ),
- Array (26,29, 30,29, 29,59, 30,29, 30,30, 30,29, 30,30), // August 15, May 30, May 29
- Array (45,29, 30,29, 29,30, 29,30, 29,30, 30,29, 30 ),
- Array (, 30 ),
- Array (,), // August 15
- Array (, 30 ),
- Array (30,30, 30,29, 30,29, 30,29, 59,29, 30,29, 30,30), // August 15, August 30, August 29
- Array (, 29, 29 ),
- Array (37,30, 29,30, 30,29, 30,30, 29,30, 29,30, 29 ),
- Array (27,30, 29,29, 30,29, 60,29, 30,30, 29,30, 29,30), // August 15, June 30, June 30
- Array (, 30, 30 ),
- Array (35,29, 30,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 15, April 29, April 29
- Array (, 30, 30 ),
- Array (,), // August 15
- Array (50,29, 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 15, June 30, June 29
- Array (47,30, 29,30, 29,30, 29,30, 30,29, 30,30, 29 ),
- Array (36,30, 29,29, 30,29, 30,29, 30,29, 30,30, 30 ),
- Array (,), // August 15
- Array (45,29, 30,29, 29,30, 29,29, 30,29, 30,30, 30 ),
- Array (34,29, 30,30, 29,29, 30,29, 29,30, 29,30, 30 ),
- Array (,), // August 15
- Array (, 30 ),
- Array (30,29, 30,30, 29,30, 29,30, 59,29, 30,29, 30,30), // August 15, 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), // August 15, May 29, May 29
- Array (, 29, 29 ),
- Array (35,30, 30,29, 29,30, 29,29, 30,29, 30,30, 29 ),
- Array (,), // August 15
- Array (, 29, 29, 29 ),
- Array (, 30, 30, 30 ),
- Array (,), // August 15
- Array (39,29, 30,29, 30,29, 30,30, 29,30, 29,30, 29 ),
- Array (,), // August 15
- Array (, 30, 30 ),
- Array (37,30, 29,29, 30,29, 29,30, 29,30, 30,29, 30 ),
- Array (,), // August 15
- Array (, 30 ),
- Array (, 29, 29, 29 ),
- Array (,), // August 15
- Array (, 30, 30 ),
- Array (30,29, 30,29, 30,29, 30,29, 30,29, 30,59, 30,29, 30,30), // August 15, September 30, September 29
- Array (49,29, 30,29, 29,30, 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 15, 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), // August 15, April 30, April 29
- Array (, 30, 30, 29 ),
- Array (, 30, 29, 30 ),
- Array (, 30, 30,), // August 15
- Array (, 29 ),
- Array (,), // August 15
- 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 (, 30, 30,), // August 15
- Array (, 30, 30 ),
- Array (, 29 ),
- Array (, 30, 30, 30,), // August 15
- Array (, 29, 30, 30, 30, 30 ),
- Array (,), // August 15
- Array (49,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 15, June 29, June 30
- Array (45,30, 30,29, 30,29, 29,30, 29,29, 30,29, 30 ),
- Array (34,30, 30,29, 30,29, 30,29, 30,29, 29,30, 29 ),
- Array (,), // August 15
- Array (, 29, 29 ),
- Array (, 30, 30, 30 ),
- Array (,), // August 15
- Array (, 30 ),
- Array (29,30, 29,30, 29,29, 30,58, 30,29, 30,30, 30,29), // August 15, July 29, July 29
- Array (47,30, 29,30, 29,29, 30,29, 29,30, 29,30, 30 ),
- Array (36,30, 29,30, 29,30, 29,30, 29,30, 29,29, 30,29, 30 ),
- Array (,), // August 15
- Array (44,29, 30,30, 29,30, 30,29, 30,29, 29,30, 29 ),
- Array (, 29, 29 ),
- Array (,), // August 15
- );
- // Whether it is a leap year
- Private function IsLeapYear ($ AYear ){
- Return ($ AYear % 4 = 0) & ($ AYear % 100! = 0) | ($ AYear % 400 = 0 ));
- }
- // The number of days in a calendar 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 calendar name
- Private function LYearName ($ year)
- {
- $ Name = array ("zero", "one", "two", "three", "four", "five", "six", "seven ", "8", "9 ");
- For ($ I = 0; $ I <4; $ I ++)
- For ($ k = 0; $ k <10; $ k ++)
- If ($ year [$ I] = $ k)
- $ Tmp. = $ Name [$ k];
- Return $ tmp;
- }
- Private function LMonName ($ month)
- {
- If ($ month> = 1 & $ month <= 12)
- {
- $ Name = array (1 => "zheng", "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 =>
- "First Day", "second day", "Third Day", "fourth day", "Fifth Day", "Sixth Day", "Seventh Day", "eighth day", "Ninth Day", "Tenth Day ",
- "11", "12", "13", "14", "15", "16", "17", "18", "19", "20 ",
- "Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5", "Jun 6", "Jun 7", "Jun 8 ", "9th", "30th"
- );
- Return $ Name [$ day];
- }
- Return $ day;
- }
- // Convert the Gregorian calendar to the lunar calendar (Sdate: the Gregorian calendar 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 the month of the current year.
- $ Date1 = strtotime ($ year. "-01-01"); // January 1 of the current year
- $ 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. = "(Updated )";
- // Return $ Ldate;
- }
- // Convert the lunar calendar to the Gregorian calendar (date: the lunar calendar 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; // The statement must be queried, but no leap month is queried.
- // If the queried lunar calendar is a leap month and the year's lunar calendar array contains the leap month data
- If ($ Larray [$ month]> 30 & $ type = 1 & count ($ Larray)> = 13) $ day = $ Larray [13] + $ day;
- // Obtain the number of days from the lunar date of the current year to the calendar month of the current year.
- $ Days = $ day;
- For ($ I = 0; $ I <= $ month-1; $ I ++)
- $ Days + = $ Larray [$ I];
- // When the query date is more than one year from the Gregorian calendar on January 1, January 1
- If ($ days> 366 | ($ this-> GetSMon ($ month, 2 )! = 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;
- }
- }
- ?>
- Require_once 'lunar. php ';
- $ Today = date ("Y-m-d ");
- $ Lunar = new Lunar ();
- // Convert the Gregorian calendar to the lunar calendar
- $ Nl = date ("Y-m-d", $ lunar-> S2L ($ today ));
- // Convert the lunar calendar to the Gregorian calendar
- $ Gl = date ("Y-m-d", $ lunar-> L2S ($ nl ));
- Echo "today's Gregorian calendar is: $ today
";
- Echo "converted to lunar calendar: $ nl
";
- Echo "returns to the Gregorian calendar: $ gl
";
- ?>
|