I have following function which should just add number of weeks to the start date and return the end date, however i get wrong end date for values greater than 20 ,
for example the
start date='2013-05-30'
interval_term=24;
will return as
end date: 2013-11-07
function is like below,
public function calculateArrangementEndDate($interval_term=12,$start_date=false)
{
$end_date=($start_date===false)?time():strtotime($start_date);
return date("Y-m-d",($end_date+($interval_term-1)*3600*7*24));
}
but the correct 24 weeks from the start_date is 2013-10-31
like this
Payment Schedule:
Due Date Amount
30/05/2013 $0.64
06/06/2013 $0.64
13/06/2013 $0.64
20/06/2013 $0.64
27/06/2013 $0.64
04/07/2013 $0.64
11/07/2013 $0.64
18/07/2013 $0.64
25/07/2013 $0.64
01/08/2013 $0.64
08/08/2013 $0.64
15/08/2013 $0.64
22/08/2013 $0.64
29/08/2013 $0.64
05/09/2013 $0.64
12/09/2013 $0.64
19/09/2013 $0.64
26/09/2013 $0.64
03/10/2013 $0.64
10/10/2013 $0.64
17/10/2013 $0.64
24/10/2013 $0.64
31/10/2013 $0.64
I though that the easy way is using strtotime to add this 24 weeks instead use
$enddate = strtotime('2013-05-30 00:00:00 +24 weeks');
var_dump(date('Y-m-d H:i:s', $enddate));
Will return "2013-11-14 00:00:00"
If you try with 2 weeks for easy test you will get 2013-06-13 00:00:00 and this is exactly 14 days more.
I hope it helps.
Firstly you need to understand the difference between your start date and the first due date - is the first one counted in the 24 months? If so, then do you want the term to actually be 23 months? 24 weeks after 30/05/2013 is indeed 14/11/2013.
You also have only 23 dates listed in your payment schedule (but starting from the start date, not using that as the first due date, if that makes sense).
Finally, I'd suggest using the DateTime
class instead. Here's an example:
public function calculateArrangementEndDate($interval_term=12,$start_date=false)
{
$date = new DateTime($start_date);
if (is_null($date)) {
// throw exception or return false here
}
$date->add(new DateInterval('P' . $interval_term . 'W'));
return $date->format("Y-m-d");
}
This will work out the date $interval_term
weeks from $start_date
- the first two points remain.
var_dump(
calculateArrangementEndDate(23, '2013-05-30')
);
// string(10) "2013-11-07"