Home Backend Development PHP Tutorial Introduction to the method of converting between the Gregorian calendar and the lunar calendar in PHP

Introduction to the method of converting between the Gregorian calendar and the lunar calendar in PHP

Sep 15, 2017 am 09:42 AM
php Convert

This article mainly introduces the relevant information on PHP to realize the conversion between Gregorian calendar dates and lunar calendar dates. I hope that this article can help everyone. Friends in need can refer to the following

PHP realizes the Gregorian calendar Conversion between dates and lunar calendar dates

Foreword:

Today, the time was converted according to the customer's needs, that is, the customer requested to increase the display of the lunar calendar date. I plagiarized a paragraph from the Internet, modified it a little and it ran successfully. It is not difficult and has very few changes.

Sample code:


<?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; 
   } 
 } 
?>
Copy after login

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
Copy after login

The above is the detailed content of Introduction to the method of converting between the Gregorian calendar and the lunar calendar in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles