/ **
This is the definition of the Gregorian calendar and the lunar calendar. Due to the date calculation limit of PHP, only the time between 1970-1938
The calculation method of the lunar calendar uses Mr. Lin Yuxian's algorithm, thank you! In Joy ASP, you can find Mr. Lin's masterpiece (JavaScript)
* /
/ **
* Date class
* Method for using JavaScript's date object with JavaScript
* Set the $ mode property, compatible with the JavaScript date object
* /
Class Date {
Var $ TIME = 0;
Var $ mode = 0; // This property is set to JavaScript, $ mode = 1 is JavaScript method
Var $ DATEMODE = "Y-m-D h: i: s"; // Date format "Y-m-D h: i: s", can set itself
Function date ($ T = 0) {
IF ($ t == 0)
$ this-> time = Time ();
}
/ **
* Return to milliseconds from GMT time starting January 1, 1970
* /
Function gettime () {
$ TEMP = GetTimeOfDay ();
RETURN $ TEMP [SEC] * 1000 Round ($ TEMP [USEC] / 1000);
}
/ **
* Return to the year
* /
Function getYear () {
$ TEMP = GetDate ($ this-> time);
RETURN $ TEMP [Year];
}
/ **
* Return to the month
* /
Function getMonth () {
$ TEMP = GetDate ($ this-> time);
RETURN $ TEMP [MON] - $ this-> mode;
}
/ **
* Return date
* /
Function getdate () {
$ TEMP = GetDate ($ this-> time);
Return $ TEMP [MDAY];
}
/ **
* Return to the week
* /
Function getday () {
$ TEMP = GetDate ($ this-> time);
RETURN $ TEMP [WDAY] - $ this-> mode;
}
/ **
* Return to the hour
* /
Function gethours () {
$ TEMP = GetDate ($ this-> time);
RETURN $ TEMP [Hours];
}
/ **
* Return
* /
Function getminutes () {
$ TEMP = GetDate ($ this-> time);
RETURN $ TEMP [Minutes];
}
/ **
* Return second
* /
Function GetSeconds () {
$ TEMP = GetDate ($ this-> time);
Return $ TEMP [SECONDS];
}
/ **
* Set the year
* php 4.0.6 Year 1970 - 2038
* /
Function setYear ($ VAL) {
$ TEMP = GetDate ($ this-> time);
$ TEMP [Year] = $ VAL;
$ this-> set_time ($ TEMP);
}
/ **
* Set the month
* /
Function setMonth ($ VAL) {
$ TEMP = GetDate ($ this-> time);
$ TEMP [MON] = $ VAL $ THIS-> MODE;
$ this-> set_time ($ TEMP);
}
/ **
* Setting date
* /
Function setDate ($ TEMP = GetDate);
$ TEMP [MDAY] = $ VAL;
$ this-> set_time ($ TEMP);
}
/ **
* Set the week
* /
Function setday ($ VAL) {
$ TEMP = GetDate ($ this-> time);
$ TEMP [wday] = $ val $ this-> mode;
$ this-> set_time ($ TEMP);
}
/ **
* Set an hour
* /
Function sethouse ($ VAL) {
$ TEMP = GetDate ($ this-> time);
$ TEMP [Hours] = $ VAL;
$ this-> set_time ($ TEMP);
}
/ **
* Settings
* /
Function Setminutes ($ VAL) {
$ TEMP = GetDate ($ this-> time);
$ TEMP [Minutes] = $ VAL;
$ this-> set_time ($ TEMP);
}
/ **
* Settings
* /
Function setseconds ($ VAL) {
$ TEMP = GetDate ($ this-> time);
$ TEMP [SECONDS] = $ VAL;
$ this-> set_time ($ TEMP);
}
/ **
* Return to the string of system format
* /
Function tolocalestring () {
Return Date ($ this-> DATEMODE, $ this-> time);
}
/ **
* Create a date value using GTM time
* /
Function UTC ($ YEAR, $ MON, $ MDAY, $ Hours = 0, $ minutes = 0, $ seconds = 0) {
$ this-> time = mktime ($ Hours, $ MINUTES, $ Seconds, $ MON, $ MDAY, $ YEAR)
Return $ this-> Time;
}
/ **
* Is equivalent to dateadd (Interval, Number, Date)
* Returns the date that has been added to specify the time interval.
* INETRVAL is a time interval character, such as divided or days to indicate.
* Number is a numerical expression that represents the number of time intervals to be added.
* Date Representation Date
*
* Interval (time interval character string express) can be the following any value:
* YYYY Year Year
* Q quarter quarter
* M Month Month
* Y day of year's number of years
* D day
* W Weekday's day number of days
* WW Week of Year Week
* H hours hour
* N Minute
* S Second Second
* W, y and d are exactly the same, that is, add one day on the current date, Q plus 3 months, WW plus 7 days.
* /
Function Add ($ Interval, $ Number, $ DATE) {
$ DATE = DATE:: GET_TIME ($ DATE);
$ DATE_TIME_ARRAY = Getdate ($ DATE);
$ Hours = $ DATE_TIME_AURRAY ["Hours"];
$ minutes = $ date_time_Array ["minutes"];
$ Seconds = $ DATE_TIME_ARRAY ["Seconds"];
$ MONTH = $ DATE_TIME_ARRAY ["MON"];
$ day = $ date_time_Array ["mday"]; $ year = $ date_time_Array ["year"];
Switch ($ interval) {
Case "YYYY": $ YEAR = $ Number; Break;
Case "Q": $ MONTH = ($ Number * 3); Break;
Case "M": $ MONTH = $ Number; Break;
Case "Y":
Case "D":
Case "W": $ day = $ Number; Break;
Case "WW": $ Day = ($ Number * 7); Break;
Case "H": $ Hours = $ Number; Break;
Case "N": $ MINUTES = $ Number; Break;
Case "S": $ Seconds = $ Number; Break;
}
$ TEMPTIME = MKTIME ($ Hours, $ MINUTES, $ Seconds, $ MONTH, $ Day, $ YEAR)
Return $ TEMPTIME;
}
/ **
* Is equivalent to Datediff (Interval, Date1, Date2)
* Returns time interval between two dates
* Intervals (time interval string expressions) can be the following anything:
* W circumference
* D days
* H hours
* N minutes
* S second
* /
Function Diff ($ Interval, $ DATE1, $ DATE2) {
// Get the number of seconds between the two days
$ TIMEDIFCERENCE = DATE :: GET_TIME ($ DATE2) - DATE: GET_TIME ($ DATE1);
Switch ($ interval) {
Case "W": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 604800); BREAK;
Case "D": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 86400); Break;
Case "H": $ RETVAL = BCDIV ($ TIMEDIFMERENCE, 3600); Break;
Case "N": $ RETVAL = BCDIV ($ TIMEDIFERENCE, 60); Break;
Case "S": $ RETVAL = $ TIMEDIFCERENCE; BREAK;
}
Return $ RetVal;
}
/ **
* Output, modify this function directly or rewrite this function in the derived class
* /
Function Display () {
$ nstr = array ('day', 'one', 'two', 'three', 'four', 'five', 'six');
Echo Sprintf ("% 4D% 2D month% 2D day Week% s
", $ this-> getYear (), $ this-> getMonth (), $ this-> getdate (), $ nSTR [$ this -> getDay ()% 7]);
}
/ **
* Working function
* /
Function set_time (& $ A) {
$ this-> Time = MKTIME ($ AR [Hours], $ AR [MINUTES], $ AR [SECONDS], $ AR [MON], $ AR [MDAY], $ AR [Year]);
/ **
* Convert to UNIX timestamp
* /
Function GET_TIME ($ d) {
IF (IS_NUMERIC ($ D))
Return $ D;
Else {
IF (! is_string) Return 0;
IF (EREG (":", $ d)) {
$ BUF = Split (" ", $ d);
$ year = split ("[- /]", $ buf [0]);
$ Hour = Split (":", $ BUF [1]);
IF (EREGI ("PM", $ BUF [2]))
$ Hour [0] = 12;
Return Mktime ($ Hour [0], $ Hour [1], $ Hour [2], $ Year [1], $ Year [2], $ YEAR [0]);
} else {
$ year = split ("[- /]", $ d);
Return Mktime (0,0,0, $ year [1], $ year [2], $ year [0]);
}
}
}
} // Date class definition end
?>
/ **
* Lunar calendar
* /
Class Lunar {
Var $ year;
Var $ MONTH;
Var $ day;
Var $ isleap;
Var $ yearcyl;
Var $ daycyl;
Var $ MONCYL;
Var $ TIME;
Var $ lunarinfo = array (
0x04BD8, 0X04AE0, 0X0A570, 0X054D5, 0X0D260, 0X0D950, 0X16554, 0X056A0, 0X09AD0, 0X055D2,
0x04AE0, 0x0A5B6, 0X0A4D0, 0X0D250, 0X1D255, 0X0B540, 0X0D6A0, 0X0ADA2, 0X095B0, 0X14977,
0x04970, 0x0A4B0, 0X0B4B5, 0X06A50, 0X06D40, 0X1Ab54, 0x02B60, 0X09570, 0X052F2, 0X04970,
0x06566, 0x0D4A0, 0X0EA50, 0X06E95, 0X05AD0, 0X02B60, 0X186E3, 0X092E0, 0X1C8D7, 0X0C950,
0x0D4A0, 0X1D8A6, 0X0B550, 0X056A0, 0X1A5B4, 0X025D0, 0X092D0, 0X0D2B2, 0X0A950, 0X0B557,
0x06ca0, 0x0B550, 0X15355, 0X04DA0, 0X0A5D0, 0X14573, 0X052D0, 0X0A9A8, 0X0E950, 0X06AA0,
0x0aea6, 0x0ab50, 0x04b60, 0x0ae4, 0x0a570, 0x05260, 0x0F263, 0x0D950, 0X05B57, 0X056A0,
0x096D0, 0x04DD5, 0X04AD0, 0X0A4D0, 0X0D4D4, 0X0D250, 0X0D558, 0X0B540, 0X0B5A0, 0X195A6,
0x095B0, 0X049B0, 0X0A974, 0X0A4B0, 0X0B27A, 0X06A50, 0X06D40, 0X0AF46, 0X0AB60, 0x09570,
0x04AF5, 0X04970, 0X064B0, 0X074A3, 0X0EA50, 0X06B58, 0X055C0, 0X0AB60, 0X096D5, 0x092E0,
0x0C960, 0x0D954, 0X0D4A0, 0X0DA50, 0X07552, 0X056A0, 0X0ABB7, 0X025D0, 0x092D0, 0X0CAB5,
0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
0x05AA0, 0X076A3, 0X096D0, 0X04BD7, 0X04AD0, 0X0A4D0, 0X1D0B6, 0X0D250, 0X0D520, 0X0DD45,
0x0B5A0, 0X056D0, 0X055B2, 0X049B0, 0X0A577, 0X0A4B0, 0X0AA50, 0X1B255, 0X06D20, 0X0ADA0,
0x14b63);
/ **
* The total number of times that passed back to the Lunar Calendar Y
* /
Function LyeardAys ($ Y) {
$ SUM = 348;
For ($ I = 0x8000; $ I> 0x8; $ I >> = 1)
$ SUM = ($ this-> lunarinfo [$ y-1900] & $ i)? 1: 0;
Return $ SUM $ THIS-> LeapDays ($ Y);
}
/ **
* Remove the number of days of lunar month Y years
* /
Function LeapDays ($ Y) {
IF ($ this-> leapmonth ($ y))
Return ($ this-> lunarinfo [$ y-1900] & 0x10000)? 30: 29;
Else Return 0;
}
/ **
* Remove the lunar calendar Y, which month 1-12, no 传 0 0
* /
Function Leapmonth ($ Y) {
Return $ this-> lunarinfo [$ y-1900] & 0xF;
}
/ **
* The total number of times that passed back the Lunar Calendar Y
* /
Function Monthdays ($ y, $ m) {
Return ($ this-> lunarinfo [$ y-1900] & (0x10000 >> $ m))? 30: 29;
}
/ **
* Create a lunar day object
* /
Function Lunar ($ Objdate, $ MONTH = 1, $ Day = 1) {
$ leap = 0;
$ TEMP = 0;
IF (is_Object ($ objDate))
$ this-> time = mktime (0,0,0, $ objdate-> getMonth (), $ objdate-> getdate (), $ objdate-> getYear ());
Else {
$ year = $ objdate;
$ this-> time = mktime (0,0,0, $ month, $ day, $ year);
IF ($ Year <1970) {
Return;
$ TEMP = 0;
For ($ I = 1970; $ I> $ YEAR; $ I -) {
$ TEMP = $ this-> lyeardays ($ i);
$ OFFSET - = $ TEMP;
}
}
}
$ OFFSET = ROUND ($ this-> Time / 86400 25537);
$ this-> daycyl = $ OFFSET 40;
$ this-> moncyl = 14;
For ($ I = 1900; $ i <$ YEAR && $ OFFSET> 0; $ i ) {
$ TEMP = $ this-> lyeardays ($ i);
$ OFFSET - = $ TEMP;
$ this-> moncyl = 12;
}
IF ($ OFFSET <0) {$ offset = $ TEMP;
$ I -;
$ this-> moncyl - = 12;
}
$ this-> year = $ i;
$ this-> yearcyl = $ i-1864;
$ leap = $ this-> leapmonth ($ i); // is the month
$ this-> isleap = false;
For ($ I = 1; $ I <13 && $ OFFSET> 0; $ i ) {
// 月
IF ($ Leap> 0 && $ I == ($ Leap 1) && $ this-> isleap == false) {
$ I -;
$ this-> isleap = True;
$ TEMP = $ THIS-> LeapDays ($ this-> year);
} else {
$ TEMP = $ this-> Monthdays ($ this-> year, $ i);
}
// Release the month
IF ($ this-> isleap == True && $ i == ($ LeAP 1))
$ this-> isleap = false;
$ OFFSET - = $ TEMP;
IF ($ this-> isleap == false)
$ this-> moncyl ;
}
IF ($ OFFSET == 0 && $ Leap> 0 && $ i == $ leap 1)
IF ($ THIS-> Isleap)
$ this-> isleap = false;
Else {
$ this-> isleap = True;
$ I -;
$ this-> moncyl -;
}
IF ($ OFFSET <0) {
$ OFFSET = $ TEMP;
$ I -;
$ this-> moncyl -;
}
$ this-> month = $ i;
$ this-> day = $ OFFSET 1;
}
Function cyclical ($ num) {
$ GaN = Array ("A", "B", "Ding", "He", "Heng", "Xin", "", "");
$ Zhi = array ("子", "ugly", "寅", "", "", "", "noon", "未", "Shen", "", "", " Hai ");
Return $ GAN [$ Num% 10]. $ zhi [$ Num% 12];
}
/ **
* Output, modify this function directly or rewrite this function in the derived class
* /
Function Display () {
$ nstr = array ('', 'positive', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', '10', '10 One ',' Law ');
Echo Sprintf ("Lunar Calendar% S% S
", ($ this-> isleap? ",": "", $ nSTR [$ this-> Month], $ this-> cday ($ this -> day)); echo sprintf ("% s year% s months% s day", $ this-> cyclical ($ this-> yearcyl), $ this-> cyclical ($ this-> moncyl), $ this- > Cyclical ($ this-> daycyl);
}
/ **
* Chinese dates
* /
Function CDAY ($ d) {
$ nStr1 = array ('day', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', '10');
$ nStr2 = array ('beginning', 'ten', '廿', '卅', '');
Switch ($ d) {
Case 10:
$ S = 'First Ten';
Break;
Case 20:
$ S = 'twenty';
Break;
Case 30:
$ S = '30';
Break;
DEFAULT:
$ S = $ nStr2 [Floor ($ D / 10)];
$ S. = $ nSTR1 [$ D% 10];
}
Return $ S;
}
} // Lunar Calendar Definition end
?>
// Test example
$ d = new date;
echo "
is now:";
$ D-> Display ();
$ D-> setYear (2037);
echo "
is now:";
Echo $ D-> getYear (). "year";
Echo $ D-> getMonth (). "Month";
Echo $ D-> getdate (). "Japan";
Echo "Week". $ D-> getday (). ""
Echo $ D-> GetHours (). "Time";
Echo $ D-> getminutes (). "points";
Echo $ D-> GetSeconds (). "second
";
$ D-> UTC (1998, 10, 2, 22, 0, 8);
Echo $ D-> Tolocalestring (). "
";
$ d = new date;
$ ld = new lunar ($ d);
$ D-> Display ();
$ ld-> display ();
$ D-> UTC (1998, 10, 2, 22, 0, 8);
$ ld = new lunar ($ d);
$ D-> Display ();
$ ld-> display ();
$ ld = new lunar (2002, 2, 12);
$ ld-> display ();
Echo __line__. "
";
$ D1 = "2002-01-11";
$ D2 = DATE ("Y-M-D", $ D-> Add ("D", 35, $ D1));
Echo $ D1. "Date :: DIFF (" D ", $ D1, $ D2)." The day is $ D2
"; Echo $ D1." 10 days ago ".date (" YMD " ", Date :: Add (" d ", - 10, $ d1))."
";
$ D3 = DATE ("Y / m / D H: I: S");
Echo "is now". $ D3. "Distance 2002/2/12 12:59:59" .date :: Diff ("S", $ D3, "2002/2/12 12:59:59") Second
";
?>