Get Last Day Of Month MySQL And PHP

In order to get last day of month in mysql or php, you can use built in functions. As you will soon see below it is a rather straight foward task. I will show you two examples on how to get the last day of the month in mysql as well as php. In php you can use the date() function and in mysql we will make use of the last_day() method.

Get last day of the month PHP

PHP has a date function which returns a string representation of the date formatted according to the expression passed to it. So the simple way is to use the ‘t’ identifier in the date function which will return the last day of the month.

echo date('Y-m-t',strtotime('2014-01-12')); // echo's 2014-01-31
echo date('t M Y'); // echo's 31 Jul 2014

Get last day of month MySQL

MySQL has a function called last day that will return the last day of the date supplied to it. It can be the date of the current month or any other month.

SELECT last_day(CURDATE() - INTERVAL 1 MONTH) AS last_month;
SELECT last_day(CURDATE()) AS current_month; 
SELECT last_day(DATE('2014-03-12')) AS march_month

Conclusion

As you can see, getting the last day of the month is a simple task. If you get stuck, let me know in the comments.