Blog

Back 6 Comments

Find the Last Day of the Month With PHP


Google - last day of month php - and you'll get over 15,000,000 results.  It's amazing how many people have searched for this & how many roundabout ways there are to accomplish what should be a simple task.

Here is the simplest way (that I know of) to get the last day of the month using PHP.

Get last day of the current month


$lastday = date('t',strtotime('today'));

 Get last day of next month

$lastday = date('t',strtotime('next month'));

Get last day of any other month
Enter any month/day/year to get the last day of that month

$lastday = date('t',strtotime('3/1/2009'));

If you want to display the last day of this month on your website, do the following:

<?php
echo date('t',strtotime('today'));
?>

Cat
October 22, 2009 11:11am
At last!

I needed to find out the last day of a user specified month from a dropdown, and I searched and found ridiculously long complicated functions but knew there must be a simple php way of doing it and luckily I came across this blog and from it created the following

$intLastDayInChosenMonth = date('t', strtotime($dteYear.'/'.$dteMonthID.'/01'));

Thankyou!!!
shaid
January 3, 2010 6:57am
Yes,
it simply brilliant!
Thank you.
http://www.askcms.com
Dan
April 23, 2010 2:34pm
thank you.
randee
May 4, 2010 3:57pm
Fabulous! Thanks a million. I love PHP - a simple solution for (practically) everything!
Tribal
June 9, 2010 6:01am
But don't use it for dates after 2038!!

$lastdayFeb = date('t',strtotime('2/2/2039')); // 31!

(Y2K38)
Mike
March 24, 2013 7:54am
I KNEW THERE WAS AN EASY WAY ... THANK YOU, THANK YOU, THANK YOU .. now date calc's are going to be so easy, I wanted to know when to start my web hosting invoices, doing it manually each month, really sucks because I have more than 60 clients now, so I automated it to make those invoices while i am sleeping or out on the town :P

Look what I did and it WORKS!!!:


that's the short version ... lol

THANK YOU VERY MUCH FOR THE CODE!!


Your comment has been posted....