PHP農曆西曆轉換

來源:互聯網
上載者:User
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. ?>
複製代碼
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.