gpt4 book ai didi

php - 如何将 PHP 函数读入数组

转载 作者:行者123 更新时间:2023-11-29 10:09:15 25 4
gpt4 key购买 nike

我有以下函数片段,用于计算双月付款计划。我需要能够将这些值作为单独的数组元素准备好插入到 MYSQL 数据库中。我如何实现这一目标?

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>';
}
}

当前状态输出:

2018-07-15
2018-09-15
2018-11-15
2019-01-15
2019-03-15
2019-05-15

最佳答案

在顶部初始化数组:

all_dates = array();

在 for 循环中:

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

关于php - 如何将 PHP 函数读入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51307594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com