PHP實現西曆與農曆間的互轉換方法介紹

來源:互聯網
上載者:User
這篇文章主要介紹了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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.