php阳历转农历优化版

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

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

PHP;"> function nongli($riqi)
{
//优化修改 20160807 FXL
$nian=date('Y',strtotime($riqi));
$yue=date('m',strtotime($riqi));
$ri=date('d',strtotime($riqi));

源码部分原作者:沈潋(S&S Lab)

农历每月的天数

$everymonth=array(
0=>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=array("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位

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

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

猜你在找的PHP相关文章