当前位置:首页 > 日记 > 正文

PHP 实现公历日期与农历日期的互转换

PHP 实现公历日期与农历日期的互转换

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

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

一键系统重装要怎么操作?一键系统重

一键系统重装要怎么操作?一键系统重

系统重装,图文教程,操作,一键,电脑软件,  小编发现有好多朋友都不知道一键系统重装要怎么操作,其实一键重装系统的方法还是蛮简单的,如果大家想要学习一键重装系统的方法,小编可以教大家!下面就给大家带来一键系统重装图文教程,希望大家能够…

利用canvas中toDataURL | 将转为da

利用canvas中toDataURL | 将转为da

方法,详解,电脑软件,toDataURL,canvas,将图片转为base64的好处将图片转换为Base64编码,可以让你很方便地在没有上传文件的条件下将图片插入其它的网页、编辑器中。 这对于一些小的图片是极为方便的,因为你不需要再去寻找一个保存图片的地方。…

Windows 2003邮件服务器配置指南

Windows 2003邮件服务器配置指南

服务器配置,邮件,指南,电脑软件,Windows,邮件服务器的配置同样是企业网络管理中经常要进行的任务之一。与Web网站、FTP站点服务器一样,邮件服务器的配置方案也非常之多,但对于中小型企业说,利用网络操作系统自带的方式进行配置是最经济的。本…

IIS6架设网站常见问题及症状答疑

IIS6架设网站常见问题及症状答疑

网站,症状,常见问题,电脑软件, 很多朋友在用IIS6架网站的时候遇到不少问题,而这些问题有些在过去的IIS5里面就遇到过,有些是新出来的,做了很多次试验,结合以前的排错经验,做出了这个总结,希望能给大家帮上忙。 问题1:未启用父路径 症状举例: S…

完美解决Thinkphp3.2中插入相同数

完美解决Thinkphp3.2中插入相同数

数据,完美,电脑软件,问题描述今天在使用TP3.2插入数据的时候,为了避免插入相同的数据(所谓相同的数据,其主键相同或者是唯一索引的字段相同),我创建的索引如下图,主键索引为自增字段,不可能出现重复,即唯一索引可能会出现重复,我希望的是uid,year,mo…

红帽Linux忘记root密码的解决方法

红帽Linux忘记root密码的解决方法

解决方法,密码,红帽,电脑软件,root,在出现以下画面后按下键盘e键 (图1) 按下后会出现如下画面,在次按下e键,将会出现如图3所示的画面(图2)如图3所示,使用上下键将焦点移动到第2项,如果出现的和本图不一样,就找到有kerner字样的那一行,然后再次按下E键…

PS怎么设计珍珠字效果的字体?

PS怎么设计珍珠字效果的字体?

字体,效果,电脑软件,PS,用PS制作珍珠字的效果,做好之后图片看上去就像是用珍珠摆成的字一样。软件名称:Adobe Photoshop 8.0 中文完整绿色破解版软件大小:150.1MB更新时间:2015-11-041、建立一个100*100的图象文件,用椭圆形选框工具画一个固定大…

angular中实现li或者某个元素点击

angular中实现li或者某个元素点击

方法,元素,两种,电脑软件,angular,本文介绍了angular中实现li或者某个元素点击变色的两种方法,分享给大家,希望对大家有帮助先说一种最直接了当的不需要js控制。方法一:直接在用ng-class就可以控制:<p ng-click="state=1;" ng-class="{activ…

ps怎么设计一个圆形的旅游图标?

ps怎么设计一个圆形的旅游图标?

图标,电脑软件,ps,ps中想要设计一个圆形的旅游图标,该怎么设计呢?下面我们就来看看详细的教程。软件名称:Adobe Photoshop 8.0 中文完整绿色破解版软件大小:150.1MB更新时间:2015-11-041、我们设计整个旅游图标为一个圆形的结构,先从天空开始设计…

javascript中神奇的 Date对象小结

javascript中神奇的 Date对象小结

对象,神奇,电脑软件,javascript,Date,Date 对象算是较常用的对象之一,但很多人完全不会操作,就算一些简单的操作也用 moment 而不自己尝试一下。本次分享下 Date 中的 date 使用技巧,希望能给大家启发。MDN官网介绍setDate() 方法根据本地时间…

PHP基于递归实现的约瑟夫环算法示

PHP基于递归实现的约瑟夫环算法示

算法示例,递归实现,约瑟夫,电脑软件,PHP,本文实例讲述了PHP基于递归实现的约瑟夫环算法。分享给大家供大家参考,具体如下:约瑟夫环问题: 39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓。于是决定了自杀方式…

excel输入带圈数字的方法excel怎么

excel输入带圈数字的方法excel怎么

输入带,数字,方法,电脑软件,excel,  在Excel中录入数据的时候经常需要输入一些带圈的数字,这些数字不是单纯的靠输入法就可以输入的,是要利用Excel中的功能。接下来请欣赏小编给大家网络收集整理的excel输入带圈数字的方法。excel输入带圈…