Blog

Back 5 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)


Your comment has been posted....
Post a comment

Name


Email (won't be published)


Comments