PHP solar calendar to lunar calendar implementation code

Source: Internet
Author: User
PHP solar calendar to lunar calendar implementation code

PHP solar calendar to lunar calendar conversion class code

  1. // A class for converting the solar calendar to the lunar calendar in PHP
  2. Class Calendar
  3. {
  4. # Number of days of the lunar calendar month
  5. Var $ everyCMonth = array (
  6. 0 => array ),
  7. 1 => array ),
  8. 2 => array (, 3 ),
  9. 3 => array ),
  10. 4 => array ),
  11. 5 => array ),
  12. 6 => array ),
  13. 7 => array ),
  14. 8 => array ),
  15. 9 => array ),
  16. 10 => array ),
  17. 11 => array (6, 30, 29, 30, 29, 30, 29, 29, 29, 30, 29, 30, 30, 8, 12 ),
  18. 12 => array ),
  19. 13 => array ),
  20. 14 => array ),
  21. 15 => array ),
  22. 16 => array ),
  23. 17 => array (, 29,29, 30,29, 30,30, 29,30, 30,29, 30,29 ),
  24. 18 => array (, 29,29, 30,29, 30,29, 30,30, 29,30, 30,0, 5, 7 ),
  25. 19 => array ),
  26. 20 => array ),
  27. 21 => array ),
  28. 22 => array ),
  29. 23 => array ),
  30. 24 => array ),
  31. 25 => array ),
  32. 26 => array (, 3 ),
  33. 27 => array (, 29,29, 30,29, 30,29, 30,29, 30,30, 30,0 ),
  34. 28 => array ),
  35. 29 => array ),
  36. 30 => array (6, 29, 30, 30, 29, 30, 29, 29, 29, 30, 30, 29, 7 ),
  37. 31 => array ),
  38. 32 => array ),
  39. 33 => array (5, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 10, 10 ),
  40. 34 => array ),
  41. 35 => array ),
  42. 36 => array (3,30, 29,29, 30,29, 29,30, 30,29, 30,30, 30,29, 3,1 ),
  43. 37 => array ),
  44. 38 => array ),
  45. 39 => array ),
  46. 40 => array ),
  47. 41 => array (6, 30, 30, 29, 30, 30, 29, 30, 29, 29, 29, 29, 8, 6 ),
  48. 42 => array ),
  49. 43 => array ),
  50. 44 => array ),
  51. 45 => array ),
  52. 46 => array ),
  53. 47 => array ),
  54. 48 => array ),
  55. 49 => array ),
  56. 50 => array ),
  57. 51 => array ),
  58. 52 => array (5, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 9, 5 ),
  59. 53 => array ),
  60. 54 => array ),
  61. 55 => array ),
  62. 56 => array ),
  63. 57 => array ),
  64. 58 => array ),
  65. 59 => array ),
  66. 60 => array (6,30, 29,30, 29,30, 30,29, 30,29, 30,29, 30,29, 30,29, 7,1 ),
  67. 61 => array ),
  68. 62 => array ),
  69. 63 => array ),
  70. 64 => array ),
  71. 65 => array ),
  72. 66 => array ),
  73. 67 => array ),
  74. 68 => array ),
  75. 69 => array ),
  76. 70 => array (, 29,29, 30,29, 30,30, 29,30, 30,29, 30,0 ),
  77. 71 => array ),
  78. 72 => array ),
  79. 73 => array ),
  80. 74 => array ),
  81. 75 => array ),
  82. 76 => array (8, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 3, 5 ),
  83. 77 => array ),
  84. 78 => array ),
  85. 79 => array (6, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 6, 8 ),
  86. 80 => array ),
  87. 81 => array ),
  88. 82 => array ),
  89. 83 => array (, 12 ),
  90. 84 => array ),
  91. 85 => array ),
  92. 86 => array (, 3 ),
  93. 87 => array (6, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 29, 4, 4 ),
  94. 88 => array ),
  95. 89 => array (, 29,29, 30,29, 29,30, 30,29, 30,30, 30,0, 6 ),
  96. 90 => array ),
  97. 91 => array ),
  98. 92 => array (, 30, 30, 29, 30, 29, 29, 29, 30, 30, 30, 0, 9, 9 ),
  99. 93 => array (, 10, 10 ),
  100. 94 => array ),
  101. 95 => array ),
  102. 96 => array ),
  103. 97 => array ),
  104. 98 => array ),
  105. 99 => array ),
  106. 100 => array ),
  107. 101 => array ),
  108. 102 => array ),
  109. 103 => array ),
  110. 104 => array ),
  111. 105 => array ),
  112. 106 => array ),
  113. 107 => array ),
  114. 108 => array ),
  115. 109 => array ),
  116. 110 => array ),
  117. 111 => array ),
  118. 112 => array ),
  119. 113 => array ),
  120. 114 => array (9,29, 30,29, 30,29, 30,29, 30,30, 29,30, 29,30 ),
  121. 115 => array ),
  122. 116 => array ),
  123. 117 => array ),
  124. 118 => array ),
  125. 119 => array ),
  126. 120 => array)
  127. );
  128. ##############################
  129. # Lunar calendar
  130. Var $ mten = array ("null", "a", "B", "C", "Ding", "E", "Ji", "Geng ", "Xin", "Shen", "Xi"); # lunar calendar
  131. Var $ mtwelve = array ("null", "Child (RAT)", "ugly (cow)", "Yin (Tiger)", "Mao (rabbit )", "Chen (Dragon )",
  132. "Si (snake)", "Wu (horse)", "Wei (goat)", "Shen (monkey)", "you (chicken)", "Shen (dog) "," Hai (pig) "); # lunar month
  133. Var $ mmonth = array ("regular", "positive", "two", "three", "four", "five", "six ",
  134. "7", "8", "9", "10", "11", "12", "month ");
  135. Var $ mday = array ("null", "First Day", "second day", "Third Day", "fourth day", "Fifth Day", "Sixth Day", "Seventh Day ", "Eighth Day", "Ninth Day", "Tenth Day ",
  136. "11", "12", "13", "14", "15", "16", "17", "18", "19", "20 ",
  137. "Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5", "Jun 6", "Jun 7", "Jun 8 ", "29th", "30th"); # lunar calendar day
  138. ##############################
  139. # Assign an initial value
  140. # Tiangan land support
  141. Var $ ten = 0;
  142. Var $ twelve = 0;
  143. Function MyPub ($ cYear, $ cMonth, $ cDay)
  144. {
  145. $ Total = 11; # The total number of days of the Gregorian calendar to January 1, December 21, 1900
  146. $ Mtotal = 0; # Total calendar days
  147. For ($ y = 1901; $ y <$ cYear; $ y ++)
  148. {
  149. $ Total + = 365;
  150. If ($ y % 4 = 0) $ total ++;
  151. }
  152. # Plus a few months of the current year
  153. Switch ($ cMonth ){
  154. Case 12:
  155. $ Total + = 30;
  156. Case 11:
  157. $ Total + = 31;
  158. Case 10:
  159. $ Total + = 30;
  160. Case 9:
  161. $ Total + = 31;
  162. Case 8:
  163. $ Total + = 31;
  164. Case 7:
  165. $ Total + = 30;
  166. Case 6:
  167. $ Total + = 31;
  168. Case 5:
  169. $ Total + = 30;
  170. Case 4:
  171. $ Total + = 31;
  172. Case 3:
  173. $ Total + = 28;
  174. Case 2:
  175. $ Total + = 31;
  176. }
  177. # Add one day for a leap year
  178. If ($ cYear % 4 = 0 and $ cMonth> 2)
  179. {
  180. $ Total ++;
  181. }
  182. $ Total + = $ cDay-1;
  183. ##############################
  184. # Use the number of days in the lunar calendar to determine whether the number of days exceeds the number of days in the Gregorian calendar
  185. For ($ j = 0; $ j <= 120; $ j ++)
  186. {
  187. $ I = 1;
  188. For ($ I = 1; $ I <= 13; $ I ++)
  189. {
  190. $ Mtotal + = $ this-> everyCMonth [$ j] [$ I];
  191. If ($ mtotal> = $ total)
  192. {
  193. $ Flag = 1;
  194. Break;
  195. }
  196. }
  197. If ($ flag = 1) break;
  198. }
  199. Return array ("m" => $ j, "d" => $ I, "t" => $ total, "n" => $ mtotal );
  200. }
  201. Function Cal ($ Year, $ Month, $ Day)
  202. {
  203. $ Par = $ this-> MyPub ($ Year, $ Month, $ Day );
  204. $ Md = $ this-> everyCMonth [$ Par ["m"] [$ Par ["d"]-($ Par ["n"]-$ Par [" t "]);
  205. $ Week = ($ Par ["t"] + 5) % 7;
  206. If ($ this-> everyCMonth [$ Par ["m"] [0] <> 0 and $ this-> everyCMonth [$ Par ["m"] [0] <$ Par ["d"])
  207. {
  208. $ Mm = $ Par ["d"]-1;
  209. } Else {
  210. $ Mm = $ Par ["d"];
  211. }
  212. If ($ Par ["d"] ==$ this-> everyCMonth [$ Par ["d"] [0] + 1 and $ this-> everyCMonth [$ Par ["d"] [0] <> 0)
  213. {
  214. $ CMonth = $ this-> mmonth [0]. $ this-> mmonth [$ mm]; # leap month
  215. } Else {
  216. $ CMonth = $ this-> mmonth [$ mm]. $ this-> mmonth [13];
  217. }
  218. Return array ("year" => $ this-> mten [$ this-> everyCMonth [$ Par ["m"] [14]. $ this-> mtwelve [$ this-> everyCMonth [$ Par ["m"] [15],
  219. "Month" => $ cMonth,
  220. "Day" => $ this-> mday [$ md],
  221. "Week" => $ week );
  222. }
  223. }
  224. //// Call
  225. $ Test = new Calendar;
  226. $ Year = $ test-> Cal (hz12, 20 );
  227. Echo $ Year ["year"]. "Year ". $ Year ["month"]. $ Year ["day"]. "Week ". $ Year ["week"];
  228. ?>


PHP

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.