Simulate two SQL Server Functions: dateadd () and datediff ()

Source: Internet
Author: User
Tags date1

<? 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 >?>

Related Article

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.