Skip to main content

Get the Last Day of a Month in PHP

The PHP date() function is a versatile tool that can be used to format dates and times according to a number of different standards. However, one less-known feature of the date() function is its ability to return the last day in a given month. This can be accomplished by simply using the “t” format when calling the date() function. For example, to return the number of days in the month of February, you would use the following code:

$last_day = date("t", strtotime("February"));
echo $last_day; //28

This code first uses the strtotime() function to convert the string “February” into a timestamp. It then passes this timestamp, along with the “t” format, to the date() function. The result is an integer value representing the number of days in February. This same technique can be used for any month of the year, simply by changing the string passed to the strtotime() function. As such, the date() function provides an easy way to determine the length of any given month.

$last_day = date("t", strtotime("January"));
$last_day = date("t", strtotime("February"));
$last_day = date("t", strtotime("March"));
$last_day = date("t", strtotime("April"));
$last_day = date("t", strtotime("May"));
$last_day = date("t", strtotime("June"));
$last_day = date("t", strtotime("July"));
$last_day = date("t", strtotime("August"));
$last_day = date("t", strtotime("September"));
$last_day = date("t", strtotime("October"));
$last_day = date("t", strtotime("November"));
$last_day = date("t", strtotime("December"));

By continuing to use the site, you agree to the use of cookies.