PHP 实现公历日期与农历日期的互转换

今天根据客户的需求对时间进行了转换,就是客户要求增加农历日期的显示,在网上抄袭了一段,稍微修改了一下运行成功了,不难的,改动的很少的.

示例代码:

  1. <?php
  2. /*
  3. *
  4. * 陈海波 新二级页面 农历转换成公历
  5. *
  6. * */
  7. // lunar.php 新建这个php文件,所有的代码无需改,需要改的是下面teacher.php 的几行
  8. class Lunar
  9. {
  10. private $_SMDay = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);//定义公历月分天数
  11. private $_LStart = 1950 ;//农历从1950年开始
  12. private $_LMDay = array(
  13. //差:该年的农历正月初一到该年公历1月1日的天数;1~12:农历月份天数;闰:如有闰月,记录该月平月天数
  14. // 差 1 2 3 4 5 6 7 8 9 10 11 12 闰
  15. array(47,29,30,30,29,30,30,29,29,30,29,30,29),
  16. array(36,30,29,30,30,29,30,29,30,29,30,29,30),
  17. array(6,29,30,29,30,59,29,30,30,29,30,29,30,29), //五月29 闰五月30
  18. array(44,29,30,29,29,30,30,29,30,30,29,30,29),
  19. array(33,30,29,30,29,29,30,29,30,30,29,30,30),
  20. array(23,29,30,59,29,29,30,29,30,29,30,30,30,29), //三月29 闰三月30
  21. array(42,29,30,29,30,29,29,30,29,30,29,30,30),
  22. array(30,30,29,30,29,30,29,29,59,30,29,30,29,30), //八月30 闰八月29
  23. array(48,30,30,30,29,30,29,29,30,29,30,29,30),
  24. array(38,29,30,30,29,30,29,30,29,30,29,30,29),
  25. array(27,30,29,30,29,30,59,30,29,30,29,30,29,30), //六月30 闰六月29
  26. array(45,30,29,30,29,30,29,30,30,29,30,29,30),
  27. array(35,29,30,29,29,30,29,30,30,29,30,30,29),
  28. array(24,30,29,30,58,30,29,30,29,30,30,30,29,29), //四月29 闰四月29
  29. array(43,30,29,30,29,29,30,29,30,29,30,30,30),
  30. array(32,29,30,29,30,29,29,30,29,29,30,30,29),
  31. array(20,30,30,59,30,29,29,30,29,29,30,30,29,30), //三月30 闰三月29
  32. array(39,30,30,29,30,30,29,29,30,29,30,29,30),
  33. array(29,29,30,29,30,30,29,59,30,29,30,29,30,30), //七月30 闰七月29
  34. array(47,29,30,29,30,29,30,30,29,30,29,30,29),
  35. array(36,30,29,29,30,29,30,30,29,30,30,29,30),
  36. array(26,29,30,29,29,59,30,29,30,30,30,29,30,30), //五月30 闰五月29
  37. array(45,29,30,29,29,30,29,30,29,30,30,29,30),
  38. array(33,30,29,30,29,29,30,29,29,30,30,29,30),
  39. array(22,30,30,29,59,29,30,29,29,30,30,29,30,30), //四月30 闰四月29
  40. array(41,30,30,29,30,29,29,30,29,29,30,29,30),
  41. array(30,30,30,29,30,29,30,29,59,29,30,29,30,30), //八月30 闰八月29
  42. array(48,30,29,30,30,29,30,29,30,29,30,29,29),
  43. array(37,30,29,30,30,29,30,30,29,30,29,30,29),
  44. array(27,30,29,29,30,29,60,29,30,30,29,30,29,30), //六月30 闰六月30
  45. array(46,30,29,29,30,29,30,29,30,30,29,30,30),
  46. array(35,29,30,29,29,30,29,29,30,30,29,30,30),
  47. array(24,30,29,30,58,30,29,29,30,29,30,30,30,29), //四月29 闰四月29
  48. array(43,30,29,30,29,29,30,29,29,30,29,30,30),
  49. array(32,30,29,30,30,29,29,30,29,29,59,30,30,30), //十月30 闰十月29
  50. array(50,29,30,30,29,30,29,30,29,29,30,29,30),
  51. array(39,29,30,30,29,30,30,29,30,29,30,29,29),
  52. array(28,30,29,30,29,30,59,30,30,29,30,29,29,30), //六月30 闰六月29
  53. array(47,30,29,30,29,30,29,30,30,29,30,30,29),
  54. array(36,30,29,29,30,29,30,29,30,29,30,30,30),
  55. array(26,29,30,29,29,59,29,30,29,30,30,30,30,30), //五月30 闰五月29
  56. array(45,29,30,29,29,30,29,29,30,29,30,30,30),
  57. array(34,29,30,30,29,29,30,29,29,30,29,30,30),
  58. array(22,29,30,59,30,29,30,29,29,30,29,30,29,30), //三月30 闰三月29
  59. array(40,30,30,30,29,30,29,30,29,29,30,29,30),
  60. array(30,29,30,30,29,30,29,30,59,29,30,29,30,30), //八月30 闰八月29
  61. array(49,29,30,29,30,30,29,30,29,30,30,29,29),
  62. array(37,30,29,30,29,30,29,30,30,29,30,30,29),
  63. array(27,30,29,29,30,58,30,30,29,30,30,29,30,29), //五月29 闰五月29
  64. array(46,30,29,29,30,29,29,30,29,30,30,30,29),
  65. array(35,30,30,29,29,30,29,29,30,29,30,30,29),
  66. array(23,30,30,29,59,30,29,29,30,29,30,29,30,30), //四月30 闰四月29
  67. array(42,30,30,29,30,29,30,29,29,30,29,30,29),
  68. array(31,30,30,29,30,30,29,30,29,29,30,29,30),
  69. array(21,29,59,30,30,29,30,29,30,29,30,29,30,30), //二月30 闰二月29
  70. array(39,29,30,29,30,29,30,30,29,30,29,30,29),
  71. array(28,30,29,30,29,30,29,59,30,30,29,30,30,30), //七月30 闰七月29
  72. array(48,29,29,30,29,29,30,29,30,30,30,29,30),
  73. array(37,30,29,29,30,29,29,30,29,30,30,29,30),
  74. array(25,30,30,29,29,59,29,30,29,30,29,30,30,30), //五月30 闰五月29
  75. array(44,30,29,30,29,30,29,29,30,29,30,29,30),
  76. array(33,30,29,30,30,29,30,29,29,30,29,30,29),
  77. array(22,30,29,30,59,30,29,30,29,30,29,30,29,30), //四月30 闰四月29
  78. array(40,30,29,30,29,30,30,29,30,29,30,29,30),
  79. array(30,29,30,29,30,29,30,29,30,59,30,29,30,30), //九月30 闰九月29
  80. array(49,29,30,29,29,30,29,30,30,30,29,30,29),
  81. array(38,30,29,30,29,29,30,29,30,30,29,30,30),
  82. array(27,29,30,29,30,29,59,29,30,29,30,30,30,29), //六月29 闰六月30
  83. array(46,29,30,29,30,29,29,30,29,30,29,30,30),
  84. array(35,30,29,30,29,30,29,29,30,29,29,30,30),
  85. array(24,29,30,30,59,30,29,29,30,29,30,29,30,30), //四月30 闰四月29
  86. array(42,29,30,30,29,30,29,30,29,30,29,30,29),
  87. array(31,30,29,30,29,30,30,29,30,29,30,29,30),
  88. array(21,29,59,29,30,30,29,30,30,29,30,29,30,30), //二月30 闰二月29
  89. array(40,29,30,29,29,30,29,30,30,29,30,30,29),
  90. array(28,30,29,30,29,29,59,30,29,30,30,30,29,30), //六月30 闰六月29
  91. array(47,30,29,30,29,29,30,29,29,30,30,30,29),
  92. array(36,30,30,29,30,29,29,30,29,29,30,30,29),
  93. array(25,30,30,30,29,59,29,30,29,29,30,30,29,30), //五月30 闰五月29
  94. array(43,30,30,29,30,29,30,29,30,29,29,30,30),
  95. array(33,29,30,29,30,30,29,30,29,30,29,30,29),
  96. array(22,29,30,59,30,29,30,30,29,30,29,30,29,30), //三月30 闰三月29
  97. array(41,30,29,29,30,29,30,30,29,30,30,29,30),
  98. array(30,29,30,29,29,30,29,30,29,30,30,59,30,30), //十一月30 闰十一月29
  99. array(49,29,30,29,29,30,29,30,29,30,30,29,30),
  100. array(38,30,29,30,29,29,30,29,29,30,30,29,30),
  101. array(27,30,30,29,30,29,59,29,29,30,29,30,30,29), //六月29 闰六月30
  102. array(45,30,30,29,30,29,29,30,29,29,30,29,30),
  103. array(34,30,30,29,30,29,30,29,30,29,29,30,29),
  104. array(23,30,30,29,30,59,30,29,30,29,30,29,29,30), //五月30 闰五月29
  105. array(42,30,29,30,30,29,30,29,30,30,29,30,29),
  106. array(31,29,30,29,30,29,30,30,29,30,30,29,30),
  107. array(21,29,59,29,30,29,30,29,30,30,29,30,30,30), //二月30 闰二月29
  108. array(40,29,30,29,29,30,29,29,30,30,29,30,30),
  109. array(29,30,29,30,29,29,30,58,30,29,30,30,30,29), //七月29 闰七月29
  110. array(47,30,29,30,29,29,30,29,29,30,29,30,30),
  111. array(36,30,29,30,29,30,29,30,29,29,30,29,30),
  112. array(25,30,29,30,30,59,29,30,29,29,30,29,30,29), //五月29 闰五月30
  113. array(44,29,30,30,29,30,30,29,30,29,29,30,29),
  114. array(32,30,29,30,29,30,30,29,30,30,29,30,29),
  115. array(22,29,30,59,29,30,29,30,30,29,30,30,29,29), //三月29 闰三月30
  116. );
  117. //是否闰年
  118. private function IsLeapYear($AYear)
  119. {
  120. return ($AYear % 4 == 0) && (($AYear % 100 != 0) || ($AYear % 400 == 0));
  121. }
  122. //公历该月的天数(year:年份; month:月份)
  123. private function GetSMon($year,$month)
  124. {
  125. if($this->IsLeapYear($year) && $month == 2)
  126. return 29;
  127. else
  128. return $this->_SMDay[$month];
  129. }
  130. //农历名称转换
  131. private function LYearName($year)
  132. {
  133. $Name = array("零","一","二","三","四","五","六","七","八","九");
  134. for($i=0;$i<4;$i++)
  135. for($k=0;$k<10;$k++)
  136. if($year[$i]==$k)
  137. $tmp.=$Name[$k];
  138. return $tmp;
  139. }
  140. private function LMonName($month)
  141. {
  142. if($month >=1 && $month <=12 )
  143. {
  144. $Name = array( 1=>"正","二","三","四","五","六","七","八","九","十","十一","十二");
  145. return $Name[$month];
  146. }
  147. return $month;
  148. }
  149. private function LDayName($day)
  150. {
  151. if($day >=1 && $day <=30 )
  152. {
  153. $Name = array( 1 =>
  154. "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
  155. "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
  156. "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
  157. );
  158. return $Name[$day];
  159. }
  160. return $day;
  161. }
  162. //公历转农历(Sdate:公历日期)
  163. public function S2L($date)
  164. {
  165. list($year, $month, $day) = explode("-", $date);
  166. if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
  167. //获取查询日期到当年1月1日的天数
  168. $date1 = strtotime($year."-01-01");//当年1月1日
  169. $date2 = strtotime($year."-".$month."-".$day);
  170. $days=round(($date2-$date1)/3600/24);
  171. $days += 1;
  172. //获取相应年度农历数据,化成数组Larray
  173. $Larray = $this->_LMDay[$year - $this->_LStart];
  174. if($days <= $Larray[0])
  175. {
  176. $Lyear = $year - 1;
  177. $days = $Larray[0] - $days;
  178. $Larray = $this->_LMDay[$Lyear - $this->_LStart];
  179. if($days < $Larray[12])
  180. {
  181. $Lmonth = 12;
  182. $Lday = $Larray[12] - $days;
  183. }
  184. else
  185. {
  186. $Lmonth = 11;
  187. $days = $days - $Larray[12];
  188. $Lday = $Larray[11] - $days;
  189. }
  190. }
  191. else
  192. {
  193. $Lyear = $year;
  194. $days = $days - $Larray[0];
  195. for($i = 1;$i <= 12;$i++)
  196. {
  197. if($days > $Larray[$i]) $days = $days - $Larray[$i];
  198. else
  199. {
  200. if ($days > 30){
  201. $days = $days - $Larray[13];
  202. $Ltype = 1;
  203. }
  204. $Lmonth = $i;
  205. $Lday = $days;
  206. break;
  207. }
  208. }
  209. }
  210. return mktime(0, 0, 0, $Lmonth, $Lday, $Lyear);
  211. //$Ldate = $Lyear."-".$Lmonth."-".$Lday;
  212. //$Ldate = $this->LYearName($Lyear)."年".$this->LMonName($Lmonth)."月".$this->LDayName($Lday);
  213. //if($Ltype) $Ldate.="(闰)";
  214. //return $Ldate;
  215. }
  216. //农历转公历(date:农历日期; type:是否闰月)
  217. public function L2S($date,$type = 0)
  218. {
  219. list($year, $month, $day) = split("-",$date);
  220. if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
  221. $Larray = $this->_LMDay[$year - $this->_LStart];
  222. if($type == 1 && count($Larray)<=12 ) return false;//要求查询闰,但查无闰月
  223. //如果查询的农历是闰月并该年度农历数组存在闰月数据就获取
  224. if($Larray[$month]>30 && $type == 1 && count($Larray) >=13) $day = $Larray[13] + $day;
  225. //获取该年农历日期到公历1月1日的天数
  226. $days = $day;
  227. for($i=0;$i<=$month-1;$i++)
  228. $days += $Larray[$i];
  229. //当查询农历日期距离公历1月1日超过一年时
  230. if($days > 366 || ($this->GetSMon($month,2)!=29 && $days>365 ))
  231. {
  232. $Syear = $year +1;
  233. if($this->GetSMon($month,2)!=29)
  234. $days-=366;
  235. else
  236. $days-=365;
  237. if($days > $this->_SMDay[1])
  238. {
  239. $Smonth = 2;
  240. $Sday = $days - $this->_SMDay[1];
  241. }
  242. else
  243. {
  244. $Smonth = 1;
  245. $Sday = $days;
  246. }
  247. }
  248. else
  249. {
  250. $Syear =$year;
  251. for($i=1;$i<=12;$i++)
  252. {
  253. if($days > $this->GetSMon($Syear,$i))
  254. $days-=$this->GetSMon($Syear,$i);
  255. else
  256. {
  257. $Smonth = $i;
  258. $Sday = $days;
  259. break;
  260. } //phpfensi.com
  261. }
  262. }
  263. return mktime(0, 0, 0, $Smonth, $Sday, $Syear);
  264. //$Sdate = $Syear."-".$Smonth."-".$Sday;
  265. //return $Sdate;
  266. }
  267. }
  268. ?>

teacher.php

  1. //公历转农历
  2. require_once(CORE_."Lunar.php"); // 加载lunar.php文件
  3. $today = date("Y-m-d");
  4. $lunar = new Lunar(); // 实例化类
  5. $nonglitime = array();
  6. $nl = date("Y-n-d",$lunar->S2L($today));
  7. echo $nl;exit; // 转为农历是:2012-05-25