Lunar calendar algorithm example implemented by php

Source: Internet
Author: User
This article mainly introduces the lunar calendar algorithm implemented by php, and analyzes in detail the principles of the lunar calendar algorithm and php implementation skills, which is of great practical value, for more information about the lunar calendar algorithm implemented by php, see the examples in this article. Share it with you for your reference. The details are as follows:

<? Phpfunction lunarcalendar ($ month, $ year) {global $ lnlunarcalendar; /*** Lunar calendar extensive and profound Lunar calendar * original data and algorithm ideas come from S & S * // * 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] indicates the day-to-day sequence of the current year. [15] indicates the day-to-day sequence of the current year */$ everymonth = array (0 => array (8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 30, 7, 1), 1 => array (0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 8, 2), 2 => array (0, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 0, 9, 3), 3 => array (5, 29, 30, 29, 30, 29, 29, 29, 30, 29, 29, 30, 30, 29, 30, 10, 4), 4 => array (0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 1, 5), 5 => array (0, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 0, 2, 6), 6 => array (4, 29, 30, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 3, 7), 7 => array (0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 4, 8), 8 => array (0, 30, 29, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 0, 5, 9), 9 => array (2, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 3 0, 29, 30, 6, 10), 10 => array (0, 29, 30, 29, 29, 30, 29, 30, 30, 29, 0, 7, 11), 11 => array (6, 30, 29, 30, 29, 29, 30, 29, 29, 29, 30, 30, 30, 8, 12), 12 => array (0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 9, 1), 13 => array (0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 0, 10, 2), 14 => array (5, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 1, 3), 15 => array (0, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 0, 2, 4 ), 16 => array (0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 29, 0, 3, 5 ), 17 => array (2, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 30, 29, 29, 4, 6 ), 18 => array (0, 30, 29, 29, 30, 29, 30, 29, 29, 30, 30, 0, 5, 7 ), 19 => array (7, 29, 30, 29, 29, 30, 29, 29, 29, 30, 29, 30, 30, 30, 6, 8 ), 20 => array (0, 29, 30, 29, 29, 30, 29, 29, 3 0, 30, 29, 30, 30, 0, 7, 9), 21 => array (0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 8, 10), 22 => array (5, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 30, 9, 11), 23 => array (0, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 0, 10, 12), 24 => array (0, 29, 30, 30, 29, 30, 30, 29, 30, 30, 29, 29, 0, 1, 1), 25 => array (4, 30, 29, 30, 29, 30, 30, 29, 30, 30, 30, 29, 30, 2, 2 ), 26 => array (0, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 3 ), 27 => array (0, 30, 29, 29, 30, 29, 30, 29, 29, 29, 30, 30, 30, 0, 4, 4 ), 28 => array (2, 29, 30, 29, 29, 30, 30, 29, 29, 30, 30, 30, 30, 5 ), 29 => array (0, 29, 30, 29, 29, 30, 30, 29, 29, 30, 30, 30, 0, 6, 6 ), 30 => array (6, 29, 30, 30, 29, 29, 30, 29, 29, 29, 30, 30, 29, 7 ), 31 => array (0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 8, 8), 32 => array (0, 30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 9, 9), 33 => array (5, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 10, 10), 34 => array (0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 0, 1, 11), 35 => array (0, 29, 29, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 0, 2, 12), 36 => array (3, 30, 29, 29, 30, 29, 29, 29, 30, 29, 30, 30, 30, 29, 3, 1), 37 => array (0, 30, 29, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 4, 2), 38 => array (7, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 5, 3), 39 => array (0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 6, 4), 40 => array (0, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 29, 0, 7, 5), 41 => array (6, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 30, 29, 8, 6), 42 => array (0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 9, 7), 43 => array (0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 10, 8), 44 => array (4, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 1, 9), 45 => array (0, 29, 29, 30, 29, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 2, 10), 46 => array (0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 0, 3, 11), 47 => array (2, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 4, 12), 48 => array (0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 5, 1), 49 => array (7, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 6, 2), 50 => array (0, 29, 30, 30, 29, 30, 30, 29, 29, 30, 29, 0, 7, 3), 51 => array (0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 30, 0, 8, 4), 52 => array (5, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 29, 30, 9, 5), 53 => array (0, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 29, 0, 10, 6 ), 54 => array (0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 30, 0, 1, 7 ), 55 => array (3, 29, 30, 29, 30, 29, 29, 30, 29, 30, 30, 30, 30, 2, 8 ), 56 => array (0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 3, 9 ), 57 => array (8, 30, 29, 30, 29, 30, 29, 29, 29, 30, 29, 30, 29, 29, 4, 10 ), 58 => array (0, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 5, 11), 59 => array (0, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 29, 0, 6, 12), 60 => array (6, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 7, 1), 61 => array (0, 30, 29, 30, 29, 30, 29, 29, 30, 30, 29, 30, 0, 8, 2), 62 => array (0, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 9, 3), 63 => array (4, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 10, 4 ), 64 => array (0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 30, 0, 1, 5 ), 65 => array (0, 29, 30, 29, 30, 29, 29, 30, 29, 29, 29, 30, 29, 0, 2, 6 ), 66 => array (3, 30, 30, 30, 29, 30, 29, 29, 29, 29, 29, 30, 30, 29, 3, 7 ), 67 => array (0, 30, 30, 29, 30, 30, 29, 29, 29, 29, 29, 30, 30, 0, 4, 8 ), 68 => array (7, 29, 30, 29, 30, 30, 29, 30, 29, 29, 29, 30, 29, 30, 5, 9 ), 69 => array (0, 29, 30, 29, 30, 2 9, 30, 30, 29, 30, 29, 30, 29, 0, 6, 10), 70 => array (0, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 0, 7, 11), 71 => array (5, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 8, 12), 72 => array (0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 9, 1), 73 => array (0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 10, 2), 74 => array (4, 30, 30, 29, 30, 29, 29, 29, 30, 29, 30, 30, 29, 30, 1, 3), 75 => array (0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 0, 2, 4), 76 => array (8, 30, 30, 29, 30, 29, 30, 29, 30, 30, 29, 29, 30, 3, 5), 77 => array (0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 29, 0, 4, 6), 78 => array (0, 30, 29, 30, 30, 29, 30, 30, 29, 29, 30, 29, 29, 0, 5, 7), 79 => array (6, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 6, 8), 80 => array (0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 0, 7, 9), 81 => array (0, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 0, 8, 10), 82 => array (4, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 9, 11), 83 => array (0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 10, 12), 84 => array (10, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 1, 1), 85 => array (0, 29, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 0, 2, 2), 86 => array (0, 29, 30, 30, 29, 30, 30, 29, 29, 30, 29, 29, 0, 3, 3), 87 => array (6, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 4, 4), 88 => array (0, 30, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 5, 5), 89 => array (0, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 6, 6), 90 => array (5, 29, 30, 29, 29, 30, 29, 29, 30, 29, 29, 30, 30, 7, 7), 91 => arr Ay (0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 29, 30, 30, 30, 0, 8, 8 ), 92 => array (0, 29, 30, 30, 29, 29, 30, 29, 29, 30, 30, 30, 0, 9, 9 ), 93 => array (3, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 10, 10 ), 94 => array (0, 30, 30, 30, 29, 30, 29, 29, 30, 30, 0, 1, 11 ), 95 => array (8, 29, 30, 30, 29, 30, 29, 30, 30, 30, 29, 29, 29, 30, 2, 12 ), 96 => array (0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 0, 3, 1), 97 => array (0, 30, 29, 30, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 0, 4, 2), 98 => array (5, 30, 29, 29, 30, 29, 29, 29, 30, 29, 30, 30, 29, 30, 5, 3), 99 => array (0, 30, 29, 29, 30, 29, 29, 29, 30, 30, 30, 29, 0, 6, 4), 100 => array (0, 30, 30, 29, 29, 30, 29, 29, 29, 30, 30, 29, 0, 7, 5), 101 => array (4, 30, 30, 29, 30, 29, 30, 29, 29, 29, 30, 29, 30, 8, 6), 102 => array (0, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 29, 0, 9, 7), 103 => array (0, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 30, 0, 10, 8), 104 => array (2, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 1, 9), 105 => array (0, 29, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 29, 0, 2, 10), 106 => array (7, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 3, 11), 107 => array (0, 29, 2 9, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 4, 12), 108 => array (0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 5, 1), 109 => array (5, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 6, 2), 110 => array (0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 7, 3), 111 => array (0, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 8, 4), 112 => array (4, 30, 29, 30, 30, 29, 30, 29, 30, 2 9, 30, 29, 30, 29, 9, 5), 113 => array (0, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 30, 0, 10, 6), 114 => array (9, 29, 30, 29, 30, 29, 30, 29, 29, 30, 30, 29, 30, 29, 30, 1, 7), 115 => array (0, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 29, 0, 2, 8), 116 => array (0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 3, 9), 117 => array (6, 29, 30, 29, 30, 29, 29, 30, 29, 30, 30, 4, 10 ), 118 => array (0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 5, 11 ), 119 => array (0, 30, 29, 30, 29, 30, 29, 29, 29, 29, 30, 30, 0, 6, 12 ), 120 => array (4, 29, 30, 30, 30, 29, 30, 29, 29, 29, 29, 30, 29, 30, 7, 1 )); $ mten = $ lnlunarcalendar ['tianganc']; // $ $ mtwelve = $ lnlunarcalendar ['dizhi']; // $ mmonth = $ lnlunarcalendar ['month']; // The month of the lunar calendar $ mday = $ lnlunarcalendar ['day']; // The day of the lunar calendar/ /The total number of days of the Gregorian calendar to December 21, 1900 $ total = 69*365 + 17 + 11; // if ($ year = "" | $ month = "" | ($ year <1970 or $ year> 2020 )) return ''; // do not calculate the total number of days from the date indicated by this range. // do not calculate the total number of days from the date indicated by the sun-for ($ y = 1970; $ y <$ year; $ y ++) {// calculate the sum of the year and $ total + = 365; if ($ y % 4 = 0) $ total ++ ;} // add the several months of the current year $ total + = gmdate ("z", gmmktime (0, 0, 0, $ month, 1, $ year )); // use the number of days accumulated in the lunar calendar to determine whether the number of days exceeds the number of days in the Gregorian calendar $ flag1 = 0; // determine the condition for jumping out of the cycle $ lc J = 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 ++;} // The upper-right corner shows the current lunar month, $ lcj is the current lunar calendar year // calculates the lunar date of the expected month 1 $ fisrtdaylunar = $ everymonth [$ lcj] [$ lci]-($ mtotal-$ total ); $ results ['Year'] = $ mten [$ everymonth [$ lcj] [14]. $ mtwelve [$ everymonth [$ lcj] [15]; // What is the current year $ dayst Hismonth = gmdate ("t", gmmktime (0, 0, 0, $ month, 1, $ year); // A total of days in the current month $ op = 1; for ($ I = 1; $ I <= $ daysthismonth; $ I ++) {$ possiblelunarday = $ fisrtdaylunar + $ OP-1; // if ($ possiblelunarday <= $ everymonth [$ lcj] [$ lci]) {// within the range of days of this month $ results [$ I] = $ mday [$ possiblelunarday]; $ op + = 1 ;} else {// not within the category of days of this month $ results [$ I] = $ mday [1]; // return to the 1st day $ fisrtdaylunar = 1; $ op = 2; $ curmonthnum = ($ eve Rymonth [$ lcj] [0]! = 0 )? 13: 12; // if ($ lci + 1> $ curmonthnum) for a few months in the current year {// The second month has been transferred to the next year $ lci = 1; $ lcj = $ lcj + 1; // after changing the year, write $ results ['Year'] for the daily support of the new year. = '/'. $ mten [$ everymonth [$ lcj] [14]. $ mtwelve [$ everymonth [$ lcj] [15];} else {// in this year, $ lci = $ lci + 1; $ lcj = $ lcj ;}} if ($ results [$ I] = $ mday [1]) {// The first day of each month shows the 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 the starting Character $ runyue = 1;} else {$ monthssshow = $ mmonth [$ monthss];} else {$ monthss = $ lci; $ monthssshow = $ mmonth [$ monthss];} if ($ monthss <= 10 & @ $ runyue! = 1) {// a month with only one word plus the word 'month' $ monthssshow. = $ mmonth [13];} $ results [$ I] = $ monthssshow;} return $ results;} // forget to add this: lunar calendar $ lnlunarcalendar = array ('tianganc' => array ("unknown", "a", "B", "C", "Ding", "E ", "Ji", "Geng", "Xin", "Zhi", "Xi"), 'dizhi' => array ("unknown", "child year (RAT )", "Ugly Year (ox)", "Yin Nian (Tiger)", "Mao Nian (rabbit)", "Chen Nian (Dragon)", "si Nian (snake )", "Wu Nian (MA)", "Wei Nian (goat)", "Shen Nian (monkey)", "Nian (chicken)"," Nian (dog )", "years (pig)"), 'month' => array ("Hangzhou", "Zheng", "two", "three", "four", "five ", "6", "7", "8", "9", "10", "11", "12", "month "), 'Day' => array ("unknown", "First day", "second day", "Third day", "fourth day", "Fifth day", "Sixth day", "Seventh day ", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th ", "18", "19", "20", "Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5", "Jun 6 ", "Chapter 7", "Chapter 8", "Article 9", "thirty"); print_r (lunarcalendar (8 ));

The running result is as follows:

Array ([year] => yyyy (goat) [1] => 17 [2] => 18 [3] => 19 [4] => 20 [5] => Jun 1 [6] => Jun 2 [7] => Example 3 [8] => Example 4 [9] => Example 5 [10] => Example 6 [11] => Example 7 [12] => Example 8 [13] => September 9 [14] => September 15 [15] => November 16 [16] => November 3 [17] => November 4 [18] => November 5 [19] => November 6 [20] => 7th [21] => 8th [22] => 9th [23] => 10th [24] => 11th [25] => 12th [26] => 13th [27] => 14th [28] => 15th [29] => 16th [30] => 17th [31] => 18th)

I hope this article will help you with php programming.

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.