Php obtains the first and last days of the month.

Source: Internet
Author: User
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. Given a date, get its first and last day of the month functiongetCurMonthFirstDay ($ date) {returndate (Y-m-01

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. Given a date, get its first and last day of the month function getCurMonthFirstDay ($ date) {return date ('Y-m-01

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.

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.

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.

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 is "+ 1 month". php determines the number of days to be added based on the specific month, which may be 28, 29 (January 1, February), 30 (small month), or 31 (large month ); the "-1 day" on the first day of a month is naturally the last day of the previous month. php will be intelligently identified as 28, 29, 30, or 31 based on the month.

Strtotime is very powerful, detailed usage can view the official documentation: http://php.net/manual/zh/function.strtotime.php.

Original article address: php obtains the first day of the month and the last day. Thank you for sharing it.

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.