Mar. 8 2011
0

Get the last day of the month

Just ran into an issue where I needed to know the last day of the month in PHP. Sure enough, there’s a way.

date('Y-m-d',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))-1);

That line will output something like 2011-03-31.

If you need just the Unix timestamp, you can just remove the date function. It’ll output something like 1301633999, which is 2011-03-01 23:59:59 GMT.

strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')));

The way it works goes a little something like this:

  1. Gets the Unix timestamp for the first day of the current month.
  2. Adds a month–giving you the first day of the next month–and returns the timestamp.
  3. Subtracts one second from that time giving you the last second of the last day of the current month.

Leave a Reply

Previous post:

Next post:

Last.fm interface from fmTuner (modified by me).

Twitter interface from HL Twitter.

Dedicated to my mom, Sharon. May she rest peacefully.
Love you always. (March 23, 1965 - November 12, 2011)

Unsupported Browser!

My site is designed for and currently displays best with modern browsers (and it looks like you don't have one!).

Everything should still function properly, but you won't be able to see all the fancy effects, text, or the way I intended my design to look.

Consider trying the latest version of either Firefox or Chrome... I think you'll like it!

Logan Bibby

P.S.: You can continue viewing my site by clicking the "close" link and you'll never see this again. :)