PHP Date Operation Class code-lunar-Gregorian conversion, leap year, calculation days, etc. _php tutorial

Source: Internet
Author: User
This is a practical php date-time operation class, which includes the Gregorian-lunar conversion, converted to Chinese date format, the calculation of the lunar calendar days apart, the lunar calendar year to obtain the lunar calendar days, the number of days to obtain the lunar calendar year, get a leap month, calculate the lunar date and the first day of the first month, calculate 2 Gregorian calendar ( Solar calendar) The number of days between dates, based on the number of days from the day of the month to calculate lunar dates, get zodiac, etc., PHP date operation class: Lunar.class.php code is as follows:

001

002class Lunar {

003 var $MIN _year = 1891;

004 var $MAX _year = 2100;

005 var $lunarInfo = Array (

006 Array (0,2,9,21936), Array (6,1,30,9656), Array (0,2,17,9584), Array (0,2,6,21168), Array (5,1,26,43344), Array ( 0,2,13,59728),

007 Array (0,2,2,27296), Array (3,1,22,44368), Array (0,2,10,43856), Array (8,1,30,19304), Array (0,2,19,19168), Array ( 0,2,8,42352),

008 Array (5,1,29,21096), Array (0,2,16,53856), Array (0,2,4,55632), Array (4,1,25,27304), Array (0,2,13,22176), Array ( 0,2,2,39632),

009 Array (2,1,22,19176), Array (0,2,10,19168), Array (6,1,30,42200), Array (0,2,18,42192), Array (0,2,6,53840), Array ( 5,1,26,54568),

010 Array (0,2,14,46400), Array (0,2,3,54944), Array (2,1,23,38608), Array (0,2,11,38320), Array (7,2,1,18872), Array ( 0,2,20,18800),

011 Array (0,2,8,42160), Array (5,1,28,45656), Array (0,2,16,27216), Array (0,2,5,27968), Array (4,1,24,44456), Array ( 0,2,13,11104),

012 Array (0,2,2,38256), Array (2,1,23,18808), Array (0,2,10,18800), Array (6,1,30,25776), Array (0,2,17,54432), Array ( 0,2,6,59984),

013 Array (5,1,26,27976), Array (0,2,14,23248), Array (0,2,4,11104), Array (3,1,24,37744), Array (0,2,11,37600), Array ( 7,1,31,51560),

014 Array (0,2,19,51536), Array (0,2,8,54432), Array (6,1,27,55888), Array (0,2,15,46416), Array (0,2,5,22176), Array ( 4,1,25,43736),

015 Array (0,2,13,9680), Array (0,2,2,37584), Array (2,1,22,51544), Array (0,2,10,43344), Array (7,1,29,46248), Array ( 0,2,17,27808),

016 Array (0,2,6,46416), Array (5,1,27,21928), Array (0,2,14,19872), Array (0,2,3,42416), Array (3,1,24,21176), Array ( 0,2,12,21168),

017 Array (8,1,31,43344), Array (0,2,18,59728), Array (0,2,8,27296), Array (6,1,28,44368), Array (0,2,15,43856), Array ( 0,2,5,19296),

018 Array (4,1,25,42352), Array (0,2,13,42352), Array (0,2,2,21088), Array (3,1,21,59696), Array (0,2,9,55632), Array ( 7,1,30,23208),

019 Array (0,2,17,22176), Array (0,2,6,38608), Array (5,1,27,19176), Array (0,2,15,19152), Array (0,2,3,42192), Array ( 4,1,23,53864),

020 Array (0,2,11,53840), Array (8,1,31,54568), Array (0,2,18,46400), Array (0,2,7,46752), Array (6,1,28,38608), Array ( 0,2,16,38320),

021 Array (0,2,5,18864), Array (4,1,25,42168), Array (0,2,13,42160), Array (10,2,2,45656), Array (0,2,20,27216), Array ( 0,2,9,27968),

022 Array (6,1,29,44448), Array (0,2,17,43872), Array (0,2,6,38256), Array (5,1,27,18808), Array (0,2,15,18800), Array ( 0,2,4,25776),

023 Array (3,1,23,27216), Array (0,2,10,59984), Array (8,1,31,27432), Array (0,2,19,23232), Array (0,2,7,43872), Array ( 5,1,28,37736),

024 Array (0,2,16,37600), Array (0,2,5,51552), Array (4,1,24,54440), Array (0,2,12,54432), Array (0,2,1,55888), Array ( 2,1,22,23208),

025 Array (0,2,9,22176), Array (7,1,29,43736), Array (0,2,18,9680), Array (0,2,7,37584), Array (5,1,26,51544), Array ( 0,2,14,43344),

026 Array (0,2,3,46240), Array (4,1,23,46416), Array (0,2,10,44368), Array (9,1,31,21928), Array (0,2,19,19360), Array ( 0,2,8,42416),

027 Array (6,1,28,21176), Array (0,2,16,21168), Array (0,2,5,43312), Array (4,1,25,29864), Array (0,2,12,27296), Array ( 0,2,1,44368),

028 Array (2,1,22,19880), Array (0,2,10,19296), Array (6,1,29,42352), Array (0,2,17,42208), Array (0,2,6,53856), Array ( 5,1,26,59696),

029 Array (0,2,13,54576), Array (0,2,3,23200), Array (3,1,23,27472), Array (0,2,11,38608), Array (11,1,31,19176), Array ( 0,2,19,19152),

030 Array (0,2,8,42192), Array (6,1,28,53848), Array (0,2,15,53840), Array (0,2,4,54560), Array (5,1,24,55968), Array ( 0,2,12,46496),

031 Array (0,2,1,22224), Array (2,1,22,19160), Array (0,2,10,18864), Array (7,1,30,42168), Array (0,2,17,42160), Array ( 0,2,6,43600),

032 Array (5,1,26,46376), Array (0,2,14,27936), Array (0,2,2,44448), Array (3,1,23,21936), Array (0,2,11,37744), Array ( 8,2,1,18808),

033 Array (0,2,19,18800), Array (0,2,8,25776), Array (6,1,28,27216), Array (0,2,15,59984), Array (0,2,4,27424), Array ( 4,1,24,43872),

034 Array (0,2,12,43744), Array (0,2,2,37600), Array (3,1,21,51568), Array (0,2,9,51552), Array (7,1,29,54440), Array ( 0,2,17,54432),

035 Array (0,2,5,55888), Array (5,1,26,23208), Array (0,2,14,22176), Array (0,2,3,42704), Array (4,1,23,21224), Array ( 0,2,11,21200),

036 Array (8,1,31,43352), Array (0,2,19,43344), Array (0,2,7,46240), Array (6,1,27,46416), Array (0,2,15,44368), Array ( 0,2,5,21920),

037 Array (4,1,24,42448), Array (0,2,12,42416), Array (0,2,2,21168), Array (3,1,22,43320), Array (0,2,9,26928), Array ( 7,1,29,29336),

038 Array (0,2,17,27296), Array (0,2,6,44368), Array (5,1,26,19880), Array (0,2,14,19296), Array (0,2,3,42352), Array ( 4,1,24,21104),

039 Array (0,2,10,53856), Array (8,1,30,59696), Array (0,2,18,54560), Array (0,2,7,55968), Array (6,1,27,27472), Array ( 0,2,15,22224),

040 Array (0,2,5,19168), Array (4,1,25,42216), Array (0,2,12,42192), Array (0,2,1,53584), Array (2,1,21,55592), Array ( 0,2,9,54560)

041);

042/**

043 * Convert the Gregorian calendar to lunar calendar

044 * @param year Gregorian calendar-years

045 * @param month Gregorian-monthly

046 * @param date Gregorian-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, ' The lunar month ', ' the ' Junior ', ' Xin Mao ', 1, 1, ' 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, ' The lunar month ', ' the ' Junior ', ' Xin Mao ', 1, 1, ' Rabbit ');

060}

061 $month _days_ary = Array (31, 28, 31, 30, 31, 30, 31, 31, 30, 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 if 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 * Get Ganzhi

081 * @param year

082 */

083 function Getlunaryearname ($year) {

084 $sky = Array (' g ', ' xin ', ' ren ', ' decyl ', ' A ', ' B ', ' C ', ' ding ', ' e ', ' own ');

085 $earth = Array (' shen ', ' unitary ', ' Xu ', ' hai ', ' zi ', ' ugly ', ' Yin ', ' Mao ', ' Chen ', ' have ', ' noon ', ' not ');

086 $year = $year. ";

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

088}

089/**

090 * Get Zodiac according to lunar year

091 * @param year Overcast

092 */

093 function Getyearzodiac ($year) {

094 $zodiac = Array (' monkey ', ' chicken ', ' dog ', ' pig ', ' rat ', ' cow ', ' tiger ', ' Rabbit ', ' dragon ', ' snake ', ' horse ', ' sheep ');

095 return $zodiac [$year%12];

096}

097/**

098 * Convert lunar calendar to solar calendar

099 * @param year lunar calendar-years

* @param month Lunar calendar-monthly, Leap months processing: For example, if the year of May, then the second May will be June, equivalent to the lunar calendar has 13 months, but sometimes the 13th month of days is 0

101 * @param date Lunar calendar-day

102 */

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

104 $yearData = $this->lunarinfo[$year-$this->min_year];

$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];

