Find last day of the month in PHP
Yday I was working with dates and got the situation to get the last day of the month so got this piece of code.
date(’Y-m-d’,strtotime(’-1 second’,strtotime(’+1 month’,strtotime(date(’m').’/01/’.date(’Y').’ 00:00:00′))));
And this piece of code will gv you the last day of the your corrent month.
tags: PHP, code, month, programming






Simply Great Script.
How do it for the last month one
great! thx!
to get the last month,
date(‘Y-m-d’,strtotime(‘-1 second’,strtotime(date(‘m’).’/01/’.date(‘Y’).’ 00:00:00′)));
the output will bre the last day of the month for last month.
Hi
I was thinking to write a complete function and then googled your this post. I cant believe you did it with one line.
great work thank you.
Natasha
Just for the record -
This here is a bit shorter and thus better readable:
date(“Y-m-d”,mktime(0, 0, 0, (date(‘m’) + 1), 0, date(‘Y)))
date(‘t’) is enough to get the last day of the month.
You could combine all those strtotime functions:
strtotime(‘+1 month -1 second ‘.date(‘Y’).’-’.date(‘m’).’-01′ );
kumar is right to get the last day of the current month is just date(‘t’) you don’t need to use more function, remember keep it simple
I agree this is much faster and readable
date(“Y-m-t”); //t = number of days in the month
cal_days_in_month() return days of month. Very handy, because format and year are parameters.
Thank you.. thank you.. date(’t’) returns only date of the last month. But I was looking for code that return complete date for last day of the month(ex – 2000-01-31) and found it here – I used tobi’s code –
date(“Y-m-d”,mktime(0, 0, 0, (date(‘m’) + 1), 0, date(‘Y’)))
Thanks Guys… This Post just help me out to find the parameter t. I’m using date(‘t’).
Cheers,
alita
super funda yar thanks a lot i using this i have formulated many calculation on date
here is the find last month
if(date(“m”)==1)
{
$to_month= 12 ;
$to_year=date(“Y”)-1;
}
else
{
$to_month=date(“m”)-1;
$to_year=date(“Y”);
}
$totime = date(“Y-m-d”,strtotime(“-1 second”,strtotime(“+1 month”,strtotime($to_month.”/01/”.$to_year.” 00:00:00″)))).”00:00:00″;;
thank you for good code
ThanX for the functional bindings.
pooleez, just use date(‘Y-m-t’)
why all the complications
[...] http://ullu.wordpress.com/2007/02/28/find-last-day-of-the-month-in-php/ [...]
all this seems overly complex when ‘mktime(0,0,0,date(“m”) + 1, 0, date(“Y”)));’ does the same thing. Less function calls and slightly easier to read.
mktime is very powerful, changing years or months if one value causes the them to roll over. date(“Y-m-d’,mktime(0,0,0, 2,32,2008)) will com out as 2008-03-03