PHP date operation code-lunar calendar-solar calendar conversion, leap year, computing days, etc. _ PHP Tutorial

Source: Internet
Author: User
PHP date operation code-lunar calendar-solar conversion, leap year, computing days, etc. This is a practical PHP date and time Operation class, it includes the Gregorian calendar-conversion of the lunar calendar, conversion to the Chinese date format, calculation of the number of days between the lunar calendar, acquisition of the Chinese zodiac based on the lunar calendar year, and acquisition of the lunar calendar. this is a practical PHP date and time Operation class, it includes the conversion of the Gregorian calendar to the lunar calendar, conversion to the Chinese date format, calculation of the number of days between the lunar calendar, acquisition of the Chinese zodiac based on the lunar calendar year, acquisition of the number of days of the lunar calendar month, acquisition of the number of days of the lunar calendar year, calculate the number of days between the calendar date and the calendar month, and calculate two calendars (calendars) the number of days between dates, the calculation of the calendar date based on the number of days from January 1, Lunar January 1, and the acquisition of the calendar year. PHP date Operation class: Lunar. class. the php code is as follows:

001

002 class Lunar {

003 var $ MIN_YEAR = 1891;

004 var $ MAX_YEAR = 2100;

005 var $ lunarInfo = array (

006 array (,), array (,), array, 13,59728 ),

007 array (,), array (,), array (), array, 8, 42352 ),

008 array (,), array, 2, 32 ),

Array (,), array (, 1, 26,54568 ),

010 array (,), array (,), array, 20,18800 ),

011 array (,), array, 13,11104 ),

012 array (,), array, 6 ),

013 array (,), array, 31,51560 ),

014 array (,), array (,), array (, 1, 25,43736 ),

015 array (,), array, 17,27808 ),

016 array (,), array, 12,21168 ),

017 array (, 43344), array (,), array (,), array (), array, 5, 19296 ),

018 array (,), array, 30,23208 ),

019 array (,), array (,), array (), array (,), array (,), array (), array (,), array (, 1, 23,53864 ),

020 array (,), array, 16,38320 ),

021 array (,), array, 9,27968 ),

022 array (,), array (,), array, 4,25776 ),

023 array (,), array (,), array (, 1, 28,37736 ),

024 array (,), array, 22,23208 ),

025 array (,), array (,), array, 14,43344 ),

026 array (,), array (,), array, 8, 16 ),

027 array (,), array (,), array, 1, 44368 ),

028 array (,), array (,), array (, 1, 26,59696 ),

029 array (,), array, 19,19152 ),

030 array (,), array, 12,46496 ),

031 array (, rows), array, 6 ),

032 array (,), array, 1, 18808 ),

033 array (,), array, 24,43872 ),

034 array (,), array, 17,54432 ),

035 array (, 55888), array, 11,21200 ),

036 array (, 43352), array, 5, 21920 ),

037 array (, 24, 48), array (, 12,), array, 29,29336 ),

038 array (, 17,27296), array (, 1, 24,21104 ),

039 array (,), array (,), array, 15,22133 ),

040 array (,), array (,), array (, 2, 9,54560)

041 );

042 /**

043 * convert a solar calendar to a lunar calendar

044 * @ param year-year

045 * @ param month Gregorian calendar-month

046 * @ param date Gregorian calendar-day

047 */

