Thursday, June 11, 2026
HomeLanguagesHow to get last day of a month from date in PHP...

How to get last day of a month from date in PHP ?

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:

  • Given a date stored in a variable as a string, the step is to convert it into date format using strtotime() function.
  • Once we get the date, we will employ date() method.
    Syntax:

    date( $format, $timestamp )
  • Within $format, we will pass “Y-m-t” and within $timestamp the date taken above. In terms of date “Y” gives a full numeric representation of a year in 4 digits, “m” provides numeric representation of a month and “t” gives the number of days in the given month.
  • By using “Y-m-t”, we will get the number of days in a month because of “t”, “m” and “Y” will provide the month and the year.
  • The last step is to use the date() again with “l” for format and the date achieved above as $timestamp.
  • Display the day.

Program:




<?php
   
// Given a date in string format 
$datestring = '2020-04-23';
  
// Converting string to date
$date = strtotime($datestring);
   
// Last date of current month.
$lastdate = strtotime(date("Y-m-t", $date ));
  
  
// Day of the last date 
$day = date("l", $lastdate);
  
echo $day;
?>


Output:

Thursday
RELATED ARTICLES

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS