PHP Gets the first day and last day of the specified month _php tutorial

Source: Internet
Author: User
This article introduces you to the use of the date and Strtotime function in PHP to get the user a given time of the first day or the last day of the article, there is a need to understand learning friends can enter the reference.

With the date and Strtotime functions, it is easy to get the first and last day of the month, next month, and last month, respectively, with their implementation. Where the function's parameter date format is YYYY-MM-DD.

1. Given a date, get its first and last day of the month

The code is as follows Copy Code

function Getcurmonthfirstday ($date) {
Return date (' y-m-01 ', Strtotime ($date));
}

function Getcurmonthlastday ($date) {
Return date (' y-m-d ', strtotime (date (' y-m-01 ', Strtotime ($date)). ' +1 month-1 Day ');
}

2. Given a date, get its first and last day of next month

The code is as follows Copy Code

function Getnextmonthfirstday ($date) {
Return date (' y-m-d ', strtotime (date (' y-m-01 ', Strtotime ($date)). ' +1 month '));
}

function Getnextmonthlastday ($date) {
Return date (' y-m-d ', strtotime (date (' y-m-01 ', Strtotime ($date)). ' +2 month-1 Day ');
}

3. Given a date, get its first and last day of next month

The code is as follows Copy Code

function Getprevmonthfirstday ($date) {
Return date (' y-m-d ', strtotime (date (' y-m-01 ', Strtotime ($date)). '-1 month '));
}

function Getprevmonthlastday ($date) {
Return date (' y-m-d ', strtotime (date (' y-m-01 ', Strtotime ($date)). '-1 day '));
}

Where the Strtotime function parameter "+1 month", PHP will determine the number of days to increase depending on the specific month, may be 28, 29 (February), 30 (Xiao Yue) or 31 (Otsuki); The first day of the month "1 days" is naturally the last day of the last month, PHP will also be 28, 29, 30 or 31 depending on the month's intelligence.


strtotime-to parse the datetime description of any English text into a Unix timestamp


Report a bug description
int Strtotime (string $time [, int $now = time ()])
This function expects to accept a string containing the U.S. English date format and attempt to resolve it to a Unix timestamp (the number of seconds from January 1 1970 00:00:00 GMT) whose value is relative to the time given by the now parameter, if this parameter is not provided, the current time of the system.

http://www.bkjia.com/PHPjc/631520.html www.bkjia.com true http://www.bkjia.com/PHPjc/631520.html techarticle This article introduces you to the use of the date and Strtotime function in PHP to get the user a given time of the first day or the last day of the article, there is a need to understand learning friends can enter the reference. With the help of D ...

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