开发者

Calculating an End Date

开发者 https://www.devze.com 2023-01-10 06:31 出处:网络
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 Day开发者_如何学Gos, Every Month, Semi-Annually - Every 6 Months

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 Day开发者_如何学Gos, 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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消