048 function convertSolarToLunar ($ year, $ month, $ date ){

049 // debugger;

050 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

051 if ($ year = $ this-> MIN_YEAR & $ month <= 2 & $ date <= 9 ){

052 return array (1891, 'August 11', '1st 1', 'sinmao ', lunar January, 'rabbit ');

053}

054 return $ this-> getLunarByBetween ($ year, $ this-> getDaysBetweenSolar ($ year, $ month, $ date, $ yearData [1], $ yearData [2]);

055}

056 function convertSolarMonthToLunar ($ year, $ month ){

057 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

058 if ($ year = $ this-> MIN_YEAR & $ month <= 2 & $ date <= 9 ){

059 return array (1891, 'August 11', '1st 1', 'sinmao ', lunar January, 'rabbit ');

060}

061 $ month_days_ary = array (31, 28, 31, 30, 31, 30, 31, 31, 30, 31 );

062 $ dd = $ month_days_ary [$ month];

063 if ($ this-> isLeapYear ($ year) & $ month = 2) $ dd ++;

064 $ lunar_ary = array ();

065 for ($ I = 1; $ I <$ dd; $ I ++ ){

066 $ array = $ this-> getLunarByBetween ($ year, $ this-> getDaysBetweenSolar ($ year, $ month, $ I, $ yearData [1], $ yearData [2]);

067 $ array [] = $ year. '-'. $ month. '-'. $ I;

068 $ lunar_ary [$ I] = $ array;

069}

070 return $ lunar_ary;

071}

072 /**

073 * determine whether it is a leap year

074 * @ param year

075 */

076 function isLeapYear ($ year ){

077 return ($ year % 4 = 0 & $ year % 100! = 0) | ($ year % 400 = 0 ));

078}

079 /**

080 * obtain the credit year

081 * @ param year

082 */

083 function getLunarYearName ($ year ){

084 $ sky = array ('geng ', 'sin', 'Shen', 'cap', 'A', 'B', 'Bing', 'ding ', 'pente', 'Ji ');

085 $ earth = array ('shen', 'unit', 'Shanghai', 'Hai', 'sub', 'ug', 'yin ', 'Mao ', 'Chen ', 'Si', 'Wu ', 'Wei ');

086 $ year = $ year .'';

087 return $ sky [$ year {3}]. $ earth [$ year % 12];

088}

089 /**

090 * obtain the Chinese zodiac based on the lunar calendar year

091 * @ param year

092 */

093 function getYearZodiac ($ year ){

094 $ zodiac = array ('Monkey ', 'Chicken', 'dog ', 'pig', 'Rat', 'ox ', 'Tiger', 'rabbit ', 'Long', 'Snake ', 'ma', 'Yang ');

095 return $ zodiac [$ year % 12];

096}

097 /**

098 * convert a calendar to a calendar

099 * @ param year lunar calendar-year

100 * @ param month: lunar calendar-month, leap month processing: for example, if the current year was, then the second month was, which is equivalent to 13 months in the calendar, sometimes the number of days in the first month is 0.

101 * @ param date: Day

102 */

103 function convertLunarToSolar ($ year, $ month, $ date ){

104 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

105 $ between = $ this-> getDaysBetweenLunar ($ year, $ month, $ date );

106 $ res = mktime (0, 0, 0, $ yearData [1], $ yearData [2], $ year );

107 $ res = date ('Y-m-D', $ res + $ between * 24*60*60 );

108 $ day = explode ('-', $ res );

109 $ year = $ day [0];

110 $ month = $ day [1];

111 $ day = $ day [2];

112 return array ($ year, $ month, $ day );

113}

114 /**

115 * obtain the number of days of the calendar month

116 * @ param year Gregorian calendar-year

117 * @ param month Gregorian calendar-month

118 */

119 function getSolarMonthDays ($ year, $ month ){

120 $ 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 );

121 return $ monthHash ["$ month"];

122}

123 /**

124 * obtain the number of days in the calendar month

125 * @ param year lunar calendar-year

126 * @ param month lunar calendar-month, starting from January 1, January

127 */

128 function getLunarMonthDays ($ year, $ month ){

129 $ monthData = $ this-> getLunarMonths ($ year );

130 return $ monthData [$ month-1];

131}

132 /**

133 * obtain the array of the number of days of the calendar month

134 * @ param year

135 */

