tags:

views:

39

answers:

1

I am trying to find the right string to modify a DateTime object. I have a recurring calendar object:

Currently finding 'this date of next month' is easy enough: $start_date->modify('+1 month');

However, I am also looking to find 'this day of next month' and 'this day of next year'.

Couldn't find the relevant strings. Cheers

EDIT:

So for example if we take the 2010-09-21 as the start date:

$start_date = new DateTime(20100921);

Finding the current DATE of the next month (numerical representation) would be:

$start_date->modify('+1 month');

However finding the current DAY of the next month (textual representation) is causing me more troubles.

This date is the third tuesday of this month - next month the third tuesday is the 19th
+1  A: 

You could do something like this for the 'same day next month':

<?php

switch(floor(date("d") / 7)) {
  case 0:
    $which = "first";
    break;
  case 1:
    $which = "second";
    break;
  case 2:
    $which = "third";
    break;
  case 3:
    $which = "fourth";
    break;
  case 4:
    $which = "fifth";
    break;
}

echo date("c", strtotime(sprintf("%s %s of +1 month", $which, date("l"))));

I dunno if the same works for 'Same day next year' (fiftysecond thuesday of +1 year), but you could also calculate it if you know which day the year starts with and then use mktime().

bouke
Thanks, that seems like a good plan
kalpaitch