1

Let's say your creating an application that needs to send you an email monthly. The date start date is FEB 29, 2012. How do you deal with that for next year since there is no FEB 29, 2013. Likewise if the start date is JAN 31, there is no FEB 31 or APR 31, and so on...

IMB
  • 15,163
  • 19
  • 82
  • 140
  • As the designer of the application, that's up to you, isn't it? Either take the 1st of next month or the last of this month. The easiest way is to use `strtotime()` ( e.g. `strtotime( '+1 month' )`) and go with what it returns. – JJJ Mar 29 '12 at 11:39
  • To those voting for a close, what's problem with this? Doesn't this fit in the "design" category? – IMB Mar 29 '12 at 11:43

4 Answers4

3

Send it on the 1st of the month instead :)

Mike
  • 628
  • 10
  • 22
3

Simply use the DateTime class which is perfect for this:

If I want the date 1 month from 29/2/2012:

$date = new DateTime('2012-02-29'); // "start date"
$date->add(new DateInterval('P1M')); //one month later
echo $date->format('Y-m-d') . "\n"; //2012-03-29

And a year later:

$date = new DateTime('2012-02-29'); //start
$date->add(new DateInterval('P1Y')); //one year later
echo $date->format('Y-m-d') . "\n"; //2013-03-01 

Using this, you can easily caculate any date intervals you require.

F21
  • 32,163
  • 26
  • 99
  • 170
2

I would test the date before saving it in the database.

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'));
?>

Source: http://www.johnboy.com/blog/find-the-last-day-of-the-month-with-php

Lohardt
  • 1,057
  • 1
  • 12
  • 26
1

When the script runs check the actual date if its the last day of the month and act according to the result.

floriank
  • 25,546
  • 9
  • 42
  • 66