php实现的农历算法实例

前端之家收集整理的这篇文章主要介绍了php实现的农历算法实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP实现的农历算法。分享给大家供大家参考。具体如下:

PHP;"> array(8,29,30,7,1),1 => array(0,8,2),2 => array(0,9,3),3 => array(5,10,4),4 => array(0,1,5),5 => array(0,2,6),6 => array(4,3,7),7 => array(0,4,8),8 => array(0,5,9),9 => array(2,6,10),10 => array(0,11),11 => array(6,12),12 => array(0,13 => array(0,14 => array(5,15 => array(0,16 => array(0,17 => array(2,18 => array(0,19 => array(7,20 => array(0,21 => array(0,22 => array(5,23 => array(0,24 => array(0,25 => array(4,26 => array(0,27 => array(0,28 => array(2,29 => array(0,30 => array(6,31 => array(0,32 => array(0,33 => array(5,34 => array(0,35 => array(0,36 => array(3,37 => array(0,38 => array(7,39 => array(0,40 => array(0,41 => array(6,42 => array(0,43 => array(0,44 => array(4,45 => array(0,46 => array(0,47 => array(2,48 => array(0,49 => array(7,50 => array(0,51 => array(0,52 => array(5,53 => array(0,54 => array(0,55 => array(3,56 => array(0,57 => array(8,58 => array(0,59 => array(0,60 => array(6,61 => array(0,62 => array(0,63 => array(4,64 => array(0,65 => array(0,66 => array(3,67 => array(0,68 => array(7,69 => array(0,70 => array(0,71 => array(5,72 => array(0,73 => array(0,74 => array(4,75 => array(0,76 => array(8,77 => array(0,78 => array(0,79 => array(6,80 => array(0,81 => array(0,82 => array(4,83 => array(0,84 => array(10,85 => array(0,86 => array(0,87 => array(6,88 => array(0,89 => array(0,90 => array(5,91 => array(0,92 => array(0,93 => array(3,94 => array(0,95 => array(8,96 => array(0,97 => array(0,98 => array(5,99 => array(0,100 => array(0,101 => array(4,102 => array(0,103 => array(0,104 => array(2,105 => array(0,106 => array(7,107 => array(0,108 => array(0,109 => array(5,110 => array(0,111 => array(0,112 => array(4,113 => array(0,114 => array(9,115 => array(0,116 => array(0,117 => array(6,118 => array(0,119 => array(0,120 => array(4,1) ); $mten = $lnlunarcalendar['tiangan'];// 农历天干 $mtwelve = $lnlunarcalendar['dizhi'];// 农历地支 $mmonth = $lnlunarcalendar['month'];// 农历月份 $mday = $lnlunarcalendar['day'];// 农历日 // 阳历总天数 至1900年12月21日 $total = 69 * 365 + 17 + 11; //1970年1月1日前的就不算了 if ($year == "" || $month == "" || ($year < 1970 or $year > 2020)) return ''; //超出这个范围不计算 // 计算到所求日期阳历的总天数-自1900年12月21日始 for ($y = 1970; $y < $year;$y++) {// 先算年的和 $total += 365; if ($y % 4 == 0) $total ++; } // 再加当年的几个月 $total += gmdate("z",gmmktime(0,$month,$year)); // 用农历的天数累加来判断是否超过阳历的天数 $flag1 = 0; //判断跳出循环的条件 $lcj = 0; while ($lcj <= 120) { $lci = 1; while ($lci <= 13) { @$mtotal += $everymonth[$lcj][$lci]; if ($mtotal >= $total) { $flag1 = 1; break; } $lci++; } if ($flag1 == 1) break; $lcj++; } // 由上,得到的 $lci 为当前农历月, $lcj 为当前农历年 // 计算所求月份1号的农历日期 $fisrtdaylunar = $everymonth[$lcj][$lci] - ($mtotal - $total); $results['year'] = $mten[$everymonth[$lcj][14]] . $mtwelve[$everymonth[$lcj][15]]; //当前是什么年 $daysthismonth = gmdate("t",$year)); //当前月共几天 $op = 1; for ($i = 1; $i <= $daysthismonth; $i++) { $possiblelunarday = $fisrtdaylunar + $op-1; //理论上叠加后的农历日 if ($possiblelunarday <= $everymonth[$lcj][$lci]) { // 在本月的天数范畴内 $results[$i] = $mday[$possiblelunarday]; $op += 1; } else { // 不在本月的天数范畴内 $results[$i] = $mday[1]; //退回到1日 $fisrtdaylunar = 1; $op = 2; $curmonthnum = ($everymonth[$lcj][0] != 0) ? 13 : 12; //当年有几个月 if ($lci + 1 > $curmonthnum) { // 第13/14个月了,转到下一年 $lci = 1; $lcj = $lcj + 1; // 换年头了,把新一年的天干地支也写上 $results['year'] .= '/' . $mten[$everymonth[$lcj][14]] . $mtwelve[$everymonth[$lcj][15]]; } else { // 还在这年里 $lci = $lci + 1; $lcj = $lcj; } } if ($results[$i] == $mday[1]) { // 每月的初一应该显示当月是什么月 if ($everymonth[$lcj][0] != 0) { // 有闰月的年 $monthss = ($lci > $everymonth[$lcj][0]) ? ($lci-1) : $lci; //闰月后的月数-1 if ($lci == $everymonth[$lcj][0] + 1) { // 这个月正好是闰月 $monthssshow = $mmonth[0] . $mmonth[$monthss]; //前面加个闰字 $runyue = 1; } else { $monthssshow = $mmonth[$monthss]; } } else { $monthss = $lci; $monthssshow = $mmonth[$monthss]; } if ($monthss <= 10 && @$runyue != 1){ //只有1个字的月加上‘月'字 $monthssshow .= $mmonth[13]; } $results[$i] = $monthssshow; } } return $results; } // 忘了加上这个:农历用字 $lnlunarcalendar = array( 'tiangan' => array("未知","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"),'dizhi' => array("未知","子年(鼠)","丑年(牛)","寅年(虎)","卯年(兔)","辰年(龙)","巳年(蛇)","午年(马)","未年(羊)","申年(猴)","酉年(鸡)","戌年(狗)","亥年(猪)"),'month' => array("闰","正","二","三","四","五","六","七","八","九","十","十一","十二","月"),'day' => array("未知","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十","十三","十四","十五","十六","十七","十八","十九","二十","廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十") ); print_r(lunarcalendar(8,2015));

运行结果如下:

乙未年(羊) [1] => 十七 [2] => 十八 [3] => 十九 [4] => 二十 [5] => 廿一 [6] => 廿二 [7] => 廿三 [8] => 廿四 [9] => 廿五 [10] => 廿六 [11] => 廿七 [12] => 廿八 [13] => 廿九 [14] => 七月 [15] => 初二 [16] => 初三 [17] => 初四 [18] => 初五 [19] => 初六 [20] => 初七 [21] => 初八 [22] => 初九 [23] => 初十 [24] => 十一 [25] => 十二 [26] => 十三 [27] => 十四 [28] => 十五 [29] => 十六 [30] => 十七 [31] => 十八 )

希望本文所述对大家的PHP程序设计有所帮助。

原文链接:https://www.f2er.com/php/21295.html

猜你在找的PHP相关文章