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.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。