136 function getLunarMonths ($ year ){

137 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

138 $ leapMonth = $ yearData [0];

139 $ bit = decbin ($ yearData [3]);

140 for ($ I = 0; $ I <strlen ($ bit); $ I ++ ){

141 $ bitArray [$ I] = substr ($ bit, $ I, 1 );

142}

143 for ($ k = 0, $ klen = 16-count ($ bitArray); $ k <$ klen; $ k ++ ){

144 array_unshift ($ bitArray, '0 ');

145}

146 $ bitArray = array_slice ($ bitArray, 0, ($ leapMonth = 0? 12: 13 ));

147 for ($ I = 0; $ I

148 $ bitArray [$ I] = $ bitArray [$ I] + 29;

149}

150 return $ bitArray;

151}

152 /**

153 * obtain the number of days of the lunar calendar year

154 * @ param year lunar year

155 */

156 function getLunarYearDays ($ year ){

157 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

158 $ monthArray = $ this-> getLunarYearMonths ($ year );

159 $ len = count ($ monthArray );

160 return ($ monthArray [$ len-1] = 0? $ MonthArray [$ len-2]: $ monthArray [$ len-1]);

161}

162 function getLunarYearMonths ($ year ){

163 // debugger;

164 $ monthData = $ this-> getLunarMonths ($ year );

165 $ res = array ();

166 $ temp = 0;

167 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

168 $ len = ($ yearData [0] = 0? 12: 13 );

169 for ($ I = 0; $ I <$ len; $ I ++ ){

170 $ temp = 0;

171 for ($ j = 0; $ j <= $ I; $ j ++ ){

172 $ temp + = $ monthData [$ j];

173}

174 array_push ($ res, $ temp );

175}

176 return $ res;

177}

178 /**

179 * obtain a leap month

180 * @ param year

181 */

182 function getLeapMonth ($ year ){

183 $ yearData = $ this-> lunarInfo [$ year-$ this-> MIN_YEAR];

184 return $ yearData [0];

185}

186 /**

187 * calculate the number of days between the calendar date and January 1, lunar January 1

188 * @ param year

189 * @ param month

190 * @ param date

191 */

192 function getDaysBetweenLunar ($ year, $ month, $ date ){

193 $ yearMonth = $ this-> getLunarMonths ($ year );

194 $ res = 0;

195 for ($ I = 1; $ I <$ month; $ I ++ ){

196 $ res + = $ yearMonth [$ i-1];

197}

198 $ res + = $ date-1;

199 return $ res;

200}

201 /**

202 * calculate the number of days between two calendar dates

203 * @ param year

204 * @ param cmonth

205 * @ param cdate

206 * @ param dmonth the calendar month corresponding to January 1, lunar January

207 * @ param ddate the number of calendar days corresponding to the first day of the lunar calendar

208 */

209 function getDaysBetweenSolar ($ year, $ cmonth, $ cdate, $ dmonth, $ ddate ){

210 $ a = mktime (0, 0, $ cmonth, $ cdate, $ year );

211 $ B = mktime (0, 0, $ dmonth, $ ddate, $ year );

212 return ceil ($ a-$ B)/24/3600 );

213}

214 /**

215 * calculate the calendar date based on the number of days from January 1, lunar January 1

216 * @ param year

217 * @ param between days

218 */

