How to find the last day of the month from date?

Mithun Sreedharan picture Mithun Sreedharan · Nov 6, 2009 · Viewed 485k times · Source

How can I get the last day of the month in PHP?

Given:

$a_date = "2009-11-23"

I want 2009-11-30; and given

$a_date = "2009-12-23"

I want 2009-12-31.

Answer

Dominic Rodger picture Dominic Rodger · Nov 6, 2009

t returns the number of days in the month of a given date (see the docs for date):

$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));