PHP代码
  1. ###########################################  
  2. #作者: 沈潋(S&S Lab) #  
  3. #E-mail:shenlian@hotmail.com #  
  4. #web: 点击查看链接 #  
  5. # #  
  6. #版权声明: #  
  7. #作者开放版权 #  
  8. #允许无条件地使用本文件的全部或部分 #  
  9. #可作任何修改或增补 #  
  10. #如果您使用了本文件,请给作者写一封E-mail #  
  11. #如有问题欢迎同作者联系 #  
  12. #谢谢您的关注! #  
  13. ###########################################  
  14. /* 
  15. *修改:小许 
  16. *mail: san@xfocus.org 
  17. *web : 点击查看链接 
  18. * 
  19. *说实在的,我对古历可以说一窍不通,下面这个数组还是不理解 可是作者的信箱好像不通 
  20. *基本上是依样画葫芦,所以出现错误,见笑见笑,呵呵 
  21. */  
  22. #农历每月的天数  
  23.   
  24. $everymonth=array(  
  25.   
  26. 0=>array(8,0,0,0,0,0,0,0,0,0,0,0,29,30,7,1),  
  27. 1=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,8,2),  
  28. 2=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,9,3),  
  29. 3=>array(5,29,30,29,30,29,29,30,29,29,30,30,29,30,10,4),  
  30. 4=>array(0,30,30,29,30,29,29,30,29,29,30,30,29,0,1,5),  
  31. 5=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,2,6),  
  32. 6=>array(4,29,30,30,29,30,29,30,29,30,29,30,29,30,3,7),  
  33. 7=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,4,8),  
  34. 8=>array(0,30,29,29,30,30,29,30,29,30,30,29,30,0,5,9),  
  35. 9=>array(2,29,30,29,29,30,29,30,29,30,30,30,29,30,6,10),  
  36. 10=>array(0,29,30,29,29,30,29,30,29,30,30,30,29,0,7,11),  
  37. 11=>array(6,30,29,30,29,29,30,29,29,30,30,29,30,30,8,12),  
  38. 12=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,9,1),  
  39. 13=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,10,2),  
  40. 14=>array(5,30,30,29,30,29,30,29,30,29,30,29,29,30,1,3),  
  41. 15=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,2,4),  
  42. 16=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,3,5),  
  43. 17=>array(2,30,29,29,30,29,30,30,29,30,30,29,30,29,4,6),  
  44. 18=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,5,7),  
  45. 19=>array(7,29,30,29,29,30,29,29,30,30,29,30,30,30,6,8),  
  46. 20=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,7,9),  
  47. 21=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,8,10),  
  48. 22=>array(5,30,29,30,30,29,29,30,29,29,30,29,30,30,9,11),  
  49. 23=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,10,12),  
  50. 24=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,1,1),  
  51. 25=>array(4,30,29,30,29,30,30,29,30,30,29,30,29,30,2,2),  
  52. 26=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,3,3),  
  53. 27=>array(0,30,29,29,30,29,30,29,30,29,30,30,30,0,4,4),  
  54. 28=>array(2,29,30,29,29,30,29,29,30,29,30,30,30,30,5,5),  
  55. 29=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,6,6),  
  56. 30=>array(6,29,30,30,29,29,30,29,29,30,29,30,30,29,7,7),  
  57. 31=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,8,8),  
  58. 32=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,9,9),  
  59. 33=>array(5,29,30,30,29,30,30,29,30,29,30,29,29,30,10,10),  
  60. 34=>array(0,29,30,29,30,30,29,30,29,30,30,29,30,0,1,11),  
  61. 35=>array(0,29,29,30,29,30,29,30,30,29,30,30,29,0,2,12),  
  62. 36=>array(3,30,29,29,30,29,29,30,30,29,30,30,30,29,3,1),  
  63. 37=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,4,2),  
  64. 38=>array(7,30,30,29,29,30,29,29,30,29,30,30,29,30,5,3),  
  65. 39=>array(0,30,30,29,29,30,29,29,30,29,30,29,30,0,6,4),  
  66. 40=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,7,5),  
  67. 41=>array(6,30,30,29,30,30,29,30,29,29,30,29,30,29,8,6),  
  68. 42=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,9,7),  
  69. 43=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,10,8),  
  70. 44=>array(4,30,29,30,29,30,29,30,29,30,30,29,30,30,1,9),  
  71. 45=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,2,10),  
  72. 46=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,3,11),  
  73. 47=>array(2,30,30,29,29,30,29,29,30,29,30,29,30,30,4,12),  
  74. 48=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,5,1),  
  75. 49=>array(7,30,29,30,30,29,30,29,29,30,29,30,29,30,6,2),  
  76. 50=>array(0,29,30,30,29,30,30,29,29,30,29,30,29,0,7,3),  
  77. 51=>array(0,30,29,30,30,29,30,29,30,29,30,29,30,0,8,4),  
  78. 52=>array(5,29,30,29,30,29,30,29,30,30,29,30,29,30,9,5),  
  79. 53=>array(0,29,30,29,29,30,30,29,30,30,29,30,29,0,10,6),  
  80. 54=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,1,7),  
  81. 55=>array(3,29,30,29,30,29,29,30,29,30,29,30,30,30,2,8),  
  82. 56=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,3,9),  
  83. 57=>array(8,30,29,30,29,30,29,29,30,29,30,29,30,29,4,10),  
  84. 58=>array(0,30,30,30,29,30,29,29,30,29,30,29,30,0,5,11),  
  85. 59=>array(0,29,30,30,29,30,29,30,29,30,29,30,29,0,6,12),  
  86. 60=>array(6,30,29,30,29,30,30,29,30,29,30,29,30,29,7,1),  
  87. 61=>array(0,30,29,30,29,30,29,30,30,29,30,29,30,0,8,2),  
  88. 62=>array(0,29,30,29,29,30,29,30,30,29,30,30,29,0,9,3),  
  89. 63=>array(4,30,29,30,29,29,30,29,30,29,30,30,30,29,10,4),  
  90. 64=>array(0,30,29,30,29,29,30,29,30,29,30,30,30,0,1,5),  
  91. 65=>array(0,29,30,29,30,29,29,30,29,29,30,30,29,0,2,6),  
  92. 66=>array(3,30,30,30,29,30,29,29,30,29,29,30,30,29,3,7),  
  93. 67=>array(0,30,30,29,30,30,29,29,30,29,30,29,30,0,4,8),  
  94. 68=>array(7,29,30,29,30,30,29,30,29,30,29,30,29,30,5,9),  
  95. 69=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,6,10),  
  96. 70=>array(0,30,29,29,30,29,30,30,29,30,30,29,30,0,7,11),  
  97. 71=>array(5,29,30,29,29,30,29,30,29,30,30,30,29,30,8,12),  
  98. 72=>array(0,29,30,29,29,30,29,30,29,30,30,29,30,0,9,1),  
  99. 73=>array(0,30,29,30,29,29,30,29,29,30,30,29,30,0,10,2),  
  100. 74=>array(4,30,30,29,30,29,29,30,29,29,30,30,29,30,1,3),  
  101. 75=>array(0,30,30,29,30,29,29,30,29,29,30,29,30,0,2,4),  
  102. 76=>array(8,30,30,29,30,29,30,29,30,29,29,30,29,30,3,5),  
  103. 77=>array(0,30,29,30,30,29,30,29,30,29,30,29,29,0,4,6),  
  104. 78=>array(0,30,29,30,30,29,30,30,29,30,29,30,29,0,5,7),  
  105. 79=>array(6,30,29,29,30,29,30,30,29,30,30,29,30,29,6,8),  
  106. 80=>array(0,30,29,29,30,29,30,29,30,30,29,30,30,0,7,9),  
  107. 81=>array(0,29,30,29,29,30,29,29,30,30,29,30,30,0,8,10),  
  108. 82=>array(4,30,29,30,29,29,30,29,29,30,29,30,30,30,9,11),  
  109. 83=>array(0,30,29,30,29,29,30,29,29,30,29,30,30,0,10,12),  
  110. 84=>array(10,30,29,30,30,29,29,30,29,29,30,29,30,30,1,1),  
  111. 85=>array(0,29,30,30,29,30,29,30,29,29,30,29,30,0,2,2),  
  112. 86=>array(0,29,30,30,29,30,30,29,30,29,30,29,29,0,3,3),  
  113. 87=>array(6,30,29,30,29,30,30,29,30,30,29,30,29,29,4,4),  
  114. 88=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,5,5),  
  115. 89=>array(0,30,29,29,30,29,29,30,30,29,30,30,30,0,6,6),  
  116. 90=>array(5,29,30,29,29,30,29,29,30,29,30,30,30,30,7,7),  
  117. 91=>array(0,29,30,29,29,30,29,29,30,29,30,30,30,0,8,8),  
  118. 92=>array(0,29,30,30,29,29,30,29,29,30,29,30,30,0,9,9),  
  119. 93=>array(3,29,30,30,29,30,29,30,29,29,30,29,30,29,10,10),  
  120. 94=>array(0,30,30,30,29,30,29,30,29,29,30,29,30,0,1,11),  
  121. 95=>array(8,29,30,30,29,30,29,30,30,29,29,30,29,30,2,12),  
  122. 96=>array(0,29,30,29,30,30,29,30,29,30,30,29,29,0,3,1),  
  123. 97=>array(0,30,29,30,29,30,29,30,30,29,30,30,29,0,4,2),  
  124. 98=>array(5,30,29,29,30,29,29,30,30,29,30,30,29,30,5,3),  
  125. 99=>array(0,30,29,29,30,29,29,30,29,30,30,30,29,0,6,4),  
  126. 100=>array(0,30,30,29,29,30,29,29,30,29,30,30,29,0,7,5),  
  127. 101=>array(4,30,30,29,30,29,30,29,29,30,29,30,29,30,8,6),  
  128. 102=>array(0,30,30,29,30,29,30,29,29,30,29,30,29,0,9,7),  
  129. 103=>array(0,30,30,29,30,30,29,30,29,29,30,29,30,0,10,8),  
  130. 104=>array(2,29,30,29,30,30,29,30,29,30,29,30,29,30,1,9),  
  131. 105=>array(0,29,30,29,30,29,30,30,29,30,29,30,29,0,2,10),  
  132. 106=>array(7,30,29,30,29,30,29,30,29,30,30,29,30,30,3,11),  
  133. 107=>array(0,29,29,30,29,29,30,29,30,30,30,29,30,0,4,12),  
  134. 108=>array(0,30,29,29,30,29,29,30,29,30,30,29,30,0,5,1),  
  135. 109=>array(5,30,30,29,29,30,29,29,30,29,30,29,30,30,6,2),  
  136. 110=>array(0,30,29,30,29,30,29,29,30,29,30,29,30,0,7,3),  
  137. 111=>array(0,30,29,30,30,29,30,29,29,30,29,30,29,0,8,4),  
  138. 112=>array(4,30,29,30,30,29,30,29,30,29,30,29,30,29,9,5),  
  139. 113=>array(0,30,29,30,29,30,30,29,30,29,30,29,30,0,10,6),  
  140. 114=>array(9,29,30,29,30,29,30,29,30,30,29,30,29,30,1,7),  
  141. 115=>array(0,29,30,29,29,30,29,30,30,30,29,30,29,0,2,8),  
  142. 116=>array(0,30,29,30,29,29,30,29,30,30,29,30,30,0,3,9),  
  143. 117=>array(6,29,30,29,30,29,29,30,29,30,29,30,30,30,4,10),  
  144. 118=>array(0,29,30,29,30,29,29,30,29,30,29,30,30,0,5,11),  
  145. 119=>array(0,30,29,30,29,30,29,29,30,29,29,30,30,0,6,12),  
  146. 120=>array(4,29,30,30,30,29,30,29,29,30,29,30,29,30,7,1)  
  147.   
  148. );  
  149. ##############################  
  150.   
  151. #农历天干  
  152.   
  153. $mten=array("null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸");  
  154. #农历地支  
  155.   
  156. $mtwelve=array("null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)",  
  157. "巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)");  
  158. #农历月份  
  159.   
  160. $mmonth=array("闰","正","二","三","四","五","六",  
  161. "七","八","九","十","十一","十二","月");  
  162. #农历日  
  163.   
  164. $mday=array("null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",  
  165. "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",  
  166. "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十");  
  167. ##############################  
  168.   
  169. #星期  
  170.   
  171. $weekday = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");  
  172. #阳历总天数 至1900年12月21日  
  173.   
  174. $total=11;  
  175. #阴历总天数  
  176.   
  177. $mtotal=0;  
  178. ##############################  
  179.   
  180. #获得当日日期  
  181.   
  182. $today=getdate();  
  183. if($today["year"]<1901 || $today["year"]>2020) die("年份出错!");  
  184. $cur_wday=$today["wday"];  
  185. for($y=1901;$y<$today["year"];$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和  
  186.   
  187. $total+=365;  
  188. if ($y%4==0) $total++;  
  189. }  
  190.   
  191. switch($today["mon"]) { //再加当年的几个月  
  192.   
  193. case 12:  
  194.   
  195. $total+=30;  
  196. case 11:  
  197.   
  198. $total+=31;  
  199. case 10:  
  200.   
  201. $total+=30;  
  202. case 9:  
  203.   
  204. $total+=31;  
  205. case 8:  
  206.   
  207. $total+=31;  
  208. case 7:  
  209.   
  210. $total+=30;  
  211. case 6:  
  212.   
  213. $total+=31;  
  214. case 5:  
  215.   
  216. $total+=30;  
  217. case 4:  
  218.   
  219. $total+=31;  
  220. case 3:  
  221.   
  222. $total+=28;  
  223. case 2:  
  224.   
  225. $total+=31;  
  226. }  
  227.   
  228. if($today["year"]%4 == 0 && $today["mon"]>2) $total++; //如果当年是闰年还要加一天  
  229.   
  230. $total=$total+$today["mday"]-1; //加当月的天数  
  231.   
  232. $flag1=0; //判断跳出循环的条件  
  233.   
  234. $j=0;  
  235. while ($j<=120){ //用农历的天数累加来判断是否超过阳历的天数  
  236.   
  237. $i=1;  
  238. while ($i<=13){  
  239.   
  240. $mtotal+=$everymonth[$j][$i];  
  241. if ($mtotal>=$total){  
  242.   
  243. $flag1=1;  
  244. break;  
  245. }  
  246.   
  247. $i++;  
  248. }  
  249.   
  250. if ($flag1==1) break;  
  251. $j++;  
  252. }  
  253.   
  254. if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里,对闰月没有修补  
  255.   
  256. $mm=$i-1;  
  257. }  
  258.   
  259. else{  
  260.   
  261. $mm=$i;  
  262. }  
  263.   
  264. if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) {  
  265.   
  266. $nlmon=$mmonth[0].$mmonth[$mm];#闰月  
  267.   
  268. }  
  269.   
  270. else {  
  271.   
  272. $nlmon=$mmonth[$mm].$mmonth[13];  
  273. }  
  274.   
  275. #计算所求月份1号的农历日期  
  276.   
  277. $md=$everymonth[$j][$i]-($mtotal-$total);if($md > $everymonth[$j][$i])  
  278.   
  279. $md-=$everymonth[$j][$i];  
  280. $nlday=$mday[$md];  
  281. $nowday=date("Y年n月j日 ").$weekday[$cur_wday]."
    "
    .$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年".$nlmon.$nlday;  
  282. echo "$nowday";  
  283.   
  284. ?>   </li> </ol> </div>