<? 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 number of days to be added, determine which part to add based on $ part
can be a negative number
$ datetime type: Timestamp
indicates the added base number
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;
}< br> $ 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
indicates: two dates to be compared
return type: value
**************** end * (**************/
function datediff ($ part, $ date1, $ date2) {
// $ diff = $ date2-$ date1;
$ year1 = date ("Y", $ date1 );
$ year2 = date ("Y", $ date2);
$ mon2= 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, $ mon22, $ day2, $ year2)-mktime (3600, 0, $ month1, $ day1, $ year1)/(* 24);
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;
}< br> return $ ret;
}< BR >?>