Home > Backend Development > PHP Tutorial > PHP Lunar Calendar Gregorian Calendar Conversion

PHP Lunar Calendar Gregorian Calendar Conversion

WBOY
Release: 2016-07-25 09:09:45
Original
2464 people have browsed it
PHP Lunar Calendar Gregorian Calendar Conversion
  1. /*
  2. Written by Yang Haixi, School of Computer Science and Engineering, Qujing Normal University, Yunnan Province
  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);//Define the number of days in the Gregorian calendar month
  8. private $_LStart = 1950;//The lunar calendar starts in 1950
  9. private $_LMDay = array(
  10. //Difference: the number of days from the first day of the first lunar month of the year to January 1 of the Gregorian calendar; 1~12: the number of days in the lunar month; leap: if there is a leap month, record the number of ordinary days in the month
  11. //Difference 1 2 3 4 5 6 7 8 9 10 11 12 leap
  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), //May 29 Leap May 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), //March 29 Leap March 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), //August 30 Leap August 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), //June 30 Leap June 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), //April 29 Leap April 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), //March 30 Leap March 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) , //July 30 Leap July 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 ), //May 30 Leap May 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), //April 30 Leap April 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), //August 30 Leap August 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), //June 30 Leap June 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), //April 29 Leap April 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), //ten Month 30 Leap October 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), // June 30 Leap June 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), / /May 30 Leap May 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), //March 30 Leap March 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), //August 30 Leap August 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), //May 29 Leap May 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), //April 30 Leap April 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), //February 30 Leap February 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), //July 30 Leap July 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), //May 30 Leap May 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), //four Month 30 Leap April 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), //September 30 Leap September 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), //June 29 Leap June 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), //April 30 Leap April 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), //February 30 leap day Month 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), //June 30 Leap June 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), //May 30 Leap May 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), //March 30 Leap March 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), //November 30 Leap November 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), //June 29 Leap June 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), //May 30 Leap May 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), //February 30 Leap February 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), //July 29 Leap July 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), //May 29 Leap May 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), //March 29 Leap March 30
  113. );
  114. //Whether it is a leap year
  115. private function IsLeapYear($AYear){
  116. return ($AYear % 4 == 0) && (($AYear % 100 != 0) || ($AYear % 400 == 0));
  117. }
  118. //The number of days in the month in the Gregorian calendar (year: year; month: 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. //Lunar calendar name conversion
  127. private function LYearName($year)
  128. {
  129. $Name = array("zero","one","two","three","four","five","six"," seven","eight","nine");
  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=>"正","二","三","四","五","六","七","八"," nine","ten","eleven","twelve");
  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. "First day of junior high school", "Second day of junior high school", "Third day of junior high school", "Fourth day of junior high school", "Fifth day of junior high school" ","The sixth day of the lunar month","The seventh day of the lunar month","The eighth day of the lunar month","The ninth day of the lunar month","The tenth day of the lunar month",
  151. "Eleventh","Twelve","Thirteenth","Fourteenth","Tenth "Five", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty",
  152. "Twenty-one", "Twenty-two", "Twenty-three", "Twenty-four", " "Twenty-five", "Twenty-six", "Twenty-seven", "Twenty-eight", "Twenty-nine", "Thirty"
  153. );
  154. return $Name[$day];
  155. }
  156. return $day;
  157. }
  158. //Convert Gregorian calendar to lunar calendar (Sdate: Gregorian calendar date)
  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. //Get the number of days from the query date to January 1st of the current year
  164. $date1 = strtotime($year."-01-01");//January 1 of the current year
  165. $date2 = strtotime($year."-".$month."-".$day);
  166. $days=round (($date2-$date1)/3600/24);
  167. $days += 1;
  168. //Get the corresponding annual lunar calendar data and turn it into an array 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)."Year".$this->LMonName($Lmonth)."Month". $this->LDayName($Lday);
  209. //if($Ltype) $Ldate.="(Leap)";
  210. //return $Ldate;
  211. }
  212. //Convert lunar calendar to Gregorian calendar (date: lunar calendar date; type: whether it is a leap month)
  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;//Request to query leap month, but no leap month is found
  219. //If the lunar calendar being queried is a leap month and the lunar calendar array of the year contains a leap month Get the data
  220. if($Larray[$month]>30 && $type == 1 && count($Larray) >=13) $day = $Larray[13] + $day;
  221. //Get the year The number of days from the lunar date to January 1 of the Gregorian calendar
  222. $days = $day;
  223. for($i=0;$i<=$month-1;$i++)
  224. $days += $Larray[$i];
  225. //When querying the lunar date is more than one year from January 1 of the Gregorian calendar
  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. ?>
Copy code
  1. require_once 'Lunar.php';
  2. $today = date("Y-m-d");
  3. $lunar = new Lunar();
  4. //Gregorian calendar to lunar calendar
  5. $nl = date("Y-m-d ",$lunar->S2L($today));
  6. //Convert lunar calendar to Gregorian calendar
  7. $gl = date("Y-m-d",$lunar->L2S($nl));
  8. echo "Today's Gregorian calendar is:$ today
    ";
  9. echo "The conversion to the lunar calendar is: $nl
    ";
  10. echo "The conversion to the Gregorian calendar is: $gl
    ";
  11. ?>
Copy Code


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template