首頁 >後端開發 >php教程 >PHP農曆公曆轉換

PHP農曆公曆轉換

WBOY
WBOY原創
2016-07-25 09:09:452480瀏覽
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 閏(48,29,29,30,29,29,30,29,30,30,30,29,30),
  69. array(37,30,29,29,30,29,29,30,29 ,30,30,29,30),
  70. array(25,30,30,29,29,59,29,30,29,30,29,30,30,30), //五月30 閏五月29
  71. array(44,30,29,30,29,30,29,29,30,29,30,29,30),
  72. array(33,30,29,30,30, 29,30,29,29,30,29,30,29),
  73. array(22,30,29,30,59,30,29,30,29,30,29,30,29,30) , //四月30 閏四月29
  74. array(40,30,29,30,29,30,30,29,30,29,30,29,30),
  75. array(30,29 ,30,29,30,29,30,29,30,59,30,29,30,30), //九月30 閏九月29
  76. array(49,29,30,29,29,30,29,30,30,30,29,30,29),
  77. array(38,30,29,30,29,29,30,29, 30,30,29,30,30),
  78. array(27,29,30,29,30,29,59,29,30,29,30,30,30,29), //六月29閏六月30
  79. array(46,29,30,29,30,29,29,30,29,30,29,30,30),
  80. array(35,30,29,30,29 ,30,29,29,30,29,29,30,30),
  81. array(24,29,30,30,59,30,29,29,30,29,30,29,30,30 ), //四月30 閏四月29
  82. array(42,29,30,30,29,30,29,30,29,30,29,30,29),
  83. array(31, 30,29,30,29,30,30,29,30,29,30,29,30),
  84. array(21,29,59,29,30,30,29,30,30,29, 30,29,30,30), //二月30 閏二月29
  85. array(40,29,30,29,29,30,29,30,30,29,30,30,29),
  86. array(28,30,29,30,29,29,59,30,29,30,30,30,29,30), //六月30 閏六月29
  87. array(47, 30,29,30,29,29,30,29,29,30,30,30,29),
  88. array(36,30,30,29,30,29,29,30,29,29, 30,30,29),
  89. array(25,30,30,30,29,59,29,30,29,29,30,30,29,30), //五月30 閏五月29
  90. array(43,30,30,29,30,29,30,29,30,29,29,30,30),
  91. array(33,29,30,29,30,30,29 ,30,29,30,29,30,29),
  92. array(22,29,30,59,30,29,30,30,29,30,29,30,29,30), //三月30 閏三月29
  93. array(41,30,29,29,30,29,30,30,29,30,30,29,30),
  94. array(30,29,30, 29,29,30,29,30,29,30,30,59,30,30), //十一月30 閏十一月29
  95. array(49,29,30,29,29,30 ,29,30,29,30,30,29,30),
  96. array(38,30,29,30,29,29,30,29,29,30,30,29,30),
  97. array(27,30,30,29,30,29,59,29,29,30,29,30,30,29), //六月29 閏六月30
  98. array(45,30, 30,29,30,29,29,30,29,29,30,29,30),
  99. array(34,30,30,29,30,29,30,29,30,29,29, 30,29),
  100. array(23,30,30,29,30,59,30,29,30,29,30,29,29,30), //五月30 閏五月29
  101. array(42,30,29,30,30,29,30,29,30,30,29,30,29),
  102. array(31,29,30,29,30,29,30,30 ,29,30,30,29,30),
  103. array(21,29,59,29,30,29,30,29,30,30,29,30,30,30), //二月30 閏二月29
  104. array(40,29,30,29,29,30,29,29,30,30,29,30,30),
  105. array(29,30,29,30, 29,29,30,58,30,29,30,30,30,29), //七月29 閏七月29
  106. array(47,30,29,30,29,29,30,29 ,29,30,29,30,30),
  107. array(36,30,29,30,29,30,29,30,29,29,30,29,30),
  108. array(25 ,30,29,30,30,59,29,30,29,29,30,29,30,29), //五月29 閏五月30
  109. array(44,29,30,30, 29,30,30,29,30,29,29,30,29),
  110. array(32,30,29,30,29,30,30,29,30,30,29,30,29) ,
  111. array(22,29,30,59,29,30,29,30,30,29,30,30,29,29), //三月29 閏三月30
  112. );
  113. //是否閏年
  114. private function IsLeapYear($AYear){
  115. return ($AYear % 4 == 0) && (($AYear % 100 != 0) || ($AYear ==% 400 == 0));
  116. }
  117. //公曆該月的天數(year:年份; month:月份)
  118. private function GetSMon($year,$month)
  119. {
  120. if($ this->IsLeapYear($year) && $month == 2)
  121. return 29;
  122. else
  123. return $this->_SMDay[$month];
  124. }
  125. //農曆名稱轉換
  126. private function LYearName($year)
  127. {
  128. $Name = array("零","一","二","三","四","五","六" ,"七","八","九");
  129. for($i=0;$i for($k=0;$k if($year[$i]==$k)
  130. $tmp.=$Name[$k];
  131. return $tmp;
  132. }
  133. private function LMonName ($month)
  134. {
  135. if($month >=1 && $month {
  136. $Name = array( 1=>"正","二","三","四","五","六","七","八","九","十","十一","十二");
  137. return $Name[$month ];
  138. }
  139. return $month;
  140. }
  141. private function LDayName($day)
  142. {
  143. if($day >=1 && $day {
  144. $Name = array( 1 =>
  145. "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
  146. "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
  147. "廿一","廿二","廿三","廿四","廿五", "廿六","廿七","廿八","廿九","三十"
  148. );
  149. return $Name[$day];
  150. }
  151. return $day ;
  152. }
  153. //公曆轉農曆(Sdate:公曆日期)
  154. public function S2L($date)
  155. {
  156. list($year, $month, $day) = explode("- ", $date);
  157. if($year = 2051 ) return false;
  158. //取得查詢日期到當年1月1日的天數
  159. $date1 = strtotime($year."-01-01");//當年1月1日
  160. $date2 = strtotime($year."-".$month ."-".$day);
  161. $days=round(($date2-$date1)/3600/24);
  162. $days += 1;
  163. //取得對應年度農曆數據,化成數組Larray
  164. $Larray = $this->_LMDay[$year - $this->_LStart];
  165. if($days {
  166. $Lyear = $year - 1;
  167. $days = $Larray[0] - $days;
  168. $Larray = $this->_LMDay[$Lyear - $this->_LStart];
  169. if($days {
  170. $Lmonth = 12;
  171. $Lday = $Larray[12] - $days;
  172. }
  173. else
  174. {
  175. $Lmonth = 11;
  176. $days = $days - $Larray[12];
  177. $Lday = $Larray[11] - $days;
  178. }
  179. }
  180. else
  181. {
  182. $Lyear = $year;
  183. $days = $days - $Larray[0];
  184. for($i = 1;$i {
  185. if ($days > $Larray[$i]) $days = $days - $Larray[$i];
  186. else
  187. {
  188. if ($days > 30){
  189. $days = $ days - $Larray[13];
  190. $Ltype = 1;
  191. }
  192. $Lmonth = $i;
  193. $Lday = $days;
  194. break;
  195. } }
  196. }
  197. }
  198. return mktime(0, 0, 0, $Lmonth, $Lday, $Lyear);
  199. //$Ldate = $Lyear."-".$Lmonth."- ".$Lday;
  200. //$Ldate = $this->LYearName($Lyear)."年".$this->LMonName($Lmonth)."月".$this->LDayName($Lday) ;
  201. //if($Ltype) $Ldate.="(閏)";
  202. //return $Ldate;
  203. }
  204. //農曆轉公曆(date:農曆日期;類型:是否閏月)
  205. public function L2S($date,$type = 0)
  206. {
  207. list($year, $month, $day) = split("-",$date);
  208. if($year = 2051 ) return false;
  209. $Larray = $this->_LMDay[$year - $this- >_LStart];
  210. if($type == 1 && count($Larray) //如果查詢的農曆是閏月並該年度農曆數組存在閏月資料就取得
  211. if($Larray[$month]>30 && $type == 1 && count($Larray) >=13) $day = $Larray[13] + $day;
  212. //取得該年農曆日期到公曆1月1日的天數
  213. $days = $day;
  214. for($i=0;$i $days += $Larray[$i];
  215. //當查詢農曆日期距離公曆1月1日超過一年時
  216. if($days > 366 || ($this->GetSMon($ month,2)!=29 && $days>365 ))
  217. {
  218. $Syear = $year +1;
  219. if($this->GetSMon($month,2)!=29)
  220. $days-=366;
  221. else
  222. $days-=365;
  223. if($days > $this->_SMDay[1])
  224. {
  225. $Smonth = 2;
  226. $Sday = $days - $this->_SMDay[1];
  227. }
  228. else
  229. {
  230. $Smonth = 1;
  231. $Sday = $days;
  232. }
  233. }
  234. else
  235. {
  236. $Syear =$year;
  237. for($i=1;$i {
  238. if( $days > $this->GetSMon($Syear,$i))
  239. $days-=$this->GetSMon($Syear,$i);
  240. else
  241. {
  242. $Smonth = $i;
  243. $Sday = $days;
  244. break;
  245. }
  246. }
  247. }
  248. return mktime(0, 0, 0, $Smonth, $Sday, $Syear) ;
  249. //$Sdate = $Syear."-".$Smonth."-".$Sday;
  250. //return $Sdate;
  251. }
  252. }
  253. ?>
複製程式碼
  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. ?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn