PHP Lunar Calendar Conversion

Source: Internet
Author: User
PHP Lunar Calendar Conversion
PHP Lunar Calendar Conversion

  1. /*
  2. School of Computer Science and Engineering, Qujing Normal University, Yunnan province-Edited by Yang Haixi
  3. 9-9-3
  4. */
  5. Class Lunar
  6. {
  7. Private $ _ SMDay = array (1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31 ); // define the calendar month/day
  8. Private $ _ LStart = 1950; // the lunar calendar starts from January 1, 1950.
  9. Private $ _ LMDay = array (
  10. // Difference: the number of days from January 1, lunar January 1 to January 1, January 1 of the year ~ 12: The number of days of the lunar month; second: if a leap month exists, record the number of days of the month.
  11. // Difference 1 2 3 4 5 6 7 8 9 10 11 12 Hour
  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, 29, 30, 29, 30, 29), // August 15, May 29, May 30
  15. Array (44,29, 30,29, 29,30, 30,29, 30,30, 29,30, 29 ),
  16. Array (, 30, 30, 30 ),
  17. Array (,), // August 15
  18. Array (, 30, 30 ),
  19. Array (,), // August 15
  20. Array (, 30, 30, 29, 29, 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), // August 15, June 30, 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), // August 15, April 29, April 29
  26. Array (, 30, 30, 30 ),
  27. Array (32, 29, 30, 29, 30, 29, 29, 29, 29, 30, 29 ),
  28. Array (,), // August 15
  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), // August 15, July 30, 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), // August 15, May 30, May 29
  34. Array (45,29, 30,29, 29,30, 29,30, 29,30, 30,29, 30 ),
  35. Array (, 30 ),
  36. Array (,), // August 15
  37. Array (, 30 ),
  38. Array (30,30, 30,29, 30,29, 30,29, 59,29, 30,29, 30,30), // August 15, August 30, August 29
  39. Array (, 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), // August 15, June 30, June 30
  42. Array (, 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), // August 15, April 29, April 29
  45. Array (, 30, 30 ),
  46. Array (,), // August 15
  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), // August 15, June 30, 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 (,), // August 15
  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 (,), // August 15
  56. Array (, 30 ),
  57. Array (30,29, 30,30, 29,30, 29,30, 59,29, 30,29, 30,30), // August 15, August 30, 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), // August 15, May 29, May 29
  61. Array (, 29, 29 ),
  62. Array (35,30, 30,29, 29,30, 29,29, 30,29, 30,30, 29 ),
  63. Array (,), // August 15
  64. Array (, 29, 29, 29 ),
  65. Array (, 30, 30, 30 ),
  66. Array (,), // August 15
  67. Array (39,29, 30,29, 30,29, 30,30, 29,30, 29,30, 29 ),
  68. Array (,), // August 15
  69. Array (, 30, 30 ),
  70. Array (37,30, 29,29, 30,29, 29,30, 29,30, 30,29, 30 ),
  71. Array (,), // August 15
  72. Array (, 30 ),
  73. Array (, 29, 29, 29 ),
  74. Array (,), // August 15
  75. Array (, 30, 30 ),
  76. Array (30,29, 30,29, 30,29, 30,29, 30,29, 30,59, 30,29, 30,30), // August 15, September 30, 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), // August 15, June 29, June 30
  80. Array (, 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), // August 15, April 30, April 29
  83. Array (, 30, 30, 29 ),
  84. Array (, 30, 29, 30 ),
  85. Array (, 30, 30,), // August 15
  86. Array (, 29 ),
  87. Array (,), // August 15
  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 (, 30, 30,), // August 15
  91. Array (, 30, 30 ),
  92. Array (, 29 ),
  93. Array (, 30, 30, 30,), // August 15
  94. Array (, 29, 30, 30, 30, 30 ),
  95. Array (,), // August 15
  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), // August 15, June 29, 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 (,), // August 15
  102. Array (, 29, 29 ),
  103. Array (, 30, 30, 30 ),
  104. Array (,), // August 15
  105. Array (, 30 ),
  106. Array (29,30, 29,30, 29,29, 30,58, 30,29, 30,30, 30,29), // August 15, July 29, 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,30, 29,29, 30,29, 30 ),
  109. Array (,), // August 15
  110. Array (44,29, 30,30, 29,30, 30,29, 30,29, 29,30, 29 ),
  111. Array (, 29, 29 ),
  112. Array (,), // August 15
  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 a calendar month (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. // Convert the lunar calendar name
  127. Private function LYearName ($ year)
  128. {
  129. $ Name = array ("zero", "one", "two", "three", "four", "five", "six", "seven ", "8", "9 ");
  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 => "zheng", "2", "3", "4", "5", "6", "7", "8 ", "9", "10", "11", "12 ");
  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", "second day", "Third Day", "fourth day", "Fifth Day", "Sixth Day", "Seventh Day", "eighth day", "Ninth Day", "Tenth Day ",
  151. "11", "12", "13", "14", "15", "16", "17", "18", "19", "20 ",
  152. "Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5", "Jun 6", "Jun 7", "Jun 8 ", "9th", "30th"
  153. );
  154. Return $ Name [$ day];
  155. }
  156. Return $ day;
  157. }
  158. // Convert the Gregorian calendar to the lunar calendar (Sdate: the 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. // Obtain the number of days from the query date to the month 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. // Obtain the lunar data of the corresponding year and convert 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. = "(Updated )";
  210. // Return $ Ldate;
  211. }
  212. // Convert the lunar calendar to the Gregorian calendar (date: the 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; // The statement must be queried, but no leap month is queried.
  219. // If the queried lunar calendar is a leap month and the year's lunar calendar array contains the leap month data
  220. If ($ Larray [$ month]> 30 & $ type = 1 & count ($ Larray)> = 13) $ day = $ Larray [13] + $ day;
  221. // Obtain the number of days from the lunar date of the current year to the calendar month of the current year.
  222. $ Days = $ day;
  223. For ($ I = 0; $ I <= $ month-1; $ I ++)
  224. $ Days + = $ Larray [$ I];
  225. // When the query date is more than one year from the Gregorian calendar on January 1, January 1
  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. ?>

  1. Require_once 'lunar. php ';
  2. $ Today = date ("Y-m-d ");
  3. $ Lunar = new Lunar ();
  4. // Convert the Gregorian calendar to the lunar calendar
  5. $ Nl = date ("Y-m-d", $ lunar-> S2L ($ today ));
  6. // Convert the lunar calendar to the Gregorian calendar
  7. $ Gl = date ("Y-m-d", $ lunar-> L2S ($ nl ));
  8. Echo "today's Gregorian calendar is: $ today
    ";
  9. Echo "converted to lunar calendar: $ nl
    ";
  10. Echo "returns to the Gregorian calendar: $ gl
    ";
  11. ?>

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.