0

I have the following function snippets which calculate the bi-monthly payment schedule. I need to be able to ready the values as individual array element to be inserted into MYSQL database. How do I achieve this?

function calculatePaymentSchedule($d1, $months){
    $date = new DateTime($d1);

    // call second function to add the months
    $newDate = $date->add(addMonths($months, $date));

    //formats final date to Y-m-d form
    $dateReturned = $newDate->format('Y-m-d'); 

    return $dateReturned;
}

$startDate = '2018-5-15'; 
$duration = 12;
$paymentCount = $duration + 2;

for($i =0; $i < $paymentCount; $i++){
    if($i % 2 == 0 && $i != 0){
        $final = calculatePaymentSchedule($startDate, $i);
        echo $final.'<br>';
    }
}

current state output:

2018-07-15
2018-09-15
2018-11-15
2019-01-15
2019-03-15
2019-05-15
2
  • Use $final[] = calculatePaymentSchedule($startDate, $i); so you fill the $final array with the individual results and reference the array elements for inserts into your database. Commented Jul 12, 2018 at 14:06
  • Already did that. First element resulted in 2018-07-15 2018-07-15 2018-07-15 2018-07-15 2018-07-15 2018-07-15 instead of 2018-07-15. Commented Jul 12, 2018 at 14:15

1 Answer 1

1

initalize array at top:

all_dates = array();

in for loop :

$final = calculatePaymentSchedule($startDate, $i);
array_push($all_dates, $final);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Lukas. Your solution is already implemented; it worked. You deserve +1
I'm glad i cloud help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.