PHP 实现公历日期与农历日期的互转换
前言:
今天根据客户的需求对时间进行了转换,就是客户要求增加农历日期的显示,在网上抄袭了一段,稍微修改了一下运行成功了,不难的,改动的很少的.
示例代码:
<"零","一","二","三","四","五","六","七","八","九"); 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; } } "htmlcode">//公历转农历 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如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“PHP 实现公历日期与农历日期的互转换”评论...