1

I need to be able to get the last day of the previous month into a date variable. The script could be run on any day of the current month. It needs to dynamically adjust even if the last day of the previous month falls on 29,30, 31 etc.

What would be the best day to do this?

stormist
  • 5,709
  • 12
  • 46
  • 65
  • 2
    So many similar questions... [How can I create a function that can figure out the previous month's last day dates?](http://stackoverflow.com/questions/1370486/how-can-i-create-a-function-that-can-figure-out-the-previous-months-last-day-dat) [PHP last day of the month](http://stackoverflow.com/questions/1686724/php-last-day-of-the-month) At least show us what you tried already. – deceze Jul 02 '10 at 05:35

1 Answers1

7

I'll give a way that none of the duplicates mentioned. mktime supports out-of-range values, so if you ask for 7/40/2010 it'll give you 8/9/2010 instead; you can reverse that and ask for day 0 of the current month to get the last day of the previous:

$lastDayTimestamp = mktime(0, 0, 0, date('n'), 0);
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175