当前位置:Gxlcms > PHP教程 > PHP农历公历转换

PHP农历公历转换

时间:2021-07-01 10:21:17 帮助过:15人阅读

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

人气教程排行