本文实例为大家分享了php阳历转农历代码,供大家参考,具体内容如下

<"null","甲","乙","丙","丁","戊","己","庚","辛","壬","癸"); 
 #农历地支 
 $mtwelve=array("null","子(鼠)","丑(牛)","寅(虎)","卯(兔)","辰(龙)", 
         "巳(蛇)","午(马)","未(羊)","申(猴)","酉(鸡)","戌(狗)","亥(猪)"); 
 #农历月份 
 $mmonth=array("闰","正","二","三","四","五","六", 
        "七","八","九","十","十一","十二","月"); 
 #农历日 
 $mday=array("null","初一","初二","初三","初四","初五","初六","初七","初八","初九","初十", 
       "十一","十二","十三","十四","十五","十六","十七","十八","十九","二十", 
       "廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"); 
############################## 
 #星期 
 $weekday = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); 
 #阳历总天数 至1900年12月21日 
 $total=11; 
 #阴历总天数 
 $mtotal=0; 
############################## 
 #获得当日日期 
 //$today=getdate(); //获取今天的日期
 if($nian<1901 || $nian>2020) die("年份出错!"); 
 //$cur_wday=$today["wday"]; //星期中第几天的数字表示
 for($y=1901;$y<$nian;$y++) { //计算到所求日期阳历的总天数-自1900年12月21日始,先算年的和 
    $total+=365; 
    if ($y%4==0) $total++; 
 } 
 switch($yue) { //再加当年的几个月 
     case 12: 
       $total+=30; 
     case 11: 
       $total+=31; 
     case 10: 
       $total+=30; 
     case 9: 
       $total+=31; 
     case 8: 
       $total+=31; 
     case 7: 
       $total+=30; 
     case 6: 
       $total+=31; 
     case 5: 
       $total+=30; 
     case 4: 
       $total+=31; 
     case 3: 
       $total+=28; 
     case 2: 
       $total+=31; 
 } 
 if($nian%4 == 0 && $yue>2) $total++; //如果当年是闰年还要加一天 
 $total=$total+$ri-1; //加当月的天数 
 $flag1=0; //判断跳出循环的条件 
 $j=0; 
 while ($j<=120){ //用农历的天数累加来判断是否超过阳历的天数 
   $i=1; 
   while ($i<=13){ 
      $mtotal+=$everymonth[$j][$i]; 
      if ($mtotal>=$total){ 
         $flag1=1; 
         break; 
      } 
      $i++; 
   } 
   if ($flag1==1) break; 
   $j++; 
 } 
 if($everymonth[$j][0]<>0 and $everymonth[$j][0]<$i){ //原来错在这里,对闰月没有修补 
   $mm=$i-1; 
 } 
 else{ 
   $mm=$i; 
 } 
 if($i==$everymonth[$j][0]+1 and $everymonth[$j][0]<>0) { 
   $nlmon=$mmonth[0].$mmonth[$mm];#闰月 
 } 
 else { 
   $nlmon=$mmonth[$mm].$mmonth[13]; 
 } 
 #计算所求月份1号的农历日期 
 $md=$everymonth[$j][$i]-($mtotal-$total); 
 if($md > $everymonth[$j][$i]) 
   $md-=$everymonth[$j][$i]; 
 $nlday=$mday[$md]; 
  
 //$nowday=date("Y年n月j日 ")."w".$weekday[$cur_wday]." ".$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年".$nlmon.$nlday; 
 $nowday=$mten[$everymonth[$j][14]].$mtwelve[$everymonth[$j][15]]."年 ".$nlmon.$nlday; 
 return $nowday;
}
echo nongli('20160807'); //标准8位

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com