views:

19

answers:

1

I need a bit of help using symfony to calculate an end date. The user will select a start date, a frequency (Ex. Every 7 Days, Every 14 Days, Every Month, Semi-Annually - Every 6 Months, etc), and the number of occurrences.

Examples:

Start date: 08/01/2010
Frequency: Every 7 days
Occurrences: 4
End Date = 08/29/2010

Start date: 08/01/2010
Frequency: Every Month
Occurrences: 3
End Date = 11/01/2010

Some notes- Our server is running PHP 5.2.13 and Symfony 1.1. It's not a requirement that I use symfony to adjust the date, as long as I can get it back to a format that symfony can validate.

Thanks in advance.

+4  A: 

You can use a strtotime for this, e.g.

echo date("Y-m-d", strtotime("+1 month", strtotime("2010-08-06")));

gives 2010-09-06.

Of course, you will have to multiply "+1 month" for the number of occurrences, so that it becomes e.g. "+7 months" for 7 occurrences.

Alternatively (more simple but less efficient), you can use a fixed interval (e.g. "+1 month") and use a for loop:

$end = $start = strtotime("2010-08-06");
for ($i = 1; $i <= $occurrences; $i++) {
    $end = strtotime("+1 month", $end);
}
Artefacto
And how would I get the result back to the format of $end['year'], $end['month'], $end['day']?
clang1234
@clang `$end = array('day'=>date("d", $end), 'month'=>...)`
Artefacto
Wonderful. Thanks for the quick help.
clang1234