In PHP, you can get the last day of the month of a specific date by using the t
format option of the date() function. Here are some examples.
Object oriented way
<?php $dt = new DateTime(); // Create an instance of the current date & time. $dt->setDate(2019, 8, 4); // Set $dt to a different date. echo $dt->format('Y-m-t'); // Output: 2019-08-31 echo "\n"; // Or, you can also use modify(). It uses the same format as strtotime(). $dt->modify('last day of this month'); echo $dt->format('Y-m-d'); // Output: 2019-08-31 ?>
Output
2019-08-31 2019-08-31
Functional way
If you prefer to use functions, then use mktime() to set your date and then use date() to get the last day of the month.
<?php $year = 2019; $month = 8; $day = 4; $last_day_of_month = date("Y-m-t", mktime(0,0,0,$month,$day,$year)); echo $last_day_of_month; // Output: 2019-08-31 ?>
Output
2019-08-31