219 function getLunarByBetween ($ year, $ ){

220 // debugger;

221 $ lunarArray = array ();

222 $ yearMonth = array ();

223 $ t = 0;

224 $ e = 0;

225 $ leapMonth = 0;

226 $ m = '';

227 if ($ between = 0 ){

228 array_push ($ lunarArray, $ year, 'October 11', '1st year ');

229 $ t = 1;

230 $ e = 1;

231} else {

232 $ year = $ between> 0? $ Year: ($ year-1 );

233 $ yearMonth = $ this-> getLunarYearMonths ($ year );

234 $ leapMonth = $ this-> getLeapMonth ($ year );

235 $ between = $ between> 0? $ Between: ($ this-> getLunarYearDays ($ year) + $ );

236 for ($ I = 0; $ I <13; $ I ++ ){

237 if ($ between ==$ yearMonth [$ I]) {

238 $ t = $ I + 2;

239 $ e = 1;

240 break;

241} else if ($ between <$ yearMonth [$ I]) {

242 $ t = $ I + 1;

243 $ e = $ between-(empty ($ yearMonth [$ i-1])? 0: $ yearMonth [$ i-1]) + 1;

244 break;

245}

246}

247 $ m = ($ leapMonth! = 0 & $ t = $ leapMonth + 1 )? ('Authorization'. $ this-> getCapitalNum ($ t-1, true): $ this-> getCapitalNum ($ leapMonth! = 0 & $ leapMonth + 1 <$ t? ($ T-1): $ t), true );

248 $ my_year = $ this-> toYear ($ year );

249 array_push ($ lunarArray, $ my_year, $ m, $ this-> getCapitalNum ($ e, false ));

250}

251 array_push ($ lunarArray, $ this-> getLunarYearName ($ year ); //

252 array_push ($ lunarArray, $ t, $ e );

253 array_push ($ lunarArray, $ this-> getYearZodiac ($ year); // 12 Zodiac

254 array_push ($ lunarArray, $ leapMonth); // months of renewal

255 return $ lunarArray;

256}

257 // Convert to Chinese year

258 function toYear ($ year ){

259 $ arr = array ("zero", "one", "two", "three", "four", "five", "six", "seven ", "8", "9 ");

260 $ year_arr = str_split ($ year );

261 $ str = $ arr [$ year_arr [0]. $ arr [$ year_arr [1]. $ arr [$ year_arr [2]. $ arr [$ year_arr [3];

262 return $ str;

263}

264 /**

265 * obtain the screen name of a number

266 * @ param num number

267 * @ param isMonth is the number of the month

268 */

269 function getCapitalNum ($ num, $ isMonth ){

270 $ isMonth = $ isMonth | false;

271 $ dateHash = array ('0' => '', '1' => '1', '2' => '2 ', '3' => '3', '4' => '4', '5' => '5', '6' => '6 ', '7' => '7', '8' => '8', '9' => '9', '10' => '10 ');

272 $ monthHash = array ('0' => '', '1' => '20180101', '2' => '20180101 ', '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 ');

273 $ res = '';

274 if ($ isMonth ){

275 $ res = $ monthHash [$ num];

276} else {

277 if ($ num <= 10 ){

278 $ res = 'chu'. $ dateHash [$ num];

279} else if ($ num> 10 & $ num <20 ){

280 $ res = '10'. $ dateHash [$ num-10];

281} else if ($ num = 20 ){

282 $ res = "20 ";

283} else if ($ num> 20 & $ num <30 ){

284 $ res = "logs". $ dateHash [$ num-20];

285} else if ($ num = 30 ){

286 $ res = "thirty ";

287}

288}

289 return $ res;

290}

291}

292?>

Save the above code as: Lunar. class. php. the following example will call this file to show you how to use it.

01

02require_once ("Lunar. class. php"); // first include this file

03 $ lunar = new Lunar (); // Generate an object

04 $ date = $ lunar-> convertLunarToSolar (, 12); // Convert the Gregorian calendar to the lunar calendar

05 $ date = $ lunar-> getYearZodiac (, 12); // Obtain the Chinese zodiac based on the lunar calendar year

06 $ date = $ lunar-> isLeapYear (, 12); // judge a leap year

07 // other functions are also called in this way

08print_r ($ date );

09 // for the conversion from the lunar calendar to the Gregorian calendar, the code is as follows:

10 $ date = $ lunar-> convertLunarToSolar (, 12); // Convert the lunar calendar to the Gregorian calendar

11print_r ($ date );

12?>

In fact, some of the functions can be independently used as functions, and can be deleted if not needed.

...

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.