How to get last day of a month from date in PHP ? (original) (raw)

Last Updated : 12 Jul, 2025

Given a date and the task is to print the last day of the month. We will use date() and strtotime() function to get the last day of the month.

**Used PHP functions:

**Examples:

Input: 2020-04-23
Output: Thursday

Input: '2018-09-11'
Output: Sunday

**Approach:

**Program:

php `

date=strtotime(date = strtotime(date=strtotime(datestring); // Last date of current month. lastdate=strtotime(date("Y−m−t",lastdate = strtotime(date("Y-m-t", lastdate=strtotime(date("Ymt",date )); // Day of the last date day=date("l",day = date("l", day=date("l",lastdate); echo $day; ?>

`

Using DateTime Class

In this approach, we utilize the DateTime class in PHP to get the last day of the month. The DateTime class provides a powerful and flexible way to manipulate and format dates. We can take advantage of its methods to achieve the task efficiently.

**Example: The following example demonstrates how to use the DateTime class to get the last day of the month and print the day of the week.

PHP `

date=newDateTime(date = new DateTime(date=newDateTime(dateString); $date->modify('last day of this month'); dayOfWeek=dayOfWeek = dayOfWeek=date->format('l'); return $dayOfWeek; } $inputDate1 = '2020-04-23'; echo getLastDayOfMonth($inputDate1); $inputDate2 = '2018-09-11'; ?>

`