无为清净楼资源网 Design By www.qnjia.com
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如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 【雨果唱片】中国管弦乐《鹿回头》WAV
- APM亚流新世代《一起冒险》[FLAC/分轨][106.77MB]
- 崔健《飞狗》律冻文化[WAV+CUE][1.1G]
- 罗志祥《舞状元 (Explicit)》[320K/MP3][66.77MB]
- 尤雅.1997-幽雅精粹2CD【南方】【WAV+CUE】
- 张惠妹.2007-STAR(引进版)【EMI百代】【WAV+CUE】
- 群星.2008-LOVE情歌集VOL.8【正东】【WAV+CUE】
- 罗志祥《舞状元 (Explicit)》[FLAC/分轨][360.76MB]
- Tank《我不伟大,至少我能改变我。》[320K/MP3][160.41MB]
- Tank《我不伟大,至少我能改变我。》[FLAC/分轨][236.89MB]
- CD圣经推荐-夏韶声《谙2》SACD-ISO
- 钟镇涛-《百分百钟镇涛》首批限量版SACD-ISO
- 群星《继续微笑致敬许冠杰》[低速原抓WAV+CUE]
- 潘秀琼.2003-国语难忘金曲珍藏集【皇星全音】【WAV+CUE】
- 林东松.1997-2039玫瑰事件【宝丽金】【WAV+CUE】