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.