How can we get the last day of the month in PHP?
- t returns the number of days in the month of a given date (see the docs for date):
- The code using strtotime() will fail after year 2038. (as given in the first answer in this thread) For example try using the following:
- It will give answer as: 1970-01-31
- So instead of strtotime, DateTime function should be used. Following code will work without Year 2038 problem:
- “This function will return the number of days in the month of year for the specified calendar.
- ” http://php.net/manual/en/function.cal-days-in-month.
This should work:
- You could create a date for the first of the next month, and then use strtotime(“-1 day”, $firstOfNextMonth)
- If you use the Carbon API extension for PHP DateTime, you can get the last day of the month with:
- Try this , if you are using PHP 5.3+,
- For finding next month last date, modify as follows,
- Here is a complete function:
- This would output following: