Example of converting a calendar into a calendar using PHP programming.

Source: Internet
Author: User

Example of converting a calendar into a calendar using PHP programming.

This article describes how to convert a solar calendar to a lunar calendar by PHP programming. We will share this with you for your reference. The details are as follows:

Php

<? Php/*** Class Lunar * Lunar solar festival * author: guoyu @ xzd */class Lunar {public $ MIN_YEAR = 1891; public $ MAX_YEAR = 2100; public $ lunarInfo = array (,), array (), array, 26,43344), array (,), array ), array (,), ar Ray (,), array (,), array, ), array ), array (,), array, 20, 18800), array (,), array (,), Array, ), array ), array (,), array, 8, 54432), array (6, 1, 27,55888), array (,), array (,), array (4, 43736,), array (,), array ), array (,), array, 24,21176), array (,), array ), array (,), array, 13, 42352), array ), array (,), array, 11,53840), array (, 54568), array (,), array ), array (,), array (, 2, 2, 45656), array (,), array ), array (,), array, 31,27432), array (,), array ), array (22,232, 24, 54440), array (,) 08), array (,), array ), array (,), array, 19,19360), array (,), array ), array (,), array (,), array (), array), Array, ), array ), array (,), array, rows), array (,), array ), Array (,), array, ), array ), array (,), array (), array (,), array, ), array (,), arr Ay (,), array (,), array, 11,21200), array (, 43352), array (,), array ), array (,), array (, 24, 48), array (, 12,), array (,), array, 9, 26928), array (,), array (5, 19880,), array (,), array, 30,59696), array (,), array ), array (,), array )); /*** convert the Gregorian calendar to the Gregorian calendar * @ param year Gregorian calendar-year * @ param month Gregorian calendar-month * @ param date Gregorian calendar-day */public func Tion convertSolarToLunar ($ year, $ month, $ date) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; if ($ year = $ this-> MIN_YEAR & $ month <= 2 & $ date <= 9) return array (1891, 'October 11', '1e ', 'sinmao ', 'rabbit'); return $ this-> getLunarByBetween ($ year, $ this-> getDaysBetweenSolar ($ year, $ month, $ date, $ yearData [1], $ yearData [2]);} public function convertSolarMonthToLunar ($ year, $ month, $ date) {$ yearData = $ this-> lunarInfo [$ y Ear-$ this-> MIN_YEAR]; if ($ year = $ this-> MIN_YEAR & $ month <= 2 & $ date <= 9) return array (1891, 'August 1', '1nday', 'sinmao ', lunar January, 'rabbit'); $ month_days_ary = array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); $ dd = $ month_days_ary [$ month]; if ($ this-> isLeapYear ($ year) & $ month = 2) $ dd ++; $ lunar_ary = array (); for ($ I = 1; $ I <$ dd; $ I ++) {$ array = $ this-> getLunarByBetween ($ year, $ this-> getDaysBetweenSolar ($ year, $ mont H, $ I, $ yearData [1], $ yearData [2]); $ array [] = $ year. '-'. $ month. '-'. $ I; $ lunar_ary [$ I] = $ array;} return $ lunar_ary ;} /*** convert the lunar calendar to the Gregorian calendar * @ param year-year * @ param month indicates the lunar calendar-month, And the leap month is processed. For example, if the current year expires on January 1, May, then the second month of May will be passed to June, which is equivalent to 13 months in the lunar calendar, sometimes, the number of days in the first month is 0 * @ param date: day */public function convertLunarToSolar ($ year, $ month, $ date) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; $ between = $ this-> getDaysBetw EenLunar ($ year, $ month, $ date); $ res = mktime (0, 0, $ yearData [1], $ yearData [2], $ year ); $ res = date ('Y-m-d', $ res + $ between * 24*60*60); $ day = explode ('-', $ res ); $ year = $ day [0]; $ month = $ day [1]; $ day = $ day [2]; return array ($ year, $ month, $ day);}/*** determine whether it is a leap year * @ param year */public function isLeapYear ($ year) {return ($ year % 4 = 0 & $ year % 100! = 0) | ($ year % 400 = 0);}/*** obtain the year of the credit calculation * @ param year */public function getLunarYearName ($ year) {$ sky = array ('geng ', 'sin', 'shen', 'cap', 'A', 'B', 'bing', 'ding ', 'pente', 'jid'); $ earth = array ('shen', 'uni', 'shanghai', 'sub', 'ug ', 'yin ', 'mao', 'chen ', 'si', 'wu ', 'wei'); $ year = $ year. ''; return $ sky [$ year {3}]. $ earth [$ year % 12];}/*** obtain the Chinese zodiac * @ param year lunar year */public function getYearZodiac ($ year) based on the lunar year) {$ zodiac = array ('Monkey ', 'chicken', 'Dog', 'pig', 'rat ', 'ox', 'tiger ', 'rabbit', 'Long', 'snake ', 'Ma', 'yang'); return $ zodiac [$ year % 12];} /*** get the number of days of the calendar month * @ param year Gregorian calendar-year * @ param month Gregorian calendar-month */public function getSolarMonthDays ($ year, $ month) {$ 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 ); return $ monthHash ["$ month"];}/*** get the number of days of the calendar month * @ param year lunar calendar-year * @ param month Lunar Calendar-month, */public function getLunarMonthDays ($ year, $ month) {$ monthData = $ this-> getLunarMonths ($ year); return $ monthData [$ month-1];} /*** get the array of the number of days of the calendar month * @ param year */public function getLunarMonths ($ year) {$ yearData = $ th Is-> lunarInfo [$ year-$ this-> MIN_YEAR]; $ leapMonth = $ yearData [0]; $ bit = decbin ($ yearData [3]); for ($ I = 0; $ I <strlen ($ bit); $ I ++) $ bitArray [$ I] = substr ($ bit, $ I, 1 ); for ($ k = 0, $ klen = 16-count ($ bitArray); $ k <$ klen; $ k ++) array_unshift ($ bitArray, '0 '); $ bitArray = array_slice ($ bitArray, 0, ($ leapMonth = 0? 12: 13); for ($ I = 0; $ I <count ($ bitArray); $ I ++) $ bitArray [$ I] = $ bitArray [$ I] + 29; return $ bitArray ;} /*** get the number of days of the lunar calendar year * @ param year */public function getLunarYearDays ($ year) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; $ monthArray = $ this-> getLunarYearMonths ($ year ); $ len = count ($ monthArray); return ($ monthArray [$ len-1] = 0? $ MonthArray [$ len-2]: $ monthArray [$ len-1]);} public function getLunarYearMonths ($ year) {$ monthData = $ this-> getLunarMonths ($ year ); $ res = array (); $ temp = 0; $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; $ len = ($ yearData [0] = 0? 12: 13); for ($ I = 0; $ I <$ len; $ I ++) {$ temp = 0; for ($ j = 0; $ j <= $ I; $ j ++) $ temp + = $ monthData [$ j]; array_push ($ res, $ temp);} return $ res ;} /*** get a leap month * @ param year lunar calendar year */public function getLeapMonth ($ year) {$ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR]; return $ yearData [0];} /*** calculate the number of days between the lunar calendar date and January 1, lunar January 1 * @ param year * @ param month * @ param date */public function getDaysBetweenLunar ($ year, $ month, $ date) {$ YearMonth = $ this-> getLunarMonths ($ year); $ res = 0; for ($ I = 1; $ I <$ month; $ I ++) $ res + = $ yearMonth [$ I-1]; $ res + = $ date-1; return $ res ;} /*** calculate the number of days between two calendar dates * @ param year calendar year * @ param cmonth * @ param cdate * @ param dmonth the calendar month corresponding to January 1, lunar January * @ param ddate the number of calendar days corresponding to the first day */public function getDaysBetweenSolar ($ year, $ cmonth, $ cdate, $ dmonth, $ ddate) {$ a = mktime (0, 0, $ cmonth, $ cdate, $ year); $ B = mktime (0, 0, 0, $ dmont H, $ ddate, $ year); return ceil ($ a-$ B)/24/3600 );} /*** calculate the lunar calendar date based on the number of days from January 1, lunar January 1 * @ param year * @ param between number of days */public function getLunarByBetween ($ year, $) {$ lunarArray = array (); $ yearMonth = array (); $ t = 0; $ e = 0; $ leapMonth = 0; $ m = ''; if ($ between = 0) {array_push ($ lunarArray, $ year, 'August 1', '1n1'); $ t = 1; $ e = 1 ;} else {$ year = $ between> 0? $ Year: ($ year-1); $ yearMonth = $ this-> getLunarYearMonths ($ year); $ leapMonth = $ this-> getLeapMonth ($ year ); $ between = $ between> 0? $ Between: ($ this-> getLunarYearDays ($ year) + $ between); for ($ I = 0; $ I <13; $ I ++) {if ($ between = $ yearMonth [$ I]) {$ t = $ I + 2; $ e = 1; break ;} else if ($ between <$ yearMonth [$ I]) {$ t = $ I + 1; $ e = $ between-(empty ($ yearMonth [$ I-1])? 0: $ yearMonth [$ I-1]) + 1; break ;}}$ m = ($ leapMonth! = 0 & $ t = $ leapMonth + 1 )? ('Authorization'. $ this-> getCapitalNum ($ t-1, true): $ this-> getCapitalNum ($ leapMonth! = 0 & $ leapMonth + 1 <$ t? ($ T-1): $ t), true); array_push ($ lunarArray, $ year, $ m, $ this-> getCapitalNum ($ e, false ));} array_push ($ lunarArray, $ this-> getLunarYearName ($ year); // array_push ($ lunarArray, $ t, $ e); array_push ($ lunarArray, $ this-> getYearZodiac ($ year); // 12 zodiac array_push ($ lunarArray, $ leapMonth); // returns $ lunarArray ;} /*** obtain the digit's lunar name * @ param num Number * @ param isMonth is the number of the month */public function getCapitalNum ($ num, $ I SMonth) {$ isMonth = $ isMonth | false; $ dateHash = array ('0' => '', '1' => '1 ', '2' => '2', '3' => '3', '4' => '4', '5' => '5 ', '6' => '6', '7' => '7', '8' => '8', '9' => '9 ', '10' => '10'); $ monthHash = array ('0' => '', '1' => 'August 1 ', '2' => 'August 11', '3' => 'August 11', '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'); $ res = ''; if ($ isMonth) $ res = $ monthHash [$ num]; else {if ($ num <= 10) $ res = 'chu '. $ dateHash [$ num ]; Else if ($ num> 10 & $ num <20) $ res = '10 '. $ dateHash [$ num-10]; else if ($ num = 20) $ res = "20"; else if ($ num> 20 & $ num <30) $ res = "Hangzhou ". $ dateHash [$ num-20]; else if ($ num = 30) $ res = "Thirty";} return $ res ;} /*** common solar saving algorithm */public function getJieQi ($ _ year, $ month, $ day) {$ year = substr ($ _ year,-2) + 0; $ coefficient = array (5.4055, 2019,-1), // small cold array (20.12, 2082,1), // large cold array (3.87), // early spring array (18.74, 2026,-1 ), // Rain array (5.63), // shocking array (20.646, 4.81, 20.1,), // Spring Equinox array (), // Qingming array ), // grain rain array (5.52, 21.04), // lixia array (5.678, 21.37), // small full array (,), // grain array ), // array (7.108, 22.83), // array (7.5, 23.13,), // array (,), // array ), // sunning array (7.646, 23.042, 8.318), // White Dew array (23.438,), // autumn Ray array (), // Cold Dew array (, 1 ), // Frost drop array (7.438, 1), // winter array (22. 36,1978, 1), // light snow array (7.18, 1954,1), // heavy snow array (21.94, 2021,-1) // winter solstice ); $ term_name = array ("Xiao Han", "da Han", "Li Chun", "Rain", "Jing zhe", "Spring Equinox", "Qingming", "Gu Yu ", "Summer", "small full", "Mountain", "Summer Solstice", "Small Heat", "great heat", "Autumn", "Summer heat", "White Dew", "Autumn Equinox ", "Cold Dew", "frost", "Winter", "light snow", "heavy snow", "Winter Solstice"); $ idx1 = ($ month-1) * 2; $ _ leap_value = floor ($ year-1)/4); $ day1 = floor ($ year * 0.2422 + $ coefficient [$ idx1] [0]) -$ _ leap_value; if (isset ($ coefficient [$ idx1] [1]) & $ coefficient [$ idx1] [1] == _ year) $ Day1 + = $ coefficient [$ idx1] [2]; $ day2 = floor ($ year * 0.2422 + $ coefficient [$ idx1 + 1] [0]) -$ _ leap_value; if (isset ($ coefficient [$ idx1 + 1] [1]) & $ coefficient [$ idx1 + 1] [1] === _ year) $ day1 + = $ coefficient [$ idx1 + 1] [2]; if ($ day = $ day1) return $ term_name [$ idx1]; if ($ day = $ day2) return $ term_name [$ idx1 + 1]; return '';}/*** get the Festival: this function can only be modified for special holidays. */public function getFestival ($ today, $ nl_info = false, $ config = 1) {If ($ config = 1) {$ 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' => 'double ninth Day', '12-08' => 'labary ', '12-23' => 'Year'); $ arr_solar = array ('01-01 '=> 'newyear', '02-14' => 'valentine's Day ', '03-12' => 'tree planting festival ', '04-01' => 'yuday', '05-01 '=> 'Labor Day ', '06-01' => 'children's Day', '10-01' => 'National Day', '10-31' => 'Halloween ', '12-24' => 'Christmas Eve ', '12-25' => 'Christmas ');} // for different holidays, use different $ config, then configure $ arr_lunar and $ arr_solar $ festival. S = array (); list ($ y, $ m, $ d) = explode ('-', $ today); if (! $ Nl_info) $ nl_info = $ this-> convertSolarToLunar ($ y, intval ($ m), intval ($ d )); if ($ nl_info [7]> 0 & $ nl_info [7] <$ nl_info [4]) $ nl_info [4]-= 1; $ md_lunar = substr ('0 '. $ nl_info [4],-2 ). '-'. substr ('0 '. $ nl_info [5],-2); $ md_solar = substr_replace ($ today, '', 0, 5); isset ($ arr_lunar [$ md_lunar])? Array_push ($ festivals, $ arr_lunar [$ md_lunar]): ''; isset ($ arr_solar [$ md_solar])? Array_push ($ festivals, $ arr_solar [$ md_solar]): ''; $ glweek = date (" w ", strtotime ($ today )); // 0-6 if ($ m = 5 & ($ d> 7) & ($ d <15) & ($ glweek = 0 )) array_push ($ festivals, "Mother's Day"); if ($ m = 6 & ($ d> 14) & ($ d <22) & ($ glweek = 0) array_push ($ festivals, "Father's Day"); $ jieqi = $ this-> getJieQi ($ y, $ m, $ d ); if ($ jieqi) array_push ($ festivals, $ jieqi); return implode ('/', $ festivals );}}

Call method:

$lunar = new Lunar();$month = $lunar->convertSolarToLunar(date('Y'),date('m'),date('d'));print_r($month);

Running result:

Array ([0] => 2017 [1] => April June [2] => 17 [3] => ding you [4] => 7 [5] => 17 [6] => chicken [7] => 6)

PS: Here are some calendar-related online tools for your reference:

Web calendar:
Http://tools.jb51.net/bianmin/webwannianli

Online calendar/Calendar Conversion Tool:
Http://tools.jb51.net/bianmin/yinli2yangli

Online calendar:
Http://tools.jb51.net/bianmin/wannianli

Online calendar flash version:
Http://tools.jb51.net/bianmin/flashwnl

In addition, this siteToday in historyThere are also similar lunar date display functions:

Http://tools.jb51.net/bianmin/lishi

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.