Example of converting the solar calendar to the lunar calendar in PHP

Source: Internet
Author: User
This article mainly introduces PHP's implementation of the conversion from the Gregorian calendar to the lunar calendar. The example analyzes the principle and implementation skills of the conversion from the Gregorian calendar to the lunar calendar, which has some reference value, for more information, see the examples in this article. Share it with you for your reference. The details are as follows:

The code is as follows:

<? Php
// A class for converting the solar calendar to the lunar calendar in PHP
Class Calendar
{
// Number of days of the lunar calendar month
Var $ everyCMonth = array (
0 => array ),
1 => array ),
2 => array (, 3 ),
3 => array ),
4 => array ),
5 => array ),
6 => array ),
7 => array ),
8 => array ),
9 => array ),
10 => array ),
11 => array (6, 30, 29, 30, 29, 30, 29, 29, 29, 30, 29, 30, 30, 8, 12 ),
12 => array ),
13 => array ),
14 => array ),
15 => array ),
16 => array ),
17 => array (, 29,29, 30,29, 30,30, 29,30, 30,29, 30,29 ),
18 => array (, 29,29, 30,29, 30,29, 30,30, 29,30, 30,0, 5, 7 ),
19 => array ),
20 => array ),
21 => array ),
22 => array ),
23 => array ),
24 => array ),
25 => array ),
26 => array (, 3 ),
27 => array (, 29,29, 30,29, 30,29, 30,29, 30,30, 30,0 ),
28 => array ),
29 => array ),
30 => array (6, 29, 30, 30, 29, 30, 29, 29, 29, 30, 30, 29, 7 ),
31 => array ),
32 => array ),
33 => array (5, 29, 30, 30, 29, 30, 29, 30, 29, 29, 30, 10, 10 ),
34 => array ),
35 => array ),
36 => array (3,30, 29,29, 30,29, 29,30, 30,29, 30,30, 30,29, 3,1 ),
37 => array ),
38 => array ),
39 => array ),
40 => array ),
41 => array (6, 30, 30, 29, 30, 30, 29, 30, 29, 29, 29, 29, 8, 6 ),
42 => array ),
43 => array ),
44 => array ),
45 => array ),
46 => array ),
47 => array ),
48 => array ),
49 => array ),
50 => array ),
51 => array ),
52 => array (5, 29, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 9, 5 ),
53 => array ),
54 => array ),
55 => array ),
56 => array ),
57 => array ),
58 => array ),
59 => array ),
60 => array (6,30, 29,30, 29,30, 30,29, 30,29, 30,29, 30,29, 30,29, 7,1 ),
61 => array ),
62 => array ),
63 => array ),
64 => array ),
65 => array ),
66 => array ),
67 => array ),
68 => array ),
69 => array ),
70 => array (, 29,29, 30,29, 30,30, 29,30, 30,29, 30,0 ),
71 => array ),
72 => array ),
73 => array ),
74 => array ),
75 => array ),
76 => array (8, 30, 29, 30, 29, 30, 29, 30, 29, 29, 30, 3, 5 ),
77 => array ),
78 => array ),
79 => array (6, 30, 29, 30, 29, 30, 30, 29, 30, 30, 29, 30, 29, 6, 8 ),
80 => array ),
81 => array ),
82 => array ),
83 => array (, 12 ),
84 => array ),
85 => array ),
86 => array (, 3 ),
87 => array (6, 30, 29, 30, 29, 30, 29, 30, 30, 29, 30, 29, 29, 4, 4 ),
88 => array ),
89 => array (, 29,29, 30,29, 29,30, 30,29, 30,30, 30,0, 6 ),
90 => array ),
91 => array ),
92 => array (, 30, 30, 29, 30, 29, 29, 29, 30, 30, 30, 0, 9, 9 ),
93 => array (, 10, 10 ),
94 => array ),
95 => array ),
96 => array ),
97 => array ),
98 => array ),
99 => array ),
100 => array ),
101 => array ),
102 => array ),
103 => array ),
104 => array ),
105 => array ),
106 => array ),
107 => array ),
108 => array ),
109 => array ),
110 => array ),
111 => array ),
112 => array ),
113 => array ),
114 => array (9,29, 30,29, 30,29, 30,29, 30,30, 29,30, 29,30 ),
115 => array ),
116 => array ),
117 => array ),
118 => array ),
119 => array ),
120 => array)
);
// Calendar month
Var $ mten = array ("null", "a", "B", "C", "Ding", "E", "Ji", "Geng ", "Xin", "Shen", "Xi"); // lunar calendar
Var $ mtwelve = array ("null", "Child (RAT)", "ugly (cow)", "Yin (Tiger)", "Mao (rabbit )", "Chen (Dragon )",
"Si (snake)", "Wu (horse)", "Wei (goat)", "Shen (monkey)", "you (chicken)", "Shen (dog) "," Hai (pig) "); // lunar month
Var $ mmonth = array ("regular", "positive", "two", "three", "four", "five", "six ",
"7", "8", "9", "10", "11", "12", "month ");
Var $ mday = array ("null", "First Day", "second day", "Third Day", "fourth day", "Fifth Day", "Sixth Day", "Seventh Day ", "Eighth Day", "Ninth Day", "Tenth Day ",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20 ",
"Jun 1", "Jun 2", "Jun 3", "Jun 4", "Jun 5", "Jun 6", "Jun 7", "Jun 8 ", "29th", "30th"); // lunar day
// Assign the initial value
// Daily support
Var $ ten = 0;
Var $ twelve = 0;
Function MyPub ($ cYear, $ cMonth, $ cDay)
{
$ Total = 11; // The total number of days of the Gregorian calendar to January 1, December 21, 1900
$ Mtotal = 0; // Total calendar days
For ($ y = 1901; $ y <$ cYear; $ y ++)
{
$ Total + = 365;
If ($ y % 4 = 0) $ total ++;
}
// Add a few months of the current year
Switch ($ cMonth ){
Case 12:
$ Total + = 30;
Case 11:
$ Total + = 31;
Case 10:
$ Total + = 30;
Case 9:
$ Total + = 31;
Case 8:
$ Total + = 31;
Case 7:
$ Total + = 30;
Case 6:
$ Total + = 31;
Case 5:
$ Total + = 30;
Case 4:
$ Total + = 31;
Case 3:
$ Total + = 28;
Case 2:
$ Total + = 31;
}

// Add one day if the current year is a leap year.
If ($ cYear % 4 = 0 and $ cMonth> 2)
{
$ Total ++;
}
$ Total + = $ cDay-1;
// Use the number of calendar days to accumulate to determine whether the number of calendar days has exceeded the number of calendar days
For ($ j = 0; $ j <= 120; $ j ++)
{
$ I = 1;
For ($ I = 1; $ I <= 13; $ I ++)
{
$ Mtotal + = $ this-> everyCMonth [$ j] [$ I];
If ($ mtotal> = $ total)
{
$ Flag = 1;
Break;
}
}
If ($ flag = 1) break;
}
Return array ("m" => $ j, "d" => $ I, "t" => $ total, "n" => $ mtotal );
}
Function Cal ($ Year, $ Month, $ Day)
{
$ Par = $ this-> MyPub ($ Year, $ Month, $ Day );

$ Md = $ this-> everyCMonth [$ Par ["m"] [$ Par ["d"]-($ Par ["n"]-$ Par [" t "]);
$ Week = ($ Par ["t"] + 5) % 7;
If ($ this-> everyCMonth [$ Par ["m"] [0] <> 0 and $ this-> everyCMonth [$ Par ["m"] [0] <$ Par ["d"])
{
$ Mm = $ Par ["d"]-1;
} Else {
$ Mm = $ Par ["d"];
}
If ($ Par ["d"] ==$ this-> everyCMonth [$ Par ["d"] [0] + 1 and $ this-> everyCMonth [$ Par ["d"] [0] <> 0)
{
$ CMonth = $ this-> mmonth [0]. $ this-> mmonth [$ mm]; // leap month
} Else {
$ CMonth = $ this-> mmonth [$ mm]. $ this-> mmonth [13];
}
Return array ("year" => $ this-> mten [$ this-> everyCMonth [$ Par ["m"] [14]. $ this-> mtwelve [$ this-> everyCMonth [$ Par ["m"] [15],
"Month" => $ cMonth,
"Day" => $ this-> mday [$ md],
"Week" => $ week );
}
}
//// Call
$ Test = new Calendar;
$ Year = $ test-> Cal (, 20 );
Echo $ Year ["year"]. "Year ". $ Year ["month"]. $ Year ["day"]. "Week ". $ Year ["week"];
?>

I hope this article will help you with php programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.