How to imitate two date processing functions of SQLServer

Source: Internet
Author: User
Tags date1
// It is inconvenient to process dates in PHP, for example, to find the month with a different date? What should I do? // File name: date. inc. php3 // before applying the two functions, convert the date or date to the timestamp type. // For example: // $ today = // it is inconvenient to process the date in PHP. for example, it is inconvenient to calculate the month of the difference between two dates? What should I do?
// File name: date. inc. php3
// Before applying 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 '));

/***** Mimic the dateadd function in sqlserver *******
$ Part Type: string
Value range: year, month, day, hour, min, sec
Performance: The part of the date to be added.
$ N type: value
Performance: determine which part to add based on $ part.
Negative
$ Datetime type: timestamp
Performance: increased base
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;
}

/***** Mimic the datediff function in sqlserver *******
$ Part Type: string
Value range: year, month, day, hour, min, sec
Performance: The part of the date to be added.
$ Date1, $ date2 type: timestamp
Performance: 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;
}

}

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.