Broad and profound lunar calendar algorithm

WBOY
Release: 2016-07-25 09:07:59
Original
1060 people have browsed it
/** * Lunar calendar Extensive and profound lunar calendar *Original data and algorithm ideas come from S&S Lab http://www.focus-2000.com Unfortunately the website seems to be closed */ /* The number of days in each month of the lunar calendar. Each element is a year. The data in each element is: [0] is the month in which the leap month is, 0 means there is no leap month; [1] to [13] are the number of days in each month of 12 or 13 months in a year; [14] is the order of heavenly stems that year, [15] is the order of the Earthly Branches of that year */
  1. function lunarcalendar ($month, $year)
  2. {
  3. global $lnlunarcalendar;
  4. /**
  5. * Lunar calendar
  6. * The original data and algorithm ideas come from S&S
  7. Lab http://www.focus-2000.com Unfortunately the website seems to be closed
  8. */
  9. /*
  10. The number of days in each month of the lunar calendar.
  11. Each element is a year.The data in each element is:
  12. [0] is the month in which the leap month is, 0 means there is no leap month;
  13. [1] to [13] are the number of days per month in 12 or 13 months of each year;
  14. [14] is the current year The order of the heavenly stems,
  15. [15] is the order of the earthly branches of the year
  16. */
  17. $everymonth = array(
  18. 0 => array(8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29, 30, 7, 1),
  19. 1 => array(0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 8, 2) ,
  20. 2 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 0, 9, 3),
  21. 3 => array(5, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 10, 4),
  22. 4 => array(0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 1, 5),
  23. 5 => array(0, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 0, 2, 6),
  24. 6 => array(4, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 30, 3, 7) ,
  25. 7 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 4, 8),
  26. 8 => array(0, 30, 29, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 0, 5, 9),
  27. 9 => array(2, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 6, 10),
  28. 10 => array(0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 0, 7, 11),
  29. 11 => array(6, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 8, 12) ,
  30. 12 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 9, 1),
  31. 13 => array(0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 0, 10, 2),
  32. 14 => array(5, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 1, 3),
  33. 15 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 2, 4),
  34. 16 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 3, 5) ,
  35. 17 => array(2, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 4, 6),
  36. 18 => array(0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 0, 5, 7),
  37. 19 => array(7, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 6, 8),
  38. 20 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 0, 7, 9),
  39. 21 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 8, 10) ,
  40. 22 => array(5, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 9, 11),
  41. 23 => array(0, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 10, 12),
  42. 24 => array(0, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 0, 1, 1),
  43. 25 => array(4, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 2, 2),
  44. 26 => array(0, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 3, 3) ,
  45. 27 => array(0, 30, 29, 29, 30, 29, 30, 29, 30, 29, 30, 30, 30, 0, 4, 4),
  46. 28 => array(2, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 30, 5, 5),
  47. 29 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 6, 6),
  48. 30 => array(6, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 7, 7),
  49. 31 => array(0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 8, 8) ,
  50. 32 => array(0, 30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 9, 9),
  51. 33 => array(5, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 10, 10),
  52. 34 => array(0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 30, 0, 1, 11),
  53. 35 => array(0, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 2, 12),
  54. 36 => array(3, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 29, 3, 1) ,
  55. 37 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 4, 2),
  56. 38 => array(7, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 5, 3),
  57. 39 => array(0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 6, 4),
  58. 40 => array(0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 7, 5),
  59. 41 => array(6, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 8, 6) ,
  60. 42 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 9, 7),
  61. 43 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 10, 8),
  62. 44 => array(4, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 1, 9),
  63. 45 => array(0, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 2, 10),
  64. 46 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 3, 11) ,
  65. 47 => array(2, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 4, 12),
  66. 48 => array(0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 5, 1),
  67. 49 => array(7, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 6, 2),
  68. 50 => array(0, 29, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 0, 7, 3),
  69. 51 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 0, 8, 4) ,
  70. 52 => array(5, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 9, 5),
  71. 53 => array(0, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 29, 0, 10, 6),
  72. 54 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 0, 1, 7),
  73. 55 => array(3, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 2, 8),
  74. 56 => array(0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 0, 3, 9),
  75. 57 => array(8, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 29, 4, 10),
  76. 58 => array(0, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 5, 11),
  77. 59 => array(0, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 0, 6, 12),
  78. 60 => array(6, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 7, 1),
  79. 61 => array(0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 0, 8, 2),
  80. 62 => array(0, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 9, 3),
  81. 63 => array(4, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 10, 4),
  82. 64 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 0, 1, 5),
  83. 65 => array(0, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 0, 2, 6),
  84. 66 => array(3, 30, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 3, 7),
  85. 67 => array(0, 30, 30, 29, 30, 30, 29, 29, 30, 29, 30, 29, 30, 0, 4, 8),
  86. 68 => array(7, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 5, 9),
  87. 69 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 6, 10),
  88. 70 => array(0, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 0, 7, 11),
  89. 71 => array(5, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 29, 30, 8, 12),
  90. 72 => array(0, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 0, 9, 1),
  91. 73 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 0, 10, 2),
  92. 74 => array(4, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 1, 3),
  93. 75 => array(0, 30, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 0, 2, 4),
  94. 76 => array(8, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 3, 5),
  95. 77 => array(0, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 29, 0, 4, 6),
  96. 78 => array(0, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 5, 7),
  97. 79 => array(6, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 6, 8),
  98. 80 => array(0, 30, 29, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 0, 7, 9),
  99. 81 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 0, 8, 10),
  100. 82 => array(4, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 9, 11),
  101. 83 => array(0, 30, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 10, 12),
  102. 84 => array(10, 30, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 1, 1),
  103. 85 => array(0, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 2, 2),
  104. 86 => array(0, 29, 30, 30, 29, 30, 30, 29, 30, 29, 30, 29, 29, 0, 3, 3),
  105. 87 => array(6, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 29, 4, 4),
  106. 88 => array(0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 5, 5),
  107. 89 => array(0, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 30, 0, 6, 6),
  108. 90 => array(5, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 30, 7, 7),
  109. 91 => array(0, 29, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 0, 8, 8),
  110. 92 => array(0, 29, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 0, 9, 9),
  111. 93 => array(3, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 10, 10),
  112. 94 => array(0, 30, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 0, 1, 11),
  113. 95 => array(8, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 30, 29, 30, 2, 12),
  114. 96 => array(0, 29, 30, 29, 30, 30, 29, 30, 29, 30, 30, 29, 29, 0, 3, 1),
  115. 97 => array(0, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 0, 4, 2),
  116. 98 => array(5, 30, 29, 29, 30, 29, 29, 30, 30, 29, 30, 30, 29, 30, 5, 3),
  117. 99 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 0, 6, 4),
  118. 100 => array(0, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 0, 7, 5),
  119. 101 => array(4, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 8, 6),
  120. 102 => array(0, 30, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 9, 7),
  121. 103 => array(0, 30, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 0, 10, 8),
  122. 104 => array(2, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 1, 9),
  123. 105 => array(0, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 0, 2, 10),
  124. 106 => array(7, 30, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 30, 3, 11),
  125. 107 => array(0, 29, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 0, 4, 12),
  126. 108 => array(0, 30, 29, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 0, 5, 1),
  127. 109 => array(5, 30, 30, 29, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 6, 2),
  128. 110 => array(0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 0, 7, 3),
  129. 111 => array(0, 30, 29, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 0, 8, 4),
  130. 112 => array(4, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 29, 30, 29, 9, 5),
  131. 113 => array(0, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 29, 30, 0, 10, 6),
  132. 114 => array(9, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30, 1, 7),
  133. 115 => array(0, 29, 30, 29, 29, 30, 29, 30, 30, 30, 29, 30, 29, 0, 2, 8),
  134. 116 => array(0, 30, 29, 30, 29, 29, 30, 29, 30, 30, 29, 30, 30, 0, 3, 9),
  135. 117 => array(6, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 30, 4, 10),
  136. 118 => array(0, 29, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 30, 0, 5, 11),
  137. 119 => array(0, 30, 29, 30, 29, 30, 29, 29, 30, 29, 29, 30, 30, 0, 6, 12),
  138. 120 => array(4, 29, 30, 30, 30, 29, 30, 29, 29, 30, 29, 30, 29, 30, 7, 1)
  139. );
  140. $mten = $lnlunarcalendar['tiangan'];// Lunar Heavenly Stems
  141. $mtwelve = $lnlunarcalendar['dizhi'];// Lunar Earthly Branches
  142. $mmonth = $lnlunarcalendar ['month'];// Lunar month
  143. $mday = $lnlunarcalendar['day'];// Lunar day
  144. // Total number of days in the Gregorian calendar to December 21, 1900
  145. $total = 69 * 365 + 17 + 11 ;
  146. //Things before January 1, 1970 are not counted
  147. if ($year == "" || $month == "" || ($year < 1970 or $year > 2020)) return ' '; //No calculation beyond this range
  148. //Calculate the total number of days in the Gregorian calendar to the requested date - since December 21, 1900
  149. for ($y = 1970; $y < $year;$y++) { // Calculate the sum of the years first
  150. $total += 365;
  151. if ($y % 4 == 0) $total ++;
  152. }
  153. // Add the months of the current year
  154. $total += gmdate(" z", gmmktime(0, 0, 0, $month, 1, $year));
  155. // Use the accumulated number of days in the lunar calendar to determine whether it exceeds the number of days in the solar calendar
  156. $flag1 = 0; // Determine the conditions for jumping out of the loop
  157. $lcj = 0;
  158. while ($lcj <= 120) {
  159. $lci = 1;
  160. while ($lci <= 13) {
  161. $mtotal += $everymonth[$lcj][$lci];
  162. if ($mtotal >= $total) {
  163. $flag1 = 1;
  164. break;
  165. }
  166. $lci++;
  167. }
  168. if ($flag1 == 1) break;
  169. $lcj++;
  170. }
  171. // from above , the obtained $lci is the current lunar month, $lcj is the current lunar year
  172. // Calculate the lunar date of the 1st of the requested month
  173. $fisrtdaylunar = $everymonth[$lcj][$lci] - ($mtotal - $total) ;
  174. $results['year'] = $mten[$everymonth[$lcj][14]] . $mtwelve[$everymonth[$lcj][15]]; //What year is it currently?
  175. $daysthismonth = gmdate( "t", gmmktime(0, 0, 0, $month, 1, $year)); //The number of days in the current month
  176. $op = 1;
  177. for ($i = 1; $i <= $daysthismonth ; $i++) {
  178. $possiblelunarday = $fisrtdaylunar + $op-1; //Theoretically superimposed lunar days
  179. if ($possiblelunarday <= $everymonth[$lcj][$lci]) { // In this Within the range of days in the month
  180. $results[$i] = $mday[$possiblelunarday];
  181. $op += 1;
  182. }
  183. else { // Not within the range of days in this month
  184. $results[$i] = $ mday[1]; //Return to day 1
  185. $fisrtdaylunar = 1;
  186. $op = 2;
  187. $curmonthnum = ($everymonth[$lcj][0] != 0) ? 13 : 12; //There are in the current year Months
  188. if ($lci + 1 > $curmonthnum) { // It’s the 13th/14th month, go to the next year
  189. $lci = 1;
  190. $lcj = $lcj + 1;
  191. // Change It’s the end of the year, so write down the new year’s zodiac signs
  192. $results['year'] .= '/' . $mten[$everymonth[$lcj][14]] . $mtwelve[$everymonth[$lcj] [15]];
  193. }
  194. else { // Still in this year
  195. $lci = $lci + 1;
  196. $lcj = $lcj;
  197. }
  198. }
  199. if ($results[$i] == $mday [1]) { // The first day of each month should show what month that month is
  200. if ($everymonth[$lcj][0] != 0) { // Years with leap months
  201. $monthss = ($lci > $everymonth[$lcj][0]) ? ($lci-1) : $lci; //The number of months after the leap month-1
  202. if ($lci == $everymonth[$lcj][0] + 1) { // This month happens to be a leap month
  203. $monthssshow = $mmonth[0] . $mmonth[$monthss]; //Add a leap character in front
  204. $runyue = 1;
  205. }
  206. else {
  207. $monthssshow = $mmonth[$ monthss];
  208. }
  209. }
  210. else {
  211. $monthss = $lci;
  212. $monthssshow = $mmonth[$monthss];
  213. }
  214. if ($monthss <= 10 && $runyue != 1){ //Only 1-character month plus the word 'month'
  215. $monthssshow .= $mmonth[13];
  216. }
  217. $results[$i] = $monthssshow;
  218. }
  219. }
  220. return $results;
  221. }
  222. // Forgot to add this: Lunar Calendar characters
  223. $lnlunarcalendar = array(
  224. 'tiangan' => array("Unknown", "A", "B", "C", "D", "五" , "Ji", "Geng", "Xin", "ren", "縸"),
  225. 'dizhi' => array("Unknown", "Zi Nian (Rat)", "Chou Nian (Ox)" , "Yin Nian (Tiger)", "Mao Nian (Rabbit)", "Chen Nian (Dragon)",
  226. "Si Nian (Snake)", "Wu Nian (Horse)", "Wei Nian (Sheep)", "Shen Nian (Monkey)", "You Nian (Rooster)", "Xu Nian (Dog)", "Hai Nian (Pig)"),
  227. 'month' => array("闰", "正", "二", "three", "four", "five", "six",
  228. "seven", "eight", "nine", "ten", "eleven", "twelve", "month"),
  229. 'day' => array("Unknown", "First day of junior high school", "Second day of junior high school", "Third day of junior high school", "Fourth day of junior high school", "Fifth day of junior high school", "Sixth day of junior high school", "Seventh day of junior high school", " Eight", "ninth day", "tenth day",
  230. "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", " Eighteen", "nineteen", "twenty",
  231. "twenty-one", "twenty-two", "twenty-three", "twenty-four", "twenty-five", "twenty-six", "twenty-seven", "忿八", "忿九", "十三")
  232. );
Copy code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!