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