-3

Possible Duplicate:
PHP last day of the month

I have to set an expiry date to my application.

First user have to select a duration of listing, for eg its 1 year and today is 25-Nov-2012

Then we have to add this 1 year to current date and automatically set expiry date as its month end. ie expiry date will be 30-Nov-2013

If current date is 04-Oct-2012 and select 1 year duration, then expiry date is 31-Oct-2012.

I success in add one/two/ whatever years to the current date, but how to find out its month end ?

Community
  • 1
  • 1
ramesh
  • 4,008
  • 13
  • 72
  • 117
  • http://stackoverflow.com/questions/8912780/get-the-last-day-of-the-month - this might help – Alex Nov 27 '12 at 15:44
  • Please see http://php.net/manual/en/datetime.add.php – dead beef Nov 27 '12 at 15:44
  • 2
    in function date, parameter "t" return number of days in the given month http://php.net/manual/en/function.date.php – adalpari Nov 27 '12 at 15:53
  • Do you know timestamp? You should rather use timestamp to save and calculate the delta between values (current and the value saved in DB) ;-) – clement Nov 27 '12 at 15:44

3 Answers3

3

You can do this:

echo date("Y-m-t", strtotime("+1 year"));

See the DOCs for strtotime

And using t for the date function to get the last day of the month

Naftali
  • 144,921
  • 39
  • 244
  • 303
2

Something like the following should work for you:

echo date("Y-m-t", strtotime("+1 year"));
Alex
  • 7,320
  • 1
  • 18
  • 31
-1

try this code:

echo date(date('t',strtotime($date)) . '-M-Y', strtotime($date . ' +1 year'));
Igor Mancos
  • 314
  • 6
  • 15