Heim > Backend-Entwicklung > PHP-Tutorial > PHP农历公历转换

PHP农历公历转换

WBOY
Freigeben: 2016-07-25 09:09:45
Original
2475 Leute haben es durchsucht
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 for($k=0;$k if($year[$i]==$k)
  131. $tmp.=$Name[$k];
  132. return $tmp;
  133. }
  134. private function LMonName($month)
  135. {
  136. if($month >=1 && $month {
  137. $Name = array( 1=>"正","二","三","四","五","六","七","八","九","十","十一","十二");
  138. return $Name[$month];
  139. }
  140. return $month;
  141. }
  142. private function LDayName($day)
  143. {
  144. if($day >=1 && $day {
  145. $Name = array( 1 =>
  146. "初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
  147. "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
  148. "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
  149. );
  150. return $Name[$day];
  151. }
  152. return $day;
  153. }
  154. //公历转农历(Sdate:公历日期)
  155. public function S2L($date)
  156. {
  157. list($year, $month, $day) = explode("-", $date);
  158. if($year = 2051 ) return false;
  159. //获取查询日期到当年1月1日的天数
  160. $date1 = strtotime($year."-01-01");//当年1月1日
  161. $date2 = strtotime($year."-".$month."-".$day);
  162. $days=round(($date2-$date1)/3600/24);
  163. $days += 1;
  164. //获取相应年度农历数据,化成数组Larray
  165. $Larray = $this->_LMDay[$year - $this->_LStart];
  166. if($days {
  167. $Lyear = $year - 1;
  168. $days = $Larray[0] - $days;
  169. $Larray = $this->_LMDay[$Lyear - $this->_LStart];
  170. if($days {
  171. $Lmonth = 12;
  172. $Lday = $Larray[12] - $days;
  173. }
  174. else
  175. {
  176. $Lmonth = 11;
  177. $days = $days - $Larray[12];
  178. $Lday = $Larray[11] - $days;
  179. }
  180. }
  181. else
  182. {
  183. $Lyear = $year;
  184. $days = $days - $Larray[0];
  185. for($i = 1;$i {
  186. if($days > $Larray[$i]) $days = $days - $Larray[$i];
  187. else
  188. {
  189. if ($days > 30){
  190. $days = $days - $Larray[13];
  191. $Ltype = 1;
  192. }
  193. $Lmonth = $i;
  194. $Lday = $days;
  195. break;
  196. }
  197. }
  198. }
  199. return mktime(0, 0, 0, $Lmonth, $Lday, $Lyear);
  200. //$Ldate = $Lyear."-".$Lmonth."-".$Lday;
  201. //$Ldate = $this->LYearName($Lyear)."年".$this->LMonName($Lmonth)."月".$this->LDayName($Lday);
  202. //if($Ltype) $Ldate.="(闰)";
  203. //return $Ldate;
  204. }
  205. //农历转公历(date:农历日期; type:是否闰月)
  206. public function L2S($date,$type = 0)
  207. {
  208. list($year, $month, $day) = split("-",$date);
  209. if($year = 2051 ) return false;
  210. $Larray = $this->_LMDay[$year - $this->_LStart];
  211. if($type == 1 && count($Larray) //如果查询的农历是闰月并该年度农历数组存在闰月数据就获取
  212. if($Larray[$month]>30 && $type == 1 && count($Larray) >=13) $day = $Larray[13] + $day;
  213. //获取该年农历日期到公历1月1日的天数
  214. $days = $day;
  215. for($i=0;$i $days += $Larray[$i];
  216. //当查询农历日期距离公历1月1日超过一年时
  217. if($days > 366 || ($this->GetSMon($month,2)!=29 && $days>365 ))
  218. {
  219. $Syear = $year +1;
  220. if($this->GetSMon($month,2)!=29)
  221. $days-=366;
  222. else
  223. $days-=365;
  224. if($days > $this->_SMDay[1])
  225. {
  226. $Smonth = 2;
  227. $Sday = $days - $this->_SMDay[1];
  228. }
  229. else
  230. {
  231. $Smonth = 1;
  232. $Sday = $days;
  233. }
  234. }
  235. else
  236. {
  237. $Syear =$year;
  238. for($i=1;$i {
  239. if($days > $this->GetSMon($Syear,$i))
  240. $days-=$this->GetSMon($Syear,$i);
  241. else
  242. {
  243. $Smonth = $i;
  244. $Sday = $days;
  245. break;
  246. }
  247. }
  248. }
  249. return mktime(0, 0, 0, $Smonth, $Sday, $Syear);
  250. //$Sdate = $Syear."-".$Smonth."-".$Sday;
  251. //return $Sdate;
  252. }
  253. }
  254. ?>
复制代码
  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. ?>
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage