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