PHP Time and date calculation (plus minus) function _php tutorial

Source: Internet
Author: User
In PHP we have to add to the time and date we can use two functions, mktime and strtotime functions, let me give you students to introduce their use methods.

Mktime function

The Mktime () function returns a Unix timestamp for a date.

The parameter always represents the GMT date, so IS_DST has no effect on the result.

Parameters can be left-to-right and empty, and empty parameters will be set to the corresponding current GMT value.

Parameter description
Hour is optional. Specified hours.
Minute is optional. Specify minutes.
Second is optional. Specify seconds.
Month is optional. Specifies the number of months to be represented.
Day is optional. Prescribed days.
Year is optional. Prescribed year. On some systems, the legal value is between 1901-2038. However, there is no such limit in PHP 5.
IS_DST is optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1.

Example

The Mktime () function is useful for date arithmetic and validation. It can automatically correct out-of-bounds input:

The code is as follows Copy Code

Echo (Date ("M-d-y", Mktime (0,0,0,12,36,2001)));
Echo (Date ("M-d-y", Mktime (0,0,0,14,1,2001)));
Echo (Date ("M-d-y", Mktime (0,0,0,1,1,2001)));
Echo (Date ("M-d-y", Mktime (0,0,0,1,1,99)));
?>

Output:

jan-05-2002
feb-01-2002
jan-01-2001
jan-01-1999


Strtotime (Time,now)


In a small project to use the comparison of the chain data, the data span is one weeks, to achieve the positioning of the time so write two functions, welcome to shoot bricks.
Date calculated for Monday:

The code is as follows Copy Code

function Getthemonday ($date) {
if (date (' N ', Strtotime ($date)) = = 1) {
Return date (' y-m-d ', strtotime (' Monday ', Strtotime ($date)));
} else {
Return date (' y-m-d ', Strtotime (' Last Monday ', Strtotime ($date)));
}
}

Date calculated for Sunday:

The code is as follows Copy Code

function Getthesunday ($date) {

Return date (' y-m-d ', strtotime (' Sunday ', Strtotime ($date)));

}


http://www.bkjia.com/PHPjc/445608.html www.bkjia.com true http://www.bkjia.com/PHPjc/445608.html techarticle in PHP we have to add to the time and date we can use two functions, mktime and strtotime functions, let me give you students to introduce their use methods. Mktime function mktime () Letter ...

  • 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.