PHP lunar calendar algorithm)

Source: Internet
Author: User

<? PHP

// Lunar calendar
$ Lnlunarcalendar = array (
'Tianganc' => array ("unknown", "Jia", "B", "bing", "ding", "Wu", "Ji", "Geng ", "Xin", "shen", "Xi "),
'Dizhi' => array ("unknown", "Child year (rat)", "ugly year (OX)", "Yin year (TIGER)", "Mao year (rabbit) "," Chen Nian (Dragon )",
"Year (snake)", "Year (Horse)", "Year (Goat)", "Year (monkey)", "Year (chicken )", "years (dogs)"," years (pigs )"),
'Month' => array ("Week", "Zheng", "two", "three", "four", "five", "Six ",
"7", "8", "9", "10", "11", "12", "month "),
'Day' => array ("unknown", "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 ")
);

$ Arr = lunarcalendar ("05", "2010 ");

Print_r ($ ARR );

Function lunarcalendar ($ month, $ year ){
Global $ lnlunarcalendar;
/* Lunar calendar
Original data and algorithm ideas from S & S lab <a href = "http://www.focus-2000.com" target = "_ blank"> http://www.focus-2000.com </a> unfortunately the website seems to be closed
*/
// The number of days of the lunar calendar month. Each element is a year. The data in each element is: [0] indicates the month in which the leap month is located, 0 indicates the non-leap month, and [1] to [13] indicates the number of days per month for 12 or 13 months each year; [14] is the daily order of the current year, and [15] is the geographic order of the current year
$ Everymonth = array (0 => array (,), 1 => array, 29,30, 30,30, 29,0,), 2 => array (, 29,30, 29,29, 30,29, 30,29, 30,30, 30,0, 9,3), 3 => array (, 30,29, 30,29, 30,29, 29,30, 29,29, 30,30, 29,30, 10,4), 4 => array (, 30,29, 30,29, 29,30, 29,29, 29,29, 30,30, 29,0 ), 5 => array (, 30, 29, 30, 30, 29, 29, 30, 29, 30, 30, 0,), 6 => array (, 30, 30, 29,30,), 7 => array (, 30,29, 30,29, 30,30, 29,30, 29,30, 29,30, 29,0,), 8 => array (, 29,29, 30,30, 29,30, 29,30, 29,30, 30,29, 30,0,), 9 => array (, 30,29, 29,30, 29,30, 29,30, 29,30, 30,30, 29,30, 6,10), 10 => array (, 30,29, 29, 29,30, 29,30, 29,30, 30,30, 29,0, 7,11), 11 => array (6,30, 29,30, 29,29, 30,29, 29,30, 30,29, 30,30, 8,12 ), 12 => array (,), 13 => array (, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 29, 29, 30, 0, 10, 2), 14 => array (5, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30,), 15 => array (, 30, 30, 29, 30, 30, 29, 29, 30,29, 30,29, 30,0,), 16 => array (, 30,29, 30,29, 30,30, 29,30, 29,30, 29,0, 3,5), 17 => array (, 29,29, 29, 29, 30,29, 30,30, 29,30, 30,29, 30,29,), 18 => array (, 29,29, 30,29, 30,29, 30,30, 29,30, 30,0, 5, 7 ), 19 => array (,), 20 => array (,), 20 =>, 30,0, 7,9), 21 => array (, 29,30, 29,29, 30,29, 29,30, 29,30, 30,0,), 22 => array (, 29,30, 30,29, 29,30, 29,30, 29,29, 30,29, 30,30, 9,11), 23 => array (, 30,30, 29,30, 29,30, 29,29, 30,29, 30,0, 10,12), 24 => array (, 30,30, 30, 29,30, 30,29, 30,29, 30,29, 29,0,), 25 => array (, 29,30, 29,30, 30,29, 30,30, 29,30, 29,30 ), 26 => array (,), 27 => array (, 30, 30, 30, 30, 30,0,), 28 => array (, 30,29, 29,30, 29,29, 30,29, 30,30, 30,30,), 29 => array (, 30,29, 29,30, 29,30, 29,29, 29, 29, 30,29, 30,30, 30,0, 6,6), 30 => array (6,29, 30,30, 29,29, 30,29, 29,30, 29,30, 30,29, 7,7), 31 => array (, 30,29, 29, 30,29, 30,29, 29,30, 29,30, 29,0,), 32 => array (, 30,30, 29,30, 29,30, 29,30, 29,29, 30,29, 30,0, 9,9 ), 33 => array (5, 29, 30, 30, 29, 30, 29, 30, 29, 29, 29, 30, 10, 10), 34 => array (0, 29, 30, 29, 30, 30, 29, 30, 29, 29, 30, 30, 30, 29, 30,0,), 35 => array (, 29,30, 29,30, 29,30, 30,29, 30,30, 29,0,), 36 => array (, 29,29, 30,29, 29,30, 29,30, 30,29, 30,30, 30,29, 3,1), 37 => array (, 29,29, 30,29, 29,30, 29,30, 30,30, 29,0,), 38 => array (, 30,29, 29,30, 29,29, 30,29, 30,30, 29,30,), 39 => array (, 30,29, 29,30, 29,29, 30,29, 30,29, 30,29, 30,0, 6, 4 ), 40 => array (,), 41 => array (, 30, 30, 30, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 30, 29, 29, 30,29,), 42 => array (, 29,30, 30,29, 30,29, 30,29, 30,29, 30,29, 30,0, 9,7), 43 => array (, 30,29, 30,29, 30,30, 30, 29,30, 29,30, 29,0,), 44 => array (, 29,30, 29,30, 29,30, 29,30, 29,30, 30,29, 30,30,), 45 => array (, 29,30, 29,29, 30,29, 30,30, 30,29, 30,0,), 46 => array (, 29,29, 30,29, 29,30, 29,30, 30,29, 30,0, 11 ), 47 => array (,), 48 => array (,), 48 =>, 30,0, 5, 1), 49 => array (, 29,30, 30,29, 30,29, 29,30, 29,30, 29,30,), 50 => array (, 30,30, 29,30, 30,29, 29, 29,30, 29,30, 29,0, 7,3), 51 => array (, 29,30, 30,29, 30,29, 30,29, 30,29, 30,0,), 52 => array (, 30,29, 29, 30,29, 30,29, 30,30, 29,30, 29,30, 9,5), 53 => array (, 30,29, 29,30, 30,29, 30,30, 29,30, 29,0, 10,6 ), 54 => array (,), 55 => array (,), 55 =>, 30,30,), 56 => array (, 30,29, 30,29, 29,30, 29,30, 29,30, 29,30, 30,0,), 57 => array (, 29,30, 29,30, 29,30, 29,29, 30,29, 30,29, 30,29,), 58 => array (, 30,30, 29,30, 29,29, 30,29, 30,29, 30,0, 5, 11), 59 => array (, 30,30, 30, 29,30, 29,30, 29,30, 29,30, 29,0, 6,12), 60 => array (6,30, 29,30, 29,30, 30,29, 30,29, 30,29, 30,29, 30,29, 7,1 ), 61 => array (,), 62 => array (, 30, 30, 30, 30, 30, 30, 30, 30, 30, 29,0, 9,3), 63 => array (, 29,30, 29,29, 30,29, 30,29, 30,29, 30,30, 30,29, 10,4), 64 => array (, 29,30, 29,29, 30,29, 30,29, 30,30, 30,0,), 65 => array (, 30,29, 30,29, 29,30, 29,29, 29,29, 30,30, 29,0,), 66 => array (, 30,30, 30, 29,30, 29,29, 30,29, 29,30, 30,29, 3,7), 67 => array (, 30,29, 30,30, 29,29, 30,29, 30,29, 30,29, 30,0 ), 68 => array (, 30, 30,), 69 => array, 29,0, 6,10), 70 => array (, 29,29, 30,29, 30,30, 29,30, 30,29, 30,0,), 71 => array (, 30,29, 29,30, 29,30, 29,30, 29,30, 30,30, 29,30, 8,12), 72 => array (, 30,29, 29,30, 29,30, 29,30, 30,29, 30,0, 9,1), 73 => array (, 29,30, 29,29, 30,29, 29,30, 30,29, 30,0, 10,2), 74 => array (, 30,29, 30,29, 29,30, 29,29, 30,30, 29,30 ), 75 => array (,), 76 => array, 29,30,), 77 => array (, 29,30, 30,29, 30,29, 30,29, 30,29, 30,29, 29,0,), 78 => array (, 29,30, 30,29, 30,30, 30, 29,30, 29,30, 29,0, 5, 7), 79 => array (6, 30, 29,29, 30,29, 30,30, 29,30, 30,29, 30,29, 6,8), 80 => array (0, 30, 29,29, 29, 30,29, 30,29, 30,30, 29,30, 30,0,), 81 => array (, 30,29, 29,30, 29,29, 30,30, 29,30, 29,30, 30,0 ), 82 => array (,), 83 => array (,), 83 =>, 30,0, 10,12), 84 => array (10, 30, 29,30, 30,29, 29,30, 29,29, 30,30,), 85 => array (, 30,30, 29,30, 29,30, 29,30, 30, 29,29, 30,29, 30,0,), 86 => array (, 30,30, 29,30, 30,29, 30,29, 30,29, 29,0, 3,3), 87 => array (6, 30, 29,30, 30, 29,30, 30,29, 30,30, 29,30, 29,29,), 88 => array (, 29,30, 29,30, 29,30, 29,30, 30,29, 30,30, 29,0, 5 ), 89 => array (, 29,29, 30,29, 29,30, 30,29, 30,30, 30,0, 6,6), 90 => array (, 29, 30,29, 29,30, 29,29, 30,30, 30,30, 30,30, 7,7), 91 => array (, 30,29, 29,30, 29,29, 30,29, 30,30, 30,0,), 92 => array (, 30,30, 29,29, 30,29, 30,29, 29, 29,30, 29,30, 30,0, 9,9), 93 => array (, 30,30, 29,30, 29,30, 29,29, 30,29, 30,29, 10,10), 94 => array (, 30,30, 30,30, 29,30, 29,30, 29,29, 30,29, 30,0,), 95 => array (8, 29, 30,30, 29,30, 29,30, 30,29, 29,30, 29,30 ), 96 => array (, 30, 30,), 97 => array (, 30, 30, 30, 30, 30, 29,0,), 98 => array (, 29,29, 30,29, 29,30, 30,29, 30,29, 29,30,), 99 => array (, 29,29, 30,29, 29,30, 29,30, 29,30, 30,30, 29,0, 6,4), 100 => array (101, 30,29, 29,30, 29,29, 30,29, 30,30, 29,0, 7,5), => array (, 30,29, 30,29, 30,29, 29,30, 29,30, 29,30, 102), => array (, 30,29, 30,29, 30,29, 29,30, 29,30, 29,30, 29,0, 9,7 ), 103 => array (104,), => array (, 30, 29,30, 105), 106 => array (, 30,29, 30,29, 30,30, 29,30, 29,30, 29,30, 29,0,), => array (, 29,30, 29,30, 29,30, 29,30, 29,30, 30,29, 30,30, 107), 108 => array (, 29,30, 29,29, 30,29, 30,30, 30,29, 30,0,), => array (, 29,29, 29, 30,29, 29,30, 29,30, 30,29, 30,0, 109), => array (, 30, 30,29, 29,30, 29,29, 30,29, 30,29, 30,30, 6, 2 ), 110 => array (111,), => array, 29,0, 112), 113 => array (, 29,30, 30,29, 30,29, 30,29, 30,29, 30,29, 9,5), => array (, 29,30, 29,30, 29,30, 30,29, 30,29, 30,29, 30,0, 10,6), 114 => array (9,29, 30,29, 30,29, 30,29, 30,30, 29,30, 29,30, 115,), => array (, 30,29, 29, 29,30, 29,30, 30,30, 29,30, 29,0, 116), => array (, 29,30, 29,29, 30,29, 30,30, 29,30, 30,0 ), 117 => array (118,), => array, 30,0, 119), 120 => array (, 29,30, 29,30, 29,29, 30,29, 29,30, 30,0,), => array (, 30,30, 30,29, 30,29, 30,29, 30,29, 29,30, 29,30, 29,30, 7,1 ));
// Calendar month
$ Mten = $ lnlunarcalendar ['tianganc'];
// Lunar calendar
$ Mtwelve = $ lnlunarcalendar ['dizhi'];
// Lunar month
$ Mmonth = $ lnlunarcalendar ['month'];
// Lunar day
$ Mday = $ lnlunarcalendar ['day'];
// The total number of days of the Gregorian calendar to January 1, December 21, 1900
$ Total = 69*365 + 17 + 11; // This is not counted before January 1, January 1, 1970.
If ($ year = "" | $ month = "" | ($ year <1970 or $ year> 2020) return ''; // beyond this range, no calculation is performed.
// Calculate the total number of days of the expected date solar calendar-starting from January 1, December 21, 1900
// Calculate the sum of years first
For ($ Y = 1970; $ Y <$ year; $ y ++ ){
$ Total + = 365;
If ($ Y % 4 = 0) $ total ++;
}
// Add a few months of the current year
$ Total + = gmdate ("Z", gmmktime (0, 0, 0, $ month, 1, $ year ));
// Use the number of calendar days to accumulate to determine whether the number of calendar days has exceeded the number of calendar days
$ Flag1 = 0; // determine the condition for jumping out of the loop
$ Lcj = 0;
While ($ lcj <= 120 ){
$ LCI = 1;
While ($ LCI <= 13 ){
$ Mtotal + = $ everymonth [$ lcj] [$ LCI];
If ($ Mtotal >=$ total ){
$ Flag1 = 1;
Break;
}
$ LCI ++;
}
If ($ flag1 = 1) break;
$ Lcj ++;
}
// From top to bottom, the obtained $ LCI is the current lunar month, and $ lcj is the current lunar year
// Calculate the calendar day of the month 1.
$ Fisrtdaylunar = $ everymonth [$ lcj] [$ LCI]-($ Mtotal-$ total );
$ Results ['Year'] = $ mten [$ everymonth [$ lcj] [14]. $ mtwelve [$ everymonth [$ lcj] [15]; // the current year
$ Daysthismonth = gmdate ("T", gmmktime (, 0, $ month, 1, $ year); // a total of days in the current month
$ Op = 1;
For ($ I = 1; $ I <= $ daysthismonth; $ I ++ ){
$ Possiblelunarday = $ fisrtdaylunar + $ op-1; // theoretically superimposed lunar day
If ($ possiblelunarday <= $ everymonth [$ lcj] [$ LCI]) {// within the number of days of this month
$ Results [$ I] = $ mday [$ possiblelunarday];
$ OP + = 1;
}
Else {// not within the number of days of the month
$ Results [$ I] = $ mday [1]; // return to 1
$ Fisrtdaylunar = 1;
$ Op = 2;
$ Curmonthnum = ($ everymonth [$ lcj] [0]! = 0 )? 13: 12; // several months in the current year
If ($ LCI + 1> $ curmonthnum) {// transfer to the next year
$ LCI = 1;
$ Lcj = $ lcj + 1;
// After another year, write the daily support for the new year
$ Results ['Year']. = '/'. $ mten [$ everymonth [$ lcj] [14]. $ mtwelve [$ everymonth [$ lcj] [15];
} Else {// still in this year
$ LCI = $ LCI + 1;
$ Lcj = $ lcj;
}
}
If ($ results [$ I] = $ mday [1]) {// the first day of each month shows the month of the current month.
If ($ everymonth [$ lcj] [0]! = 0) {// year with a leap month
$ Monthss = ($ LCI> $ everymonth [$ lcj] [0])? ($ Lci-1): $ LCI; // number of months after a leap month-1
If ($ LCI = $ everymonth [$ lcj] [0] + 1) {// this month is exactly a leap month
$ Monthssshow = $ mmonth [0]. $ mmonth [$ monthss]; // Add a comment before
$ Runyue = 1;
} Else {
$ Monthssshow = $ mmonth [$ monthss];
}
} Else {
$ Monthss = $ LCI;
$ Monthssshow = $ mmonth [$ monthss];
}
If ($ monthss <= 10 & $ runyue! = 1) $ monthssshow. = $ mmonth [13]; // Add the word 'month' to the month with only one word
$ Results [$ I] = $ monthssshow;
}
}
Return $ results;
}

?>

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.