<? PHP
// File name: Date. Inc. php3
// Before using these two functions, convert the date or date to the timestamp type.
// For example:
// $ Today = mktime (0, 0, 0, date ("M"), date ("D"), date ("Y "));
/***** Simulate the dateadd function in sqlserver *******
$ Part type: String
Value Range: year, month, day, hour, Min, Sec
Indicates the part of the date to be added.
$ N type: Value
Indicates the amount to be added. You can decide which part to add based on $ part.
Negative
$ Datetime type: Timestamp
Indicates the base number to be added.
Return type: Timestamp
**************/
Function dateadd ($ part, $ N, $ datetime ){
$ Year = date ("Y", $ datetime );
$ Month = date ("M", $ datetime );
$ Day = date ("D", $ datetime );
$ Hour = date ("H", $ datetime );
$ Min = date ("I", $ datetime );
$ Sec = date ("S", $ datetime );
$ Part = strtolower ($ part );
$ Ret = 0;
Switch ($ part ){
Case "year ":
$ Year + = $ N;
Break;
Case "month ":
$ Month + = $ N;
Break;
Case "day ":
$ Day + = $ N;
Break;
Case "Hour ":
$ Hour + = $ N;
Break;
Case "min ":
$ Min + = $ N;
Break;
Case "Sec ":
$ Sec + = $ N;
Break;
Default:
Return $ ret;
Break;
}
$ Ret = mktime ($ hour, $ min, $ sec, $ month, $ day, $ year );
Return $ ret;
}
/***** Simulate the datediff function in sqlserver *******
$ Part type: String
Value Range: year, month, day, hour, Min, Sec
Indicates the part of the date to be added.
$ Date1, $ date2 type: Timestamp
Two dates to be compared
Return type: Numeric Value
* ************** End *(*************/
Function datediff ($ part, $ date1, $ date2 ){
// $ Diff = $ date2-$ date1;
$ Year1 = date ("Y", $ date1 );
$ Year2 = date ("Y", $ date2 );
$ Mon2nd = date ("M", $ date2 );
$ Month1 = date ("M", $ date1 );
$ Day2 = date ("D", $ date2 );
$ Day1 = date ("D", $ date1 );
$ Hour2 = date ("D", $ date2 );
$ Hour1 = date ("D", $ date1 );
$ Min2 = date ("I", $ date2 );
$ Min1 = date ("I", $ date1 );
$ Sec2 = date ("S", $ date2 );
$ Sec1 = date ("S", $ date1 );
$ Part = strtolower ($ part );
$ Ret = 0;
Switch ($ part ){
Case "year ":
$ Ret = $ year2-$ year1;
Break;
Case "month ":
$ Ret = ($ year2-$ year1) * 12 + $ mon2s-$ month1;
Break;
Case "day ":
$ Ret = (mktime (, 0, $ mon2, $ day2, $ year2)-mktime (, 0, $ month1, $ day1, $ year1 )) /(24x3600 );
Break;
Case "Hour ":
$ Ret = (mktime ($ hour2, $ mon2, $ day2, $ year2)-mktime ($ hour1, $ month1, $ day1, $ year1 )) /3600;
Break;
Case "min ":
$ Ret = (mktime ($ hour2, $ min2, 0, $ mon2, $ day2, $ year2)-mktime ($ hour1, $ min1, 0, $ month1, $ day1, $ year1)/60;
Break;
Case "Sec ":
$ Ret = $ date2-$ date1;
Break;
Default:
Return $ ret;
Break;
}
Return $ ret;
}
?> |