How to simulate two SQL Server date processing functions

Source: Internet
Author: User
Tags datetime min

// 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 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;
}

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.