duanjia4097 2018-07-12 13:58
浏览 49

如何将PHP函数读入数组

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
  • 写回答

1条回答 默认 最新

  • dongpian4954 2018-07-12 14:33
    关注

    initalize array at top:

    all_dates = array();
    

    in for loop :

    $final = calculatePaymentSchedule($startDate, $i);
    array_push($all_dates, $final);
    
    评论

报告相同问题?