views:

42

answers:

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?

+6  A: 

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