php 实现公历日期与农历日期的互转换
前言:
今天根据客户的需求对时间进行了转换,就是客户要求增加农历日期的显示,在网上抄袭了一段,稍微修改了一下运行成功了,不难的,改动的很少的.
示例代码:
<?php
/*
*
* 陈海波 新二级页面 农历转换成公历
*
* */
// lunar.php 新建这个php文件,所有的代码无需改,需要改的是下面teacher.php 的几行
class lunar
{
private $_smday = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);//定义公历月分天数
private $_lstart = 1950 ;//农历从1950年开始
private $_lmday = array(
//差:该年的农历正月初一到该年公历1月1日的天数;1~12:农历月份天数;闰:如有闰月,记录该月平月天数
// 差 1 2 3 4 5 6 7 8 9 10 11 12 闰
array(47,29,30,30,29,30,30,29,29,30,29,30,29),
array(36,30,29,30,30,29,30,29,30,29,30,29,30),
array(6,29,30,29,30,59,29,30,30,29,30,29,30,29), //五月29 闰五月30
array(44,29,30,29,29,30,30,29,30,30,29,30,29),
array(33,30,29,30,29,29,30,29,30,30,29,30,30),
array(23,29,30,59,29,29,30,29,30,29,30,30,30,29), //三月29 闰三月30
array(42,29,30,29,30,29,29,30,29,30,29,30,30),
array(30,30,29,30,29,30,29,29,59,30,29,30,29,30), //八月30 闰八月29
array(48,30,30,30,29,30,29,29,30,29,30,29,30),
array(38,29,30,30,29,30,29,30,29,30,29,30,29),
array(27,30,29,30,29,30,59,30,29,30,29,30,29,30), //六月30 闰六月29
array(45,30,29,30,29,30,29,30,30,29,30,29,30),
array(35,29,30,29,29,30,29,30,30,29,30,30,29),
array(24,30,29,30,58,30,29,30,29,30,30,30,29,29), //四月29 闰四月29
array(43,30,29,30,29,29,30,29,30,29,30,30,30),
array(32,29,30,29,30,29,29,30,29,29,30,30,29),
array(20,30,30,59,30,29,29,30,29,29,30,30,29,30), //三月30 闰三月29
array(39,30,30,29,30,30,29,29,30,29,30,29,30),
array(29,29,30,29,30,30,29,59,30,29,30,29,30,30), //七月30 闰七月29
array(47,29,30,29,30,29,30,30,29,30,29,30,29),
array(36,30,29,29,30,29,30,30,29,30,30,29,30),
array(26,29,30,29,29,59,30,29,30,30,30,29,30,30), //五月30 闰五月29
array(45,29,30,29,29,30,29,30,29,30,30,29,30),
array(33,30,29,30,29,29,30,29,29,30,30,29,30),
array(22,30,30,29,59,29,30,29,29,30,30,29,30,30), //四月30 闰四月29
array(41,30,30,29,30,29,29,30,29,29,30,29,30),
array(30,30,30,29,30,29,30,29,59,29,30,29,30,30), //八月30 闰八月29
array(48,30,29,30,30,29,30,29,30,29,30,29,29),
array(37,30,29,30,30,29,30,30,29,30,29,30,29),
array(27,30,29,29,30,29,60,29,30,30,29,30,29,30), //六月30 闰六月30
array(46,30,29,29,30,29,30,29,30,30,29,30,30),
array(35,29,30,29,29,30,29,29,30,30,29,30,30),
array(24,30,29,30,58,30,29,29,30,29,30,30,30,29), //四月29 闰四月29
array(43,30,29,30,29,29,30,29,29,30,29,30,30),
array(32,30,29,30,30,29,29,30,29,29,59,30,30,30), //十月30 闰十月29
array(50,29,30,30,29,30,29,30,29,29,30,29,30),
array(39,29,30,30,29,30,30,29,30,29,30,29,29),
array(28,30,29,30,29,30,59,30,30,29,30,29,29,30), //六月30 闰六月29
array(47,30,29,30,29,30,29,30,30,29,30,30,29),
array(36,30,29,29,30,29,30,29,30,29,30,30,30),
array(26,29,30,29,29,59,29,30,29,30,30,30,30,30), //五月30 闰五月29
array(45,29,30,29,29,30,29,29,30,29,30,30,30),
array(34,29,30,30,29,29,30,29,29,30,29,30,30),
array(22,29,30,59,30,29,30,29,29,30,29,30,29,30), //三月30 闰三月29
array(40,30,30,30,29,30,29,30,29,29,30,29,30),
array(30,29,30,30,29,30,29,30,59,29,30,29,30,30), //八月30 闰八月29
array(49,29,30,29,30,30,29,30,29,30,30,29,29),
array(37,30,29,30,29,30,29,30,30,29,30,30,29),
array(27,30,29,29,30,58,30,30,29,30,30,29,30,29), //五月29 闰五月29
array(46,30,29,29,30,29,29,30,29,30,30,30,29),
array(35,30,30,29,29,30,29,29,30,29,30,30,29),
array(23,30,30,29,59,30,29,29,30,29,30,29,30,30), //四月30 闰四月29
array(42,30,30,29,30,29,30,29,29,30,29,30,29),
array(31,30,30,29,30,30,29,30,29,29,30,29,30),
array(21,29,59,30,30,29,30,29,30,29,30,29,30,30), //二月30 闰二月29
array(39,29,30,29,30,29,30,30,29,30,29,30,29),
array(28,30,29,30,29,30,29,59,30,30,29,30,30,30), //七月30 闰七月29
array(48,29,29,30,29,29,30,29,30,30,30,29,30),
array(37,30,29,29,30,29,29,30,29,30,30,29,30),
array(25,30,30,29,29,59,29,30,29,30,29,30,30,30), //五月30 闰五月29
array(44,30,29,30,29,30,29,29,30,29,30,29,30),
array(33,30,29,30,30,29,30,29,29,30,29,30,29),
array(22,30,29,30,59,30,29,30,29,30,29,30,29,30), //四月30 闰四月29
array(40,30,29,30,29,30,30,29,30,29,30,29,30),
array(30,29,30,29,30,29,30,29,30,59,30,29,30,30), //九月30 闰九月29
array(49,29,30,29,29,30,29,30,30,30,29,30,29),
array(38,30,29,30,29,29,30,29,30,30,29,30,30),
array(27,29,30,29,30,29,59,29,30,29,30,30,30,29), //六月29 闰六月30
array(46,29,30,29,30,29,29,30,29,30,29,30,30),
array(35,30,29,30,29,30,29,29,30,29,29,30,30),
array(24,29,30,30,59,30,29,29,30,29,30,29,30,30), //四月30 闰四月29
array(42,29,30,30,29,30,29,30,29,30,29,30,29),
array(31,30,29,30,29,30,30,29,30,29,30,29,30),
array(21,29,59,29,30,30,29,30,30,29,30,29,30,30), //二月30 闰二月29
array(40,29,30,29,29,30,29,30,30,29,30,30,29),
array(28,30,29,30,29,29,59,30,29,30,30,30,29,30), //六月30 闰六月29
array(47,30,29,30,29,29,30,29,29,30,30,30,29),
array(36,30,30,29,30,29,29,30,29,29,30,30,29),
array(25,30,30,30,29,59,29,30,29,29,30,30,29,30), //五月30 闰五月29
array(43,30,30,29,30,29,30,29,30,29,29,30,30),
array(33,29,30,29,30,30,29,30,29,30,29,30,29),
array(22,29,30,59,30,29,30,30,29,30,29,30,29,30), //三月30 闰三月29
array(41,30,29,29,30,29,30,30,29,30,30,29,30),
array(30,29,30,29,29,30,29,30,29,30,30,59,30,30), //十一月30 闰十一月29
array(49,29,30,29,29,30,29,30,29,30,30,29,30),
array(38,30,29,30,29,29,30,29,29,30,30,29,30),
array(27,30,30,29,30,29,59,29,29,30,29,30,30,29), //六月29 闰六月30
array(45,30,30,29,30,29,29,30,29,29,30,29,30),
array(34,30,30,29,30,29,30,29,30,29,29,30,29),
array(23,30,30,29,30,59,30,29,30,29,30,29,29,30), //五月30 闰五月29
array(42,30,29,30,30,29,30,29,30,30,29,30,29),
array(31,29,30,29,30,29,30,30,29,30,30,29,30),
array(21,29,59,29,30,29,30,29,30,30,29,30,30,30), //二月30 闰二月29
array(40,29,30,29,29,30,29,29,30,30,29,30,30),
array(29,30,29,30,29,29,30,58,30,29,30,30,30,29), //七月29 闰七月29
array(47,30,29,30,29,29,30,29,29,30,29,30,30),
array(36,30,29,30,29,30,29,30,29,29,30,29,30),
array(25,30,29,30,30,59,29,30,29,29,30,29,30,29), //五月29 闰五月30
array(44,29,30,30,29,30,30,29,30,29,29,30,29),
array(32,30,29,30,29,30,30,29,30,30,29,30,29),
array(22,29,30,59,29,30,29,30,30,29,30,30,29,29), //三月29 闰三月30
);
//是否闰年
private function isleapyear($ayear)
{
return ($ayear % 4 == 0) && (($ayear % 100 != 0) || ($ayear % 400 == 0));
}
//公历该月的天数(year:年份; month:月份)
private function getsmon($year,$month)
{
if($this->isleapyear($year) && $month == 2)
return 29;
else
return $this->_smday[$month];
}
//农历名称转换
private function lyearname($year)
{
$name = array("零","一","二","三","四","五","六","七","八","九");
for($i=0;$i<4;$i++)
for($k=0;$k<10;$k++)
if($year[$i]==$k)
$tmp.=$name[$k];
return $tmp;
}
private function lmonname($month)
{
if($month >=1 && $month <=12 )
{
$name = array( 1=>"正","二","三","四","五","六","七","八","九","十","十一","十二");
return $name[$month];
}
return $month;
}
private function ldayname($day)
{
if($day >=1 && $day <=30 )
{
$name = array( 1 =>
"初一","初二","初三","初四","初五","初六","初七","初八","初九","初十",
"十一","十二","十三","十四","十五","十六","十七","十八","十九","二十",
"廿一","廿二","廿三","廿四","廿五","廿六","廿七","廿八","廿九","三十"
);
return $name[$day];
}
return $day;
}
//公历转农历(sdate:公历日期)
public function s2l($date)
{
list($year, $month, $day) = explode("-", $date);
if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
//获取查询日期到当年1月1日的天数
$date1 = strtotime($year."-01-01");//当年1月1日
$date2 = strtotime($year."-".$month."-".$day);
$days=round(($date2-$date1)/3600/24);
$days += 1;
//获取相应年度农历数据,化成数组larray
$larray = $this->_lmday[$year - $this->_lstart];
if($days <= $larray[0])
{
$lyear = $year - 1;
$days = $larray[0] - $days;
$larray = $this->_lmday[$lyear - $this->_lstart];
if($days < $larray[12])
{
$lmonth = 12;
$lday = $larray[12] - $days;
}
else
{
$lmonth = 11;
$days = $days - $larray[12];
$lday = $larray[11] - $days;
}
}
else
{
$lyear = $year;
$days = $days - $larray[0];
for($i = 1;$i <= 12;$i++)
{
if($days > $larray[$i]) $days = $days - $larray[$i];
else
{
if ($days > 30){
$days = $days - $larray[13];
$ltype = 1;
}
$lmonth = $i;
$lday = $days;
break;
}
}
}
return mktime(0, 0, 0, $lmonth, $lday, $lyear);
//$ldate = $lyear."-".$lmonth."-".$lday;
//$ldate = $this->lyearname($lyear)."年".$this->lmonname($lmonth)."月".$this->ldayname($lday);
//if($ltype) $ldate.="(闰)";
//return $ldate;
}
//农历转公历(date:农历日期; type:是否闰月)
public function l2s($date,$type = 0)
{
list($year, $month, $day) = split("-",$date);
if($year <= 1951 || $month <= 0 || $day <= 0 || $year >= 2051 ) return false;
$larray = $this->_lmday[$year - $this->_lstart];
if($type == 1 && count($larray)<=12 ) return false;//要求查询闰,但查无闰月
//如果查询的农历是闰月并该年度农历数组存在闰月数据就获取
if($larray[$month]>30 && $type == 1 && count($larray) >=13) $day = $larray[13] + $day;
//获取该年农历日期到公历1月1日的天数
$days = $day;
for($i=0;$i<=$month-1;$i++)
$days += $larray[$i];
//当查询农历日期距离公历1月1日超过一年时
if($days > 366 || ($this->getsmon($month,2)!=29 && $days>365 ))
{
$syear = $year +1;
if($this->getsmon($month,2)!=29)
$days-=366;
else
$days-=365;
if($days > $this->_smday[1])
{
$smonth = 2;
$sday = $days - $this->_smday[1];
}
else
{
$smonth = 1;
$sday = $days;
}
}
else
{
$syear =$year;
for($i=1;$i<=12;$i++)
{
if($days > $this->getsmon($syear,$i))
$days-=$this->getsmon($syear,$i);
else
{
$smonth = $i;
$sday = $days;
break;
}
}
}
return mktime(0, 0, 0, $smonth, $sday, $syear);
//$sdate = $syear."-".$smonth."-".$sday;
//return $sdate;
}
}
?>
teacher.php
//公历转农历
require_once(core_."lunar.php"); // 加载lunar.php文件
$today = date("y-m-d");
$lunar = new lunar(); // 实例化类
$nonglitime = array();
$nl = date("y-n-d",$lunar->s2l($today));
echo $nl;exit; // 转为农历是:2012-05-25
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
评论列表:
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 3天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论
发布于 2天前回复该评论