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


Posted in PHP onSeptember 13, 2017

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

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

PHP 相关文章推荐
PHP+MySQL5.0中文乱码解决方法
Nov 20 PHP
9个PHP开发常用功能函数小结
Jul 15 PHP
PHP调用MsSQL Server 2012存储过程获取多结果集(包含output参数)的详解
Jul 03 PHP
php采用file_get_contents代替使用curl实例
Nov 07 PHP
php中静态类与静态变量用法的区别分析
Jan 15 PHP
PHP开发注意事项总结
Feb 04 PHP
php简单实现查询数据库返回json数据
Apr 16 PHP
PHP list() 将数组中的值赋给变量的简单实例
Jun 13 PHP
[原创]解决wincache不支持64位PHP5.5/5.6的问题(提供64位wincache下载)
Jun 22 PHP
php实用代码片段整理
Nov 12 PHP
利用PHP访问MySql数据库的逻辑操作以及增删改查的实例讲解
Aug 30 PHP
PHP实现通过二维数组键值获取一维键名操作示例
Oct 11 PHP
php生成条形码的图片的实例详解
Sep 13 #PHP
php之可变函数的实例详解
Sep 13 #PHP
PHP实现一个多功能购物网站的案例
Sep 13 #PHP
php之可变变量的实例详解
Sep 12 #PHP
PHP清除缓存的几种方法总结
Sep 12 #PHP
PHP 传输会话curl函数的实例详解
Sep 12 #PHP
基于ThinkPHP5.0实现图片上传插件
Sep 25 #PHP
You might like
基于PHP服务端图片生成缩略图的方法详解
2013/06/20 PHP
PHP实现适用于自定义的验证码类
2016/06/15 PHP
php7安装mongoDB扩展的方法分析
2017/08/02 PHP
php 广告点击统计代码(php+mysql)
2018/02/21 PHP
使用JS进行目录上传(相当于批量上传)
2010/12/05 Javascript
JS解决ie6下png透明的方法实例
2013/08/02 Javascript
快速解决jQuery与其他库冲突的方法介绍
2014/01/02 Javascript
PHPExcel中的一些常用方法汇总
2015/01/23 Javascript
超漂亮的Bootstrap 富文本编辑器summernote
2016/04/05 Javascript
深入浅析search 搜索框的写法
2016/08/02 Javascript
Angular2 自定义表单验证器的实现方法
2018/12/14 Javascript
简单两步使用node发送qq邮件的方法
2019/03/01 Javascript
微信小程序生成海报分享朋友圈的实现方法
2019/05/06 Javascript
基于Fixed定位的框选功能的实现代码
2019/05/13 Javascript
小程序server请求微信服务器超时的解决方法
2019/05/21 Javascript
vue实现页面切换滑动效果
2020/06/29 Javascript
如何在vue中使用HTML 5 拖放API
2021/01/14 Vue.js
[59:42]Secret vs Alliacne 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python使用socket进行简单网络连接的方法
2015/04/29 Python
讲解Python的Scrapy爬虫框架使用代理进行采集的方法
2016/02/18 Python
在阿里云服务器上配置CentOS+Nginx+Python+Flask环境
2016/06/18 Python
在django中图片上传的格式校验及大小方法
2019/07/28 Python
基于Django OneToOneField和ForeignKey的区别详解
2020/03/30 Python
python导入库的具体方法
2020/06/18 Python
python实现猜数游戏(保存游戏记录)
2020/06/22 Python
PyQt中使用QtSql连接MySql数据库的方法
2020/07/28 Python
python 实用工具状态机transitions
2020/11/21 Python
意大利在线眼镜精品店:Ottica Lipari
2019/11/11 全球购物
幼儿园清明节活动总结
2014/07/04 职场文书
工商管理本科生求职信
2014/07/13 职场文书
大学生考试作弊检讨书
2014/09/21 职场文书
夫妻婚内购房协议书
2014/10/05 职场文书
办公楼租房协议书范本
2014/11/25 职场文书
幼儿园小朋友毕业感言
2015/07/30 职场文书
银行求职信怎么写
2019/06/20 职场文书
html5 录制mp3音频支持采样率和比特率设置
2021/07/15 Javascript