search
HomeBackend DevelopmentPHP TutorialPHP Lunar Calendar Gregorian Calendar Conversion

PHP Lunar Calendar Gregorian Calendar Conversion
  1. /*
  2. Written by Yang Haixi, School of Computer Science and Engineering, Qujing Normal University, Yunnan Province
  3. 2009-9-3
  4. */
  5. class Lunar
  6. {
  7. private $_SMDay = array(1 => 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);//Define the number of days in the Gregorian calendar month
  8. private $_LStart = 1950;//The lunar calendar starts in 1950
  9. private $_LMDay = array(
  10. //Difference: the number of days from the first day of the first lunar month of the year to January 1 of the Gregorian calendar; 1~12: the number of days in the lunar month; leap: if there is a leap month, record the number of ordinary days in the month
  11. //Difference 1 2 3 4 5 6 7 8 9 10 11 12 leap
  12. array(47,29,30,30,29,30,30,29,29,30,29,30,29),
  13. array(36,30, 29,30,30,29,30,29,30,29,30,29,30),
  14. array(6,29,30,29,30,59,29,30,30,29,30,29, 30,29), //May 29 Leap May 30
  15. array(44,29,30,29,29,30,30,29,30,30,29,30,29),
  16. array(33,30 ,29,30,29,29,30,29,30,30,29,30,30),
  17. array(23,29,30,59,29,29,30,29,30,29,30,30 ,30,29), //March 29 Leap March 30
  18. array(42,29,30,29,30,29,29,30,29,30,29,30,30),
  19. array(30, 30,29,30,29,30,29,29,59,30,29,30,29,30), //August 30 Leap August 29
  20. array(48,30,30,30,29,30 ,29,29,30,29,30,29,30),
  21. array(38,29,30,30,29,30,29,30,29,30,29,30,29),
  22. array(27 ,30,29,30,29,30,59,30,29,30,29,30,29,30), //June 30 Leap June 29
  23. array(45,30,29,30,29, 30,29,30,30,29,30,29,30),
  24. array(35,29,30,29,29,30,29,30,30,29,30,30,29),
  25. array( 24,30,29,30,58,30,29,30,29,30,30,30,29,29), //April 29 Leap April 29
  26. array(43,30,29,30,29 ,29,30,29,30,29,30,30,30),
  27. array(32,29,30,29,30,29,29,30,29,29,30,30,29),
  28. array (20,30,30,59,30,29,29,30,29,29,30,30,29,30), //March 30 Leap March 29
  29. array(39,30,30,29, 30,30,29,29,30,29,30,29,30),
  30. array(29,29,30,29,30,30,29,59,30,29,30,29,30,30) , //July 30 Leap July 29
  31. array(47,29,30,29,30,29,30,30,29,30,29,30,29),
  32. array(36,30,29,29 ,30,29,30,30,29,30,30,29,30),
  33. array(26,29,30,29,29,59,30,29,30,30,30,29,30,30 ), //May 30 Leap May 29
  34. array(45,29,30,29,29,30,29,30,29,30,30,29,30),
  35. array(33,30,29, 30,29,29,30,29,29,30,30,29,30),
  36. array(22,30,30,29,59,29,30,29,29,30,30,29,30, 30), //April 30 Leap April 29
  37. array(41,30,30,29,30,29,29,30,29,29,30,29,30),
  38. array(30,30,30 ,29,30,29,30,29,59,29,30,29,30,30), //August 30 Leap August 29
  39. array(48,30,29,30,30,29,30, 29,30,29,30,29,29),
  40. array(37,30,29,30,30,29,30,30,29,30,29,30,29),
  41. array(27,30, 29,29,30,29,60,29,30,30,29,30,29,30), //June 30 Leap June 30
  42. array(46,30,29,29,30,29,30 ,29,30,30,29,30,30),
  43. array(35,29,30,29,29,30,29,29,30,30,29,30,30),
  44. array(24,30 ,29,30,58,30,29,29,30,29,30,30,30,29), //April 29 Leap April 29
  45. array(43,30,29,30,29,29, 30,29,29,30,29,30,30),
  46. array(32,30,29,30,30,29,29,30,29,29,59,30,30,30), //ten Month 30 Leap October 29
  47. array(50,29,30,30,29,30,29,30,29,29,30,29,30),
  48. array(39,29,30,30,29,30 ,30,29,30,29,30,29,29),
  49. array(28,30,29,30,29,30,59,30,30,29,30,29,29,30), // June 30 Leap June 29
  50. array(47,30,29,30,29,30,29,30,30,29,30,30,29),
  51. array(36,30,29,29,30, 29,30,29,30,29,30,30,30),
  52. array(26,29,30,29,29,59,29,30,29,30,30,30,30,30), / /May 30 Leap May 29
  53. array(45,29,30,29,29,30,29,29,30,29,30,30,30),
  54. array(34,29,30,30,29 ,29,30,29,29,30,29,30,30),
  55. array(22,29,30,59,30,29,30,29,29,30,29,30,29,30), //March 30 Leap March 29
  56. array(40,30,30,30,29,30,29,30,29,29,30,29,30),
  57. array(30,29,30,30, 29,30,29,30,59,29,30,29,30,30), //August 30 Leap August 29
  58. array(49,29,30,29,30,30,29,30,29 ,30,30,29,29),
  59. array(37,30,29,30,29,30,29,30,30,29,30,30,29),
  60. array(27,30,29,29 ,30,58,30,30,29,30,30,29,30,29), //May 29 Leap May 29
  61. array(46,30,29,29,30,29,29,30, 29,30,30,30,29),
  62. array(35,30,30,29,29,30,29,29,30,29,30,30,29),
  63. array(23,30,30, 29,59,30,29,29,30,29,30,29,30,30), //April 30 Leap April 29
  64. array(42,30,30,29,30,29,30,29 ,29,30,29,30,29),
  65. array(31,30,30,29,30,30,29,30,29,29,30,29,30),
  66. array(21,29,59 ,30,30,29,30,29,30,29,30,29,30,30), //February 30 Leap February 29
  67. array(39,29,30,29,30,29,30, 30,29,30,29,30,29),
  68. array(28,30,29,30,29,30,29,59,30,30,29,30,30,30), //July 30 Leap July 29
  69. array(48,29,29,30,29,29,30,29,30,30,30,29,30),
  70. array(37,30,29,29,30,29,29 ,30,29,30,30,29,30),
  71. array(25,30,30,29,29,59,29,30,29,30,29,30,30,30), //May 30 Leap May 29
  72. array(44,30,29,30,29,30,29,29,30,29,30,29,30),
  73. array(33,30,29,30,30,29, 30,29,29,30,29,30,29),
  74. array(22,30,29,30,59,30,29,30,29,30,29,30,29,30), //four Month 30 Leap April 29
  75. array(40,30,29,30,29,30,30,29,30,29,30,29,30),
  76. array(30,29,30,29,30,29 ,30,29,30,59,30,29,30,30), //September 30 Leap September 29
  77. array(49,29,30,29,29,30,29,30,30,30,29,30,29),
  78. array(38,30,29,30,29,29,30,29,30, 30,29,30,30),
  79. array(27,29,30,29,30,29,59,29,30,29,30,30,30,29), //June 29 Leap June 30
  80. array(46,29,30,29,30,29,29,30,29,30,29,30,30),
  81. array(35,30,29,30,29,30,29,29,30 ,29,29,30,30),
  82. array(24,29,30,30,59,30,29,29,30,29,30,29,30,30), //April 30 Leap April 29
  83. array(42,29,30,30,29,30,29,30,29,30,29,30,29),
  84. array(31,30,29,30,29,30,30,29, 30,29,30,29,30),
  85. array(21,29,59,29,30,30,29,30,30,29,30,29,30,30), //February 30 leap day Month 29
  86. array(40,29,30,29,29,30,29,30,30,29,30,30,29),
  87. array(28,30,29,30,29,29,59,30 ,29,30,30,30,29,30), //June 30 Leap June 29
  88. array(47,30,29,30,29,29,30,29,29,30,30,30, 29),
  89. array(36,30,30,29,30,29,29,30,29,29,30,30,29),
  90. array(25,30,30,30,29,59,29, 30,29,29,30,30,29,30), //May 30 Leap May 29
  91. array(43,30,30,29,30,29,30,29,30,29,29,30 ,30),
  92. array(33,29,30,29,30,30,29,30,29,30,29,30,29),
  93. array(22,29,30,59,30,29,30 ,30,29,30,29,30,29,30), //March 30 Leap March 29
  94. array(41,30,29,29,30,29,30,30,29,30,30, 29,30),
  95. array(30,29,30,29,29,30,29,30,29,30,30,59,30,30), //November 30 Leap November 29
  96. array (49,29,30,29,29,30,29,30,29,30,30,29,30),
  97. array(38,30,29,30,29,29,30,29,29,30 ,30,29,30),
  98. array(27,30,30,29,30,29,59,29,29,30,29,30,30,29), //June 29 Leap June 30
  99. array(45,30,30,29,30,29,29,30,29,29,30,29,30),
  100. array(34,30,30,29,30,29,30,29,30, 29,29,30,29),
  101. array(23,30,30,29,30,59,30,29,30,29,30,29,29,30), //May 30 Leap May 29
  102. array(42,30,29,30,30,29,30,29,30,30,29,30,29),
  103. array(31,29,30,29,30,29,30,30,29 ,30,30,29,30),
  104. array(21,29,59,29,30,29,30,29,30,30,29,30,30,30), //February 30 Leap February 29
  105. array(40,29,30,29,29,30,29,29,30,30,29,30,30),
  106. array(29,30,29,30,29,29,30,58, 30,29,30,30,30,29), //July 29 Leap July 29
  107. array(47,30,29,30,29,29,30,29,29,30,29,30,30 ),
  108. array(36,30,29,30,29,30,29,30,29,29,30,29,30),
  109. array(25,30,29,30,30,59,29,30 ,29,29,30,29,30,29), //May 29 Leap May 30
  110. array(44,29,30,30,29,30,30,29,30,29,29,30, 29),
  111. array(32,30,29,30,29,30,30,29,30,30,29,30,29),
  112. array(22,29,30,59,29,30,29, 30,30,29,30,30,29,29), //March 29 Leap March 30
  113. );
  114. //Whether it is a leap year
  115. private function IsLeapYear($AYear){
  116. return ($AYear % 4 == 0) && (($AYear % 100 != 0) || ($AYear % 400 == 0));
  117. }
  118. //The number of days in the month in the Gregorian calendar (year: year; month: month)
  119. private function GetSMon( $year,$month)
  120. {
  121. if($this->IsLeapYear($year) && $month == 2)
  122. return 29;
  123. else
  124. return $this->_SMDay[$month];
  125. }
  126. //Lunar calendar name conversion
  127. private function LYearName($year)
  128. {
  129. $Name = array("zero","one","two","three","four","five","six"," seven","eight","nine");
  130. for($i=0;$i for($k=0;$k if($year[ $i]==$k)
  131. $tmp.=$Name[$k];
  132. return $tmp;
  133. }
  134. private function LMonName($month)
  135. {
  136. if($month >=1 && $month {
  137. $Name = array( 1=>"正","二","三","四","五","六","七","八"," nine","ten","eleven","twelve");
  138. return $Name[$month];
  139. }
  140. return $month;
  141. }
  142. private function LDayName($day)
  143. {
  144. if( $day >=1 && $day {
  145. $Name = array( 1 =>
  146. "First day of junior high school", "Second day of junior high school", "Third day of junior high school", "Fourth day of junior high school", "Fifth day of junior high school" ","The sixth day of the lunar month","The seventh day of the lunar month","The eighth day of the lunar month","The ninth day of the lunar month","The tenth day of the lunar month",
  147. "Eleventh","Twelve","Thirteenth","Fourteenth","Tenth "Five", "Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty",
  148. "Twenty-one", "Twenty-two", "Twenty-three", "Twenty-four", " "Twenty-five", "Twenty-six", "Twenty-seven", "Twenty-eight", "Twenty-nine", "Thirty"
  149. );
  150. return $Name[$day];
  151. }
  152. return $day;
  153. }
  154. //Convert Gregorian calendar to lunar calendar (Sdate: Gregorian calendar date)
  155. public function S2L($date)
  156. {
  157. list($year, $month, $day) = explode("-", $date);
  158. if($ year = 2051 ) return false;
  159. //Get the number of days from the query date to January 1st of the current year
  160. $date1 = strtotime($year."-01-01");//January 1 of the current year
  161. $date2 = strtotime($year."-".$month."-".$day);
  162. $days=round (($date2-$date1)/3600/24);
  163. $days += 1;
  164. //Get the corresponding annual lunar calendar data and turn it into an array Larray
  165. $Larray = $this->_LMDay[$year - $this- >_LStart];
  166. if($days {
  167. $Lyear = $year - 1;
  168. $days = $Larray[0] - $days;
  169. $Larray = $this- >_LMDay[$Lyear - $this->_LStart];
  170. if($days {
  171. $Lmonth = 12;
  172. $Lday = $Larray[12] - $days;
  173. }
  174. else
  175. {
  176. $Lmonth = 11;
  177. $days = $days - $Larray[12];
  178. $Lday = $Larray[11] - $days;
  179. }
  180. }
  181. else
  182. {
  183. $Lyear = $ year;
  184. $days = $days - $Larray[0];
  185. for($i = 1;$i {
  186. if($days > $Larray[$i]) $ days = $days - $Larray[$i];
  187. else
  188. {
  189. if ($days > 30){
  190. $days = $days - $Larray[13];
  191. $Ltype = 1;
  192. }
  193. $ Lmonth = $i;
  194. $Lday = $days;
  195. break;
  196. }
  197. }
  198. }
  199. return mktime(0, 0, 0, $Lmonth, $Lday, $Lyear);
  200. //$Ldate = $Lyear. "-".$Lmonth."-".$Lday;
  201. //$Ldate = $this->LYearName($Lyear)."Year".$this->LMonName($Lmonth)."Month". $this->LDayName($Lday);
  202. //if($Ltype) $Ldate.="(Leap)";
  203. //return $Ldate;
  204. }
  205. //Convert lunar calendar to Gregorian calendar (date: lunar calendar date; type: whether it is a leap month)
  206. public function L2S($date,$type = 0)
  207. {
  208. list($year, $month, $day) = split("-",$date);
  209. if($year = 2051 ) return false;
  210. $Larray = $this->_LMDay[$year - $this->_LStart ];
  211. if($type == 1 && count($Larray) //If the lunar calendar being queried is a leap month and the lunar calendar array of the year contains a leap month Get the data
  212. if($Larray[$month]>30 && $type == 1 && count($Larray) >=13) $day = $Larray[13] + $day;
  213. //Get the year The number of days from the lunar date to January 1 of the Gregorian calendar
  214. $days = $day;
  215. for($i=0;$i $days += $Larray[$i];
  216. //When querying the lunar date is more than one year from January 1 of the Gregorian calendar
  217. if($days > 366 || ($this->GetSMon($month,2)!=29 && $days>365 ))
  218. {
  219. $Syear = $year +1;
  220. if($this->GetSMon($month,2)!=29)
  221. $days-=366;
  222. else
  223. $days-=365;
  224. if($days > $this->_SMDay[1])
  225. {
  226. $Smonth = 2;
  227. $Sday = $days - $this->_SMDay[1];
  228. }
  229. else
  230. {
  231. $Smonth = 1;
  232. $Sday = $days;
  233. }
  234. }
  235. else
  236. {
  237. $Syear =$year;
  238. for($i=1;$i {
  239. if($days > $this- >GetSMon($Syear,$i))
  240. $days-=$this->GetSMon($Syear,$i);
  241. else
  242. {
  243. $Smonth = $i;
  244. $Sday = $days;
  245. break ;
  246. }
  247. }
  248. }
  249. return mktime(0, 0, 0, $Smonth, $Sday, $Syear);
  250. //$Sdate = $Syear."-".$Smonth."-".$Sday;
  251. //return $Sdate;
  252. }
  253. }
  254. ?>
Copy code
  1. require_once 'Lunar.php';
  2. $today = date("Y-m-d");
  3. $lunar = new Lunar();
  4. //Gregorian calendar to lunar calendar
  5. $nl = date("Y-m-d ",$lunar->S2L($today));
  6. //Convert lunar calendar to Gregorian calendar
  7. $gl = date("Y-m-d",$lunar->L2S($nl));
  8. echo "Today's Gregorian calendar is:$ today
    ";
  9. echo "The conversion to the lunar calendar is: $nl
    ";
  10. echo "The conversion to the Gregorian calendar is: $gl
    ";
  11. ?>
Copy Code


Statement
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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.