相思资源网 Design By www.200059.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

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

标签:
PHP,实现公历日期与农历日期,公历日期与农历日期PHP代码实现

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“PHP 实现公历日期与农历日期的互转换”

暂无PHP 实现公历日期与农历日期的互转换的评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?