Next month, same day in PHP


I have an "event" that needs to be scheduled the same day of every month.

Say you set the start date on the 1st May you should get the next events on the 1st of Jun, 1 Jul etc. The problem comes with a start date on the 31st (the next ones could be 30 or 28 depending on the month).

Considering that there are months with different numbers of days (28, 30, 31) depending on the month itself and the year... what would be an easy way to setup this?

Consider the following (and flawed) nextmonth function:

$events = array()

function nextmonth($date) {
   return $date+(60*60*24*30);
$curr = $start;
while($curr < $end) {
    $events[ = $curr;
    $curr = nextmonth($curr);

Edited to add: The problem for me is, simply enough, how to solve what the number of days of any given month is and thus get the next corresponding date.

8/6/2015 5:52:21 AM


This will give you the number of days in given month:

echo date('t', $timestamp);

See: date()

Old answer:

I'm not sure about the algorithm you're thinking of but I believe this will help you:

echo date('d-M-y', strtotime('next month'));
6/5/2009 10:33:14 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow