ItSolutionStuff.com

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

By Hardik Savani • May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Convert Datetime into Milliseconds in PHP?

Read Now →

How to Increase Session Timeout in PHP?

Read Now →

How to Extract Img SRC, Title and Alt from HTML in PHP?

Read Now →

How to Convert Bytes into KB, MB and GB in PHP?

Read Now →

How to Convert Array to XML in PHP?

Read Now →

How to Upgrade PHP Version from 8.2 to 8.3 in Ubuntu?

Read Now →

How to Install PHP 8.3 on Ubuntu 22.04?

Read Now →

How to Send Mail using PHPMailer in Laravel?

Read Now →

PHP MySQL Login with Google Account Example

Read Now →

PHP MySQL Image Gallery CRUD Example

Read Now →

PHP JQuery Ajax Image Upload Example Tutorial

Read Now →

PHP Ajax Dependent Dropdown List Example

Read Now →