PHP computing festival, solar term, and agricultural calendar Interchange Code

Source: Internet
Author: User
PHP computing festival, solar term, and agricultural calendar Interchange Code

  1. /*
  2. * Lunar holidays
  3. * Edit: bbs.it-home.org
  4. */
  5. Class Lunar {
  6. Var $ MIN_YEAR = 1891;
  7. Var $ MAX_YEAR = 2100;
  8. Var $ lunarInfo = array (
  9. Array (,), array, 13,59728 ),
  10. Array (,), array (), array, 8, 42352 ),
  11. Array (,), array, 2, 32 ),
  12. Array (,), array, 26,54568 ),
  13. Array (,), array, 20,18800 ),
  14. Array (,), array, 13,11104 ),
  15. Array (,), array, 6 ),
  16. Array (,), array, 31,51560 ),
  17. Array (,), array (, 1, 25,43736 ),
  18. Array (,), array, 17,27808 ),
  19. Array (,), array, 12,21168 ),
  20. Array (,), array, 5, 19296 ),
  21. Array (,), array, 30,23208 ),
  22. Array (,), array, 23,53864 ),
  23. Array (,), array, 16,38320 ),
  24. Array (,), array, 9,27968 ),
  25. Array (,), array, 4,25776 ),
  26. Array (,), array, 28,37736 ),
  27. Array (,), array, 22,23208 ),
  28. Array (,), array, 14,43344 ),
  29. Array (,), array, 8, 16 ),
  30. Array (,), array, 1, 44368 ),
  31. Array (,), array, 26,59696 ),
  32. Array (,), array, 19,19152 ),
  33. Array (,), array, 12,46496 ),
  34. Array (, rows), array, 6 ),
  35. Array (,), array, 1, 18808 ),
  36. Array (,), array, 24,43872 ),
  37. Array (,), array, 17,54432 ),
  38. Array (,), array, 11,21200 ),
  39. Array (,), array (), array, 5, 21920 ),
  40. Array (, 24, 48), array (, 12,), array (,), array, 29,29336 ),
  41. Array (,), array, 24,21104 ),
  42. Array (,), array, 15,22133 ),
  43. Array (,), array, 9,54560)
  44. );
  45. /**
  46. * Convert a solar calendar to a lunar calendar
  47. * @ Param year-year
  48. * @ Param month: Calendar-month
  49. * @ Param date: calendar-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, 'August 11', '1st 1', 'sinmao ', lunar January, '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, 'August 11', '1st 1', 'sinmao ', lunar January, 'rabbit ');
  63. }
  64. $ Month_days_ary = array (31, 28, 31, 30, 31, 30, 31, 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 a calendar to a calendar
  77. * @ Param year: lunar calendar-year
  78. * @ Param month: lunar calendar-month, leap month processing: for example, if the current year expires on January 1, then the second month will be transferred on January 1, which is equivalent to 13 months in the calendar, sometimes the number of days in the first month is 0.
  79. * @ Param date: 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. * Determines whether a leap year is used.
  94. * @ Param year
  95. */
  96. Function isLeapYear ($ year ){
  97. Return ($ year % 4 = 0 & $ year % 100! = 0) | ($ year % 400 = 0 ));
  98. }
  99. /**
  100. * Obtain the credit year
  101. * @ Param year
  102. */
  103. Function getLunarYearName ($ year ){
  104. $ Sky = array ('geng', 'sin', 'hangzhou', 'cap', 'A', 'B', 'Bing', 'ding ', 'pente', 'Ji ');
  105. $ Earth = array ('shen', 'uni', 'Shanghai', 'sub', 'Ugly ', 'yin', 'Mao ', 'Chen ', 'Si', 'Wu ', 'Wei ');
  106. $ Year = $ year .'';
  107. Return $ sky [$ year {3}]. $ earth [$ year % 12];
  108. }
  109. /**
  110. * Obtain the Chinese zodiac based on the lunar calendar year
  111. * @ Param year: lunar calendar year
  112. */
  113. Function getYearZodiac ($ year ){
  114. $ Zodiac = array ('Monkey ', 'Chicken', 'dog', 'pig', 'Rat ', 'ox', 'Tiger ', 'rabbit ', 'Long', 'Snake ', 'ma', 'Yang ');
  115. Return $ zodiac [$ year % 12];
  116. }
  117. /**
  118. * Obtain the number of days of the calendar month.
  119. * @ Param year: year
  120. * @ Param month: Calendar-month
  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, '10' => 31, '11' => 30, '12' => 31 );
  124. Return $ monthHash ["$ month"];
  125. }
  126. /**
  127. * Obtain the number of days in the calendar month.
  128. * @ Param year: lunar calendar-year
  129. * @ Param month: lunar calendar-month, starting from January 1, January
  130. */
  131. Function getLunarMonthDays ($ year, $ month ){
  132. $ MonthData = $ this-> getLunarMonths ($ year );
  133. Return $ monthData [$ month-1];
  134. }
  135. /**
  136. * Obtain the array of the number of days of the calendar 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 $ BitArray [$ I] = $ bitArray [$ I] + 29;
  151. }
  152. Return $ bitArray;
  153. }
  154. /**
  155. * Obtain the number of days of the lunar calendar year
  156. * @ Param year: lunar year
  157. */
  158. Function getLunarYearDays ($ year ){
  159. $ YearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];
  160. $ MonthArray = $ this-> getLunarYearMonths ($ year );
  161. $ Len = count ($ monthArray );
  162. Return ($ monthArray [$ len-1] = 0? $ MonthArray [$ len-2]: $ monthArray [$ len-1]);
  163. }
  164. Function getLunarYearMonths ($ year ){
  165. // Debugger;
  166. $ MonthData = $ this-> getLunarMonths ($ year );
  167. $ Res = array ();
  168. $ Temp = 0;
  169. $ YearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];
  170. $ Len = ($ yearData [0] = 0? 12: 13 );
  171. For ($ I = 0; $ I <$ len; $ I ++ ){
  172. $ Temp = 0;
  173. For ($ j = 0; $ j <= $ I; $ j ++ ){
  174. $ Temp + = $ monthData [$ j];
  175. }
  176. Array_push ($ res, $ temp );
  177. }
  178. Return $ res;
  179. }
  180. /**
  181. * Obtain a leap month
  182. * @ Param year indicates the year of the lunar calendar.
  183. */
  184. Function getLeapMonth ($ year ){
  185. $ YearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];
  186. Return $ yearData [0];
  187. }
  188. /**
  189. * Calculate the number of days between the lunar calendar date and January 1, lunar January 1.
  190. * @ Param year
  191. * @ Param month
  192. * @ Param date
  193. */
  194. Function getDaysBetweenLunar ($ year, $ month, $ date ){
  195. $ YearMonth = $ this-> getLunarMonths ($ year );
  196. $ Res = 0;
  197. For ($ I = 1; $ I <$ month; $ I ++ ){
  198. $ Res + = $ yearMonth [$ i-1];
  199. }
  200. $ Res + = $ date-1;
  201. Return $ res;
  202. }
  203. /**
  204. * Calculate the number of days between two calendar dates.
  205. * @ Param year
  206. * @ Param cmonth
  207. * @ Param cdate
  208. * @ Param dmonth: the calendar month corresponding to January 1, lunar January
  209. * @ Param ddate the number of calendar days corresponding to the first day of the lunar calendar
  210. */
  211. Function getDaysBetweenSolar ($ year, $ cmonth, $ cdate, $ dmonth, $ ddate ){
  212. $ A = mktime (0, 0, 0, $ cmonth, $ cdate, $ year );
  213. $ B = mktime (0, 0, 0, $ dmonth, $ ddate, $ year );
  214. Return ceil ($ a-$ B)/24/3600 );
  215. }
  216. /**
  217. * Calculate the calendar date based on the number of days from January 1, lunar January 1.
  218. * @ Param year
  219. * @ Param between number of days
  220. */
  221. Function getLunarByBetween ($ year, $ ){
  222. // Debugger;
  223. $ LunarArray = array ();
  224. $ YearMonth = array ();
  225. $ T = 0;
  226. $ E = 0;
  227. $ LeapMonth = 0;
  228. $ M = '';
  229. If ($ between = 0 ){
  230. Array_push ($ lunarArray, $ year, 'October 11', '1st year ');
  231. $ T = 1;
  232. $ E = 1;
  233. } Else {
  234. $ Year = $ between> 0? $ Year: ($ year-1 );
  235. $ YearMonth = $ this-> getLunarYearMonths ($ year );
  236. $ LeapMonth = $ this-> getLeapMonth ($ year );
  237. $ Between = $ between> 0? $ Between: ($ this-> getLunarYearDays ($ year) + $ );
  238. For ($ I = 0; $ I <13; $ I ++ ){
  239. If ($ between = $ yearMonth [$ I]) {
  240. $ T = $ I + 2;
  241. $ E = 1;
  242. Break;
  243. } Else if ($ between <$ yearMonth [$ I]) {
  244. $ T = $ I + 1;
  245. $ E = $ between-(emptyempty ($ yearMonth [$ i-1])? 0: $ yearMonth [$ i-1]) + 1;
  246. Break;
  247. }
  248. }
  249. $ M = ($ leapMonth! = 0 & $ t = $ leapMonth + 1 )? ('Authorization'. $ this-> getCapitalNum ($ t-1, true): $ this-> getCapitalNum ($ leapMonth! = 0 & $ leapMonth + 1 <$ t? ($ T-1): $ t), true );
  250. Array_push ($ lunarArray, $ year, $ m, $ this-> getCapitalNum ($ e, false ));
  251. }
  252. Array_push ($ lunarArray, $ this-> getLunarYearName ($ year); // daily support
  253. Array_push ($ lunarArray, $ t, $ e );
  254. Array_push ($ lunarArray, $ this-> getYearZodiac ($ year); // 12 Zodiac
  255. Array_push ($ lunarArray, $ leapMonth); // months of renewal
  256. Return $ lunarArray;
  257. }
  258. /**
  259. * Obtain the calendar name of a number
  260. * @ Param num number
  261. * @ Param isMonth is the number of the month
  262. */
  263. Function getCapitalNum ($ num, $ isMonth ){
  264. $ IsMonth = $ isMonth | false;
  265. $ DateHash = array ('0' => '', '1' => '1', '2' => '2', '3' => '3 ', '4' => '4', '5' => '5', '6' => '6', '7' => '7 ', '8' => '8', '9' => '9', '10' => '10 ');
  266. $ MonthHash = array ('0' => '', '1' => '20180101', '2' => '20180101', '3' => '20180101 ', '4' => 'August 11', '5' => 'August 11', '6' => 'August 11', '7' => 'August 11 ', '8' => 'August 11', '9' => 'August 11', '10' => 'August 11', '11' => 'August 11 ', '12' => 'August 11 ');
  267. $ Res = '';
  268. If ($ isMonth ){
  269. $ Res = $ monthHash [$ num];
  270. } Else {
  271. If ($ num <= 10 ){
  272. $ Res = 'chu'. $ dateHash [$ num];
  273. } Else if ($ num> 10 & $ num <20 ){
  274. $ Res = '10'. $ dateHash [$ num-10];
  275. } Else if ($ num = 20 ){
  276. $ Res = "20 ";
  277. } Else if ($ num> 20 & $ num <30 ){
  278. $ Res = "logs". $ dateHash [$ num-20];
  279. } Else if ($ num = 30 ){
  280. $ Res = "thirty ";
  281. }
  282. }
  283. Return $ res;
  284. }
  285. /*
  286. * Common solar saving algorithm
  287. */
  288. Function getJieQi ($ _ year, $ month, $ day)
  289. {
  290. $ Year = substr ($ _ year,-2) + 0;
  291. $ Coefficient = array (
  292. Array (5.4055, 2019,-1), // small cold
  293. Array (20.12, 2082,1), // Dahan
  294. Array (3.87), // Spring
  295. Array (18.74, 2026,-1), // rain
  296. Array (5.63), // stunned
  297. Array (20.646,), // Spring Equinox
  298. Array (4.81), // Qingming
  299. Array (20.1), // Grain Rain
  300. Array (5.52,), // Summer
  301. Array (21.04,), // fill
  302. Array (5.678,), // Mountain
  303. Array (21.37,), // Summer Solstice
  304. Array (7.108,), // slight heat
  305. Array (22.83,), // Large heat
  306. Array (7.5,), // Liqiu
  307. Array (23.13), // heatmap
  308. Array (7.646,), // White Dew
  309. Array (23.042,), // Autumn score
  310. Array (8.318), // Cold Dew
  311. Array (23.438, 1), // Frost
  312. Array (7.438, 1), // early winter
  313. Array (22.36,), // Light Snow
  314. Array (7.18,), // heavy snow
  315. Array (21.94, 2021,-1) // Winter Solstice
  316. );
  317. $ Term_name = array (
  318. "Xiao Han", "Da Han", "Li Chun", "Rain", "Jing Zhe", "Spring Equinox", "Qingming Festival", "Gu Yu ",
  319. "Summer", "Summer full", "mountain", "Summer Solstice", "Summer Heat", "Summer Heat", "Autumn", "Summer Heat ",
  320. "White Dew", "Autumn Equinox", "Cold Dew", "Frost fall", "Winter", "light snow", "heavy snow", "Winter Solstice ");
  321. $ Idx1 = ($ month-1) * 2;
  322. $ _ Leap_value = floor ($ year-1)/4 );
  323. $ Day1 = floor ($ year * 0.2422 + $ coefficient [$ idx1] [0])-$ _ leap_value;
  324. If (isset ($ coefficient [$ idx1] [1]) & $ coefficient [$ idx1] [1] ==$ _ year)
  325. {
  326. $ Day1 + = $ coefficient [$ idx1] [2];
  327. }
  328. $ Day2 = floor ($ year * 0.2422 + $ coefficient [$ idx1 + 1] [0])-$ _ leap_value;
  329. If (isset ($ coefficient [$ idx1 + 1] [1]) & $ coefficient [$ idx1 + 1] [1] === _ year)
  330. {
  331. $ Day1 + = $ coefficient [$ idx1 + 1] [2];
  332. }
  333. // Echo _ FILE _. '->'. _ LINE _. '$ day1 ='. $ day1, ', $ day2 ='. $ day2 .'
    '. Chr (10 );
  334. If ($ day = $ day1) return $ term_name [$ idx1];
  335. If ($ day = $ day2) return $ term_name [$ idx1 + 1];
  336. Return '';
  337. }
  338. /*
  339. * Get a holiday: This function can only be modified for special holidays.
  340. */
  341. Function getFestival ($ today, $ nl_info = false, $ config = 1)
  342. {
  343. If ($ config = 1)
  344. {
  345. $ Arr_lunar = array ('01-01 '=> 'Spring Festival', '01-15' => 'Lantern festival ', '02-02' => 'August 11 ', '05-05 '=> 'Dragon Boat festival', '07-07 '=> 'qixi festival', '08-15' => 'mid-autumn festival ', '09-09' => 'chongyang festival ', '12-08' => 'labarba', '12-23' => 'year ');
  346. $ Arr_solar = array ('01-01 '=> 'New Year', '02-14' => 'Valentine's Day', '03-12' => 'arbor day ', '04-01' => 'yue', '05-01 '=> 'Labor Day', '06-01' => 'Children's Day ', '10-01' => 'national day', '10-31' => 'Halloween ', '12-24' => 'Christmas Eve ', '12-25' => 'Christmas ');
  347. } // For different festivals, use different $ config and configure $ arr_lunar and $ arr_solar.
  348. $ Festivals = array ();
  349. List ($ y, $ m, $ d) = explode ('-', $ today );
  350. If (! $ Nl_info) $ nl_info = $ this-> convertSolarToLunar ($ y, intval ($ m), intval ($ d ));
  351. If ($ nl_info [7]> 0 & $ nl_info [7] <$ nl_info [4]) $ nl_info [4]-= 1;
  352. $ Md_lunar = substr ('0'. $ nl_info [4],-2). '-'. substr ('0'. $ nl_info [5],-2 );
  353. $ Md_solar = substr_replace ($ today, '', 0, 5 );
  354. Isset ($ arr_lunar [$ md_lunar])? Array_push ($ festivals, $ arr_lunar [$ md_lunar]): '';
  355. Isset ($ arr_solar [$ md_solar])? Array_push ($ festivals, $ arr_solar [$ md_solar]): '';
  356. $ Glweek = date ("w", strtotime ($ today); // 0-6
  357. If ($ m = 5 & ($ d> 7) & ($ d <15) & ($ glweek = 0) array_push ($ festivals, "Mother's Day ");
  358. If ($ m = 6 & ($ d> 14) & ($ d <22) & ($ glweek = 0) array_push ($ festivals, "Father's Day ");
  359. $ Jieqi = $ this-> getJieQi ($ y, $ m, $ d );
  360. If ($ jieqi) array_push ($ festivals, $ jieqi );
  361. Return implode ('/', $ festivals );
  362. }
  363. }
  364. Header ("Content-Type: text/html; charset = utf-8 ");
  365. $ Lunar = new Lunar ();//
  366. $ Month = $ lunar-> convertSolarToLunar (, 08); // converts a calendar to a calendar
  367. Echo'
    ';
  368. print_r($month);

Print result:

Array ([0] => 2013 [1] => May [2] => thirty [3] => Si [4] => 5 [5] => 30 [6] => snake [7] => 0)

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.