To get the last day of a month, you can use the php date() function and format with “Y-m-t”. “t” returns the last day of a month.
echo date('Y-m-t');
echo date('Y-m-t', strtotime('22-04-01'));
// Output:
2022-04-30
2022-04-30
When working with date objects in php, the ability to easily change or get information about a specific time period is valuable.
One such piece of information is the last day of a given month.
We can easily get the last day of a given month with the php date() function.
To get the last date of a month, you can use the “Y-m-t” format.
Below is an example of how you can use date() to get the last date of a month in php.
echo date('Y-m-t');
echo date('Y-m-t', strtotime('22-04-01'));
// Output:
2022-04-30
2022-04-30
Getting the Number of Days in the Current Month with php
If you want to get the number of days in the current month, we can use the date() function and only pass ‘t’. As we know, ‘t’ returns the last day in a month.
Below is an example in php of how to get the number of days in the current month using date().
echo date('t');
// Output:
30
Hopefully this article has been useful for you to learn how to get the last date of a given month in your php code.