PHP implementation code for lunar calendar to lunar calendar

Source: Internet
Author: User
  1. /**
  2. **php Lunar calendar to lunar calendar
  3. * by bbs.it-home.org
  4. */
  5. Class Lunar {
  6. var $MIN _year = 1891;
  7. var $MAX _year = 2100;
  8. var $lunarInfo = Array (
  9. Array (0,2,9,21936), Array (6,1,30,9656), Array (0,2,17,9584), Array (0,2,6,21168), Array (5,1,26,43344), Array ( 0,2,13,59728),
  10. Array (0,2,2,27296), Array (3,1,22,44368), Array (0,2,10,43856), Array (8,1,30,19304), Array (0,2,19,19168), Array ( 0,2,8,42352),
  11. Array (5,1,29,21096), Array (0,2,16,53856), Array (0,2,4,55632), Array (4,1,25,27304), Array (0,2,13,22176), Array ( 0,2,2,39632),
  12. Array (2,1,22,19176), Array (0,2,10,19168), Array (6,1,30,42200), Array (0,2,18,42192), Array (0,2,6,53840), Array ( 5,1,26,54568),
  13. Array (0,2,14,46400), Array (0,2,3,54944), Array (2,1,23,38608), Array (0,2,11,38320), Array (7,2,1,18872), Array ( 0,2,20,18800),
  14. Array (0,2,8,42160), Array (5,1,28,45656), Array (0,2,16,27216), Array (0,2,5,27968), Array (4,1,24,44456), Array ( 0,2,13,11104),
  15. Array (0,2,2,38256), Array (2,1,23,18808), Array (0,2,10,18800), Array (6,1,30,25776), Array (0,2,17,54432), Array ( 0,2,6,59984),
  16. Array (5,1,26,27976), Array (0,2,14,23248), Array (0,2,4,11104), Array (3,1,24,37744), Array (0,2,11,37600), Array ( 7,1,31,51560),
  17. Array (0,2,19,51536), Array (0,2,8,54432), Array (6,1,27,55888), Array (0,2,15,46416), Array (0,2,5,22176), Array ( 4,1,25,43736),
  18. Array (0,2,13,9680), Array (0,2,2,37584), Array (2,1,22,51544), Array (0,2,10,43344), Array (7,1,29,46248), Array ( 0,2,17,27808),
  19. Array (0,2,6,46416), Array (5,1,27,21928), Array (0,2,14,19872), Array (0,2,3,42416), Array (3,1,24,21176), Array ( 0,2,12,21168),
  20. Array (8,1,31,43344), Array (0,2,18,59728), Array (0,2,8,27296), Array (6,1,28,44368), Array (0,2,15,43856), Array ( 0,2,5,19296),
  21. Array (4,1,25,42352), Array (0,2,13,42352), Array (0,2,2,21088), Array (3,1,21,59696), Array (0,2,9,55632), Array ( 7,1,30,23208),
  22. Array (0,2,17,22176), Array (0,2,6,38608), Array (5,1,27,19176), Array (0,2,15,19152), Array (0,2,3,42192), Array ( 4,1,23,53864),
  23. Array (0,2,11,53840), Array (8,1,31,54568), Array (0,2,18,46400), Array (0,2,7,46752), Array (6,1,28,38608), Array ( 0,2,16,38320),
  24. Array (0,2,5,18864), Array (4,1,25,42168), Array (0,2,13,42160), Array (10,2,2,45656), Array (0,2,20,27216), Array ( 0,2,9,27968),
  25. Array (6,1,29,44448), Array (0,2,17,43872), Array (0,2,6,38256), Array (5,1,27,18808), Array (0,2,15,18800), Array ( 0,2,4,25776),
  26. Array (3,1,23,27216), Array (0,2,10,59984), Array (8,1,31,27432), Array (0,2,19,23232), Array (0,2,7,43872), Array ( 5,1,28,37736),
  27. Array (0,2,16,37600), Array (0,2,5,51552), Array (4,1,24,54440), Array (0,2,12,54432), Array (0,2,1,55888), Array ( 2,1,22,23208),
  28. Array (0,2,9,22176), Array (7,1,29,43736), Array (0,2,18,9680), Array (0,2,7,37584), Array (5,1,26,51544), Array ( 0,2,14,43344),
  29. Array (0,2,3,46240), Array (4,1,23,46416), Array (0,2,10,44368), Array (9,1,31,21928), Array (0,2,19,19360), Array ( 0,2,8,42416),
  30. Array (6,1,28,21176), Array (0,2,16,21168), Array (0,2,5,43312), Array (4,1,25,29864), Array (0,2,12,27296), Array ( 0,2,1,44368),
  31. Array (2,1,22,19880), Array (0,2,10,19296), Array (6,1,29,42352), Array (0,2,17,42208), Array (0,2,6,53856), Array ( 5,1,26,59696),
  32. Array (0,2,13,54576), Array (0,2,3,23200), Array (3,1,23,27472), Array (0,2,11,38608), Array (11,1,31,19176), Array ( 0,2,19,19152),
  33. Array (0,2,8,42192), Array (6,1,28,53848), Array (0,2,15,53840), Array (0,2,4,54560), Array (5,1,24,55968), Array ( 0,2,12,46496),
  34. Array (0,2,1,22224), Array (2,1,22,19160), Array (0,2,10,18864), Array (7,1,30,42168), Array (0,2,17,42160), Array ( 0,2,6,43600),
  35. Array (5,1,26,46376), Array (0,2,14,27936), Array (0,2,2,44448), Array (3,1,23,21936), Array (0,2,11,37744), Array ( 8,2,1,18808),
  36. Array (0,2,19,18800), Array (0,2,8,25776), Array (6,1,28,27216), Array (0,2,15,59984), Array (0,2,4,27424), Array ( 4,1,24,43872),
  37. Array (0,2,12,43744), Array (0,2,2,37600), Array (3,1,21,51568), Array (0,2,9,51552), Array (7,1,29,54440), Array ( 0,2,17,54432),
  38. Array (0,2,5,55888), Array (5,1,26,23208), Array (0,2,14,22176), Array (0,2,3,42704), Array (4,1,23,21224), Array ( 0,2,11,21200),
  39. Array (8,1,31,43352), Array (0,2,19,43344), Array (0,2,7,46240), Array (6,1,27,46416), Array (0,2,15,44368), Array ( 0,2,5,21920),
  40. Array (4,1,24,42448), Array (0,2,12,42416), Array (0,2,2,21168), Array (3,1,22,43320), Array (0,2,9,26928), Array ( 7,1,29,29336),
  41. Array (0,2,17,27296), Array (0,2,6,44368), Array (5,1,26,19880), Array (0,2,14,19296), Array (0,2,3,42352), Array ( 4,1,24,21104),
  42. Array (0,2,10,53856), Array (8,1,30,59696), Array (0,2,18,54560), Array (0,2,7,55968), Array (6,1,27,27472), Array ( 0,2,15,22224),
  43. Array (0,2,5,19168), Array (4,1,25,42216), Array (0,2,12,42192), Array (0,2,1,53584), Array (2,1,21,55592), Array ( 0,2,9,54560)
  44. );
  45. /**
  46. * Convert the Gregorian calendar to lunar calendar
  47. * @param year Gregorian calendar-years
  48. * @param month Gregorian-monthly
  49. * @param date Gregorian-day
  50. */
  51. function Convertsolartolunar ($year, $month, $date) {
  52. Debugger
  53. $yearData = $this->lunarinfo[$year-$this->min_year];
  54. if ($year = = $this->min_year&& $month <=2&& $date <=9) {
  55. Return Array (1891, ' The lunar month ', ' the ' Junior ', ' Xin Mao ', 1, 1, ' Rabbit ');
  56. }
  57. return $this->getlunarbybetween ($year, $this->getdaysbetweensolar ($year, $month, $date, $yearData [1],$ YEARDATA[2]));
  58. }
  59. function Convertsolarmonthtolunar ($year, $month) {
  60. $yearData = $this->lunarinfo[$year-$this->min_year];
  61. if ($year = = $this->min_year&& $month <=2&& $date <=9) {
  62. Return Array (1891, ' The lunar month ', ' the ' Junior ', ' Xin Mao ', 1, 1, ' Rabbit ');
  63. }
  64. $month _days_ary = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  65. $DD = $month _days_ary[$month];
  66. if ($this->isleapyear ($year) && $month = = 2) $dd + +;
  67. $lunar _ary = Array ();
  68. for ($i = 1; $i < $DD; $i + +) {
  69. $array = $this->getlunarbybetween ($year, $this->getdaysbetweensolar ($year, $month, $i, $yearData [1], $yearData [ 2]));
  70. $array [] = $year. '-' . $month. '-' . $i;
  71. $lunar _ary[$i] = $array;
  72. }
  73. return $lunar _ary;
  74. }
  75. /**
  76. * Convert lunar calendar to solar calendar
  77. * @param year lunar calendar-years
  78. * @param monthly lunar calendar-month, leap months processing: For example, if the current year leap May, then the second May will be June, equivalent to the lunar calendar has 13 months, but sometimes the 13th month of the days of 0
  79. * @param date Lunar calendar-day
  80. */
  81. function Convertlunartosolar ($year, $month, $date) {
  82. $yearData = $this->lunarinfo[$year-$this->min_year];
  83. $between = $this->getdaysbetweenlunar ($year, $month, $date);
  84. $res = Mktime (0,0,0, $yearData [1], $yearData [2], $year);
  85. $res = Date (' y-m-d ', $res + $between *24*60*60);
  86. $day = Explode ('-', $res);
  87. $year = $day [0];
  88. $month = $day [1];
  89. $day = $day [2];
  90. Return Array ($year, $month, $day);
  91. }
  92. /**
  93. * Determine if it is a leap year
  94. * @param year
  95. */
  96. function Isleapyear ($year) {
  97. Return (($year%4==0 && $year%100!=0) | | ($year%400==0));
  98. }
  99. /**
  100. * Get Ganzhi
  101. * @param year
  102. */
  103. function Getlunaryearname ($year) {
  104. $sky = Array (' g ', ' xin ', ' ren ', ' decyl ', ' A ', ' B ', ' C ', ' ding ', ' e ', ' own ');
  105. $earth = Array (' shen ', ' unitary ', ' Xu ', ' hai ', ' zi ', ' ugly ', ' Yin ', ' Mao ', ' Chen ', ' have ', ' noon ', ' not ');
  106. $year = $year. ";
  107. return $sky [$year {3}]. $earth [$year%12];
  108. }
  109. /**
  110. * Get Zodiac according to lunar year
  111. * @param year Overcast
  112. */
  113. function Getyearzodiac ($year) {
  114. $zodiac = Array (' monkey ', ' chicken ', ' dog ', ' pig ', ' rat ', ' cow ', ' tiger ', ' Rabbit ', ' dragon ', ' snake ', ' horse ', ' sheep ');
  115. return $zodiac [$year%12];
  116. }
  117. /**
  118. * Get the days of the Gregorian month
  119. * @param year Gregorian calendar-years
  120. * @param month Solar-moon
  121. */
  122. function Getsolarmonthdays ($year, $month) {
  123. $monthHash = Array (' 1 ' =>31, ' 2 ' = $this->isleapyear ($year)? 29:28, ' 3 ' =>31, ' 4 ' =>30, ' 5 ' =>31, ' 6 ' = >30, ' 7 ' =>31, ' 8 ' =>31, ' 9 ' =>30, ' ten ' =>31, ' one ' =>30, ' one ' =>31 ';
  124. return $monthHash ["$month"];
  125. }
  126. /**
  127. * Get the number of days of the lunar month
  128. * @param year lunar calendar-years
  129. * @param month Lunar calendar-months, starting from January
  130. */
  131. function Getlunarmonthdays ($year, $month) {
  132. $monthData = $this->getlunarmonths ($year);
  133. return $monthData [$month-1];
  134. }
  135. /**
  136. * Get an array of days of the lunar month
  137. * @param year
  138. */
  139. function Getlunarmonths ($year) {
  140. $yearData = $this->lunarinfo[$year-$this->min_year];
  141. $leapMonth = $yearData [0];
  142. $bit = Decbin ($yearData [3]);
  143. for ($i = 0; $i < strlen ($bit); $i + +) {
  144. $bitArray [$i] = substr ($bit, $i, 1);
  145. }
  146. For ($k =0, $klen =16-count ($bitArray), $k < $klen; $k + +) {
  147. Array_unshift ($bitArray, ' 0 ');
  148. }
  149. $bitArray = Array_slice ($bitArray, 0, ($leapMonth ==0?12:13));
  150. for ($i =0; $i
  151. $bitArray [$i] = $bitArray [$i] + 29;
  152. }
  153. return $bitArray;
  154. }
  155. /**
  156. * Get lunar calendar days per year
  157. * @param year of the lunar calendar
  158. */
  159. function Getlunaryeardays ($year) {
  160. $yearData = $this->lunarinfo[$year-$this->min_year];
  161. $monthArray = $this->getlunaryearmonths ($year);
  162. $len = count ($monthArray);
  163. Return ($monthArray [$len -1]==0? $monthArray [$len-2]: $monthArray [$len-1]);
  164. }
  165. function Getlunaryearmonths ($year) {
  166. Debugger
  167. $monthData = $this->getlunarmonths ($year);
  168. $res =array ();
  169. $temp = 0;
  170. $yearData = $this->lunarinfo[$year-$this->min_year];
  171. $len = ($yearData [0]==0?12:13);
  172. for ($i =0; $i < $len; $i + +) {
  173. $temp = 0;
  174. for ($j =0; $j <= $i; $j + +) {
  175. $temp + = $monthData [$j];
  176. }
  177. Array_push ($res, $temp);
  178. }
  179. return $res;
  180. }
  181. /**
  182. * Get Leap month
  183. * @param lunar Year
  184. */
  185. function Getleapmonth ($year) {
  186. $yearData = $this->lunarinfo[$year-$this->min_year];
  187. return $yearData [0];
  188. }
  189. /**
  190. * Calculate the number of days between lunar date and the day of the month
  191. * @param year
  192. * @param month
  193. * @param date
  194. */
  195. function Getdaysbetweenlunar ($year, $month, $date) {
  196. $yearMonth = $this->getlunarmonths ($year);
  197. $res = 0;
  198. for ($i =1; $i < $month; $i + +) {
  199. $res + = $yearMonth [$i-1];
  200. }
  201. $res + = $date-1;
  202. return $res;
  203. }
  204. /**
  205. * Calculates the number of days between 2 Gregorian dates
  206. * @param year
  207. * @param cmonth
  208. * @param cdate
  209. * @param the Gregorian month of Dmonth lunar month
  210. * @param the Gregorian calendar days of the Ddate lunar day
  211. */
  212. function Getdaysbetweensolar ($year, $cmonth, $cdate, $dmonth, $ddate) {
  213. $a = Mktime (0,0,0, $cmonth, $cdate, $year);
  214. $b = Mktime (0,0,0, $dmonth, $ddate, $year);
  215. Return Ceil (($a-$b)/24/3600);
  216. }
  217. /**
  218. * Based on the day of the month to calculate the lunar calendar date, such as today is the lunar calendar a few days of the week and so on
  219. * @param year
  220. * @param between Days
  221. */
  222. function Getlunarbybetween ($year, $between) {
  223. Debugger
  224. $lunarArray = Array ();
  225. $yearMonth =array ();
  226. $t = 0;
  227. $e = 0;
  228. $leapMonth = 0;
  229. $m = ";
  230. if ($between ==0) {
  231. Array_push ($lunarArray, $year, ' The lunar month ', ' the Junior ');
  232. $t = 1;
  233. $e = 1;
  234. }else{
  235. $year = $between >0? $year: ($year-1);
  236. $yearMonth = $this->getlunaryearmonths ($year);
  237. $leapMonth = $this->getleapmonth ($year);
  238. $between = $between >0? $between: ($this->getlunaryeardays ($year) + $between);
  239. for ($i =0; $i <13; $i + +) {
  240. if ($between = = $yearMonth [$i]) {
  241. $t = $i +2;
  242. $e = 1;
  243. Break
  244. }else if ($between < $yearMonth [$i]) {
  245. $t = $i +1;
  246. $e = $between-(Empty ($yearMonth [$i-1])? 0: $yearMonth [$i-1]) +1;
  247. Break
  248. }
  249. }
  250. $m = ($leapMonth!=0&& $t = = $leapMonth + 1)? (' Leap '. $this, Getcapitalnum ($t -1,true)): $this->getcapitalnum (($leapMonth!=0&& $leapMonth +1< $t? ( $t-1): $t), true);
  251. Array_push ($lunarArray, $year, $m, $this->getcapitalnum ($e, false));
  252. }
  253. Array_push ($lunarArray, $this->getlunaryearname ($year));//Zodiac Earthly Branches
  254. Array_push ($lunarArray, $t, $e);
  255. Array_push ($lunarArray, $this->getyearzodiac ($year));//12 Zodiac
  256. Array_push ($lunarArray, $leapMonth);//Leap months
  257. return $lunarArray;
  258. }
  259. /**
  260. * Get numbers on the lunar calendar
  261. * @param num Number
  262. * @param Ismonth is the number of months
  263. */
  264. function Getcapitalnum ($num, $isMonth) {
  265. $isMonth = $isMonth | | False
  266. $dateHash =array (' 0 ' = ', ' 1 ' = ' one ', ' 2 ' = ' two ', ' 3 ' = ' three ', ' 4 ' = ' Four ', ' 5 ' = ' five ', ' 6 ' = ' Six ', ' 7 ' = ' Seven ' , ' 8 ' = ' eight ', ' 9 ' = ' nine ', ' ten ' = ' x ';
  267. $monthHash =array (' 0 ' = ', ' 1 ' = ' The lunar month ', ' 2 ' = ' February ', ' 3 ' = ' March ', ' 4 ' =& gt; ' April ', ' 5 ' = ' May ', ' 6 ' = ' June ', ' 7 ' = ' July ', ' 8 ' = ' August ', ' 9 ' = ' September ', ' ' + ' ' + ' ' October ', ' one ' ' = ' ' Winter months ', ' );
  268. $res = ";
  269. if ($isMonth) {
  270. $res = $monthHash [$num];
  271. }else{
  272. if ($num <=10) {
  273. $res = ' At the beginning '. $dateHash [$num];
  274. }else if ($num >10&& $num <20) {
  275. $res = ' ten '. $dateHash [$num-10];
  276. }else if ($num ==20) {
  277. $res = "20";
  278. }else if ($num >20&& $num <30) {
  279. $res = "20". $dateHash [$num-20];
  280. }else if ($num ==30) {
  281. $res = "30";
  282. }
  283. }
  284. return $res;
  285. }
  286. }
  287. $lunar = new Lunar ();
  288. $month = $lunar->convertlunartosolar (2012, +);
  289. Print_r ($month);
  290. Exit
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.