$month = $day [1];

111 $day = $day [2];

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

113}

114/**

115 * Number of days to get the Gregorian month

* @param year Gregorian calendar-years

117 * @param month solar-moon

118 */

119 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, ' ten ' =>31, ' one ' =>30, ' one ' =>31 ';

121 return $monthHash ["$month"];

122}

123/**

124 * Gets the number of days of the lunar month

* @param year lunar calendar-years

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

127 */

Getlunarmonthdays function ($year, $month) {

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

$monthData [$month-1];

131}

132/**

133 * Get an array of days of lunar 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]);

$ 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}

$bitArray of the return;

151}

152/**

153 * Get lunar calendar days per year

154 * @param lunar Year

155 */

156 function Getlunaryeardays ($year) {

157 $yearData = $this->lunarinfo[$year-$this->min_year];

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

159 $len = count ($monthArray);

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 + +) {

$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 * Get Leap month

* @param year lunar years

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 lunar date and the day of the month

188 * @param Year

189 * @param Month

* @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 * Calculates the number of days between 2 Gregorian dates

203 * @param year Sun

204 * @param Cmonth

205 * @param CDate

206 * @param the Gregorian month of the Dmonth lunar month

207 * @param the Gregorian calendar days of the Ddate lunar day

208 */

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

$a = Mktime (0,0,0, $cmonth, $cdate, $year);

211 $b = Mktime (0,0,0, $dmonth, $ddate, $year);

212 Return Ceil (($a-$b)/24/3600);

213}

