PHP solar calendar to lunar calendar

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

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

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.