How to Get Last Day of Month from Date in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hello Dev,

In this post, we will learn php get last day of month from date. This article goes in detailed on php date last day of month from date. This example will help you how to get last day of month from date in php. you can see php find last day of month from date.

In this example, i will give you following two simple examples with output to find last day of month from date in php. so, let's see the one by one example:

Example 1:

If you want to get the last day of the month for a specific date, you can use "strtotime" to convert a date string to a Unix timestamp and then format it:

index.php

<?php

$specificDate = '2023-04-15';

$lastDayOfMonth = date('Y-m-t', strtotime($specificDate));

echo $lastDayOfMonth;

?>

Output:

2023-04-30

Replace '2023-04-15' with your specific date. This code will output the last day of the month for the specified date.

Example 2:

In PHP, you can use the "date" and "strtotime" functions to get the last day of the month. Here's an example:

index.php

<?php

$lastDayOfMonth = date('Y-m-t'); /* 't' returns the number of days in the month */

echo $lastDayOfMonth;

?>

Output:

2023-12-31

In this example, "date('Y-m-t')" will give you the current date in the format 'Y-m-d', where 't' is replaced by the number of days in the month. This effectively gives you the last day of the month.

I hope it can help you...

Tags :
Shares