214/**

215 * The lunar date is calculated based on the number of days from the month

216 * @param year Sun

217 * @param between days

218 */

219 function Getlunarbybetween ($year, $between) {

//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, ' The lunar month ', ' the Junior ');

229 $t = 1;

$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) + $between);

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

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

238 $t = $i +2;

239 $e = 1;

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)? (' Leap '. $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));//Zodiac earthly

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

253 Array_push ($lunarArray, $this->getyearzodiac ($year));//12 Zodiac

254 Array_push ($lunarArray, $leapMonth);//Leap months

255 return $lunarArray;

256}

257//Convert to Chinese year

258 function Toyear ($year) {

259 $arr = Array ("0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");

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 * Get the number of the lunar term

266 * @param num numbers

267 * @param Ismonth is the number of months

268 */

269 function Getcapitalnum ($num, $isMonth) {

$isMonth = $isMonth | | False

271 $dateHash =array (' 0 ' = ', ' 1 ' = ' one ', ' 2 ' = ' two ', ' 3 ' = ' three ', ' 4 ' = ' Four ', ' 5 ' = ' five ', ' 6 ' = ' Six ', ' 7 ' = ' + ') ' ' Seven ', ' 8 ' = ' eight ', ' 9 ' = ' nine ', ' ten ' = ' x ';

272 $monthHash =array (' 0 ' + = ', ' 1 ' = ' the ' month ', ' 2 ' = ' February ', ' 3 ' = ' March ', ' 4 ' = ' April ', ' 5 ' = = ' May ', ' 6 ' = ' + ' June ', ' 7 ' + ' July ', ' 8 ' = ' August ', ' 9 ' = ' September ', ' ' ten ' = ' + ' October ', ' one ' ' = ' = ', ' Winter months ' and ' lunar ';

273 $res = ";

274 if ($isMonth) {

275 $res = $monthHash [$num];

276}else{

277 if ($num <=10) {

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

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

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

281}else if ($num ==20) {

282 $res = "20";

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

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

285}else if ($num ==30) {

286 $res = "30";

287}

288}

289 return $res;

290}

291}

292?>

Please save the above code as: Lunar.class.php, the following example will call this file to tell you how to use it.

01

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

03$lunar = new Lunar ();//Generate Object

04$date = $lunar->convertlunartosolar (2014,2,12); Gregorian calendar to lunar calendar

05$date = $lunar->getyearzodiac (2014,2,12); Get Zodiac according to lunar year

06$date = $lunar->isleapyear (2014,2,12); Judging leap years

07//Other functions are also the calling method

08print_r ($date);

09//If it is the lunar calendar, the code is as follows:

10$date = $lunar->convertlunartosolar (2014,2,12); Lunar Calendar

11print_r ($date);

12?>

In fact, some of the functions can be used as a function, do not need to delete.

http://www.bkjia.com/PHPjc/755623.html www.bkjia.com true http://www.bkjia.com/PHPjc/755623.html techarticle This is a practical php date-time operation class, which includes the Gregorian-lunar conversion, converted into Chinese date format, calculate the number of days between the lunar calendar, according to the lunar calendar year to obtain the zodiac, get the lunar calendar ...

  • 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.