Php get the first day and last day of the specified month _ PHP Tutorial

Source: Internet
Author: User
Php retrieves the first and last days of the specified month. This article introduces how to use the date and strtotime functions in php to obtain the first day or the last day of the document given by the user. if you need to learn more, you can refer to it. This article introduces how to use the date and strtotime functions in php to obtain the first or last day of the user's given time. if you need to learn more, you can refer to it.

With the date and strtotime functions, you can easily obtain the first and last days of the current month, next month, and last month. The following describes their implementation. The date format of the function is yyyy-MM-dd.

1. specify a date to obtain the first and last days of the month.

The code is as follows:

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. specify a date to obtain the first and last days of the next month.

The code is as follows:

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. specify a date to obtain the first and last days of the next month.

The code is as follows:

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 '));
}

The strtotime function parameter "+ 1 month", php determines the number of days to be added based on the specific month, which may be 28, 29 (February), 30 (small month), or 31 (large month ); the first day of a month, "-1 day", is naturally the last day of the month. php will be intelligently identified as 28, 29, 30, or 31 based on the month.


Strtotime-parses the date and time descriptions of any English text into Unix timestamps


Report a bug description
Int strtotime (string $ time [, int $ now = time ()])
This function is expected to accept a string that contains a date format in American English and try to parse it into a Unix timestamp (seconds since January 1 1970 00:00:00 GMT ), the value is relative to the time given by the now parameter. If this parameter is not provided, the current time is used.

Use the date and strtotime functions in week to obtain the first day of the user's given time or the last day of the article. if you need to learn more, refer. 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.