摊销脚本

时间:2017-04-07 00:53:20

标签: php

我正在尝试创建一个计算下降本金的摊销计算器

$x = 1;

$starting_pmt = 26;

$ending_pmt = 36;

$i = 0.0010316264327892;

$p = 410000;
$pmt = 916.84;

$num_pmts = $ending_pmt - $starting_pmt;



echo "<table border=\"1\" align=\"center\">";
echo "<tr><th>PMT Num</th>";
echo "<th>Balance</th>";
echo "<th>Principle</th>";
echo "<th>TTL Principle</th>";
echo "<th>Interest</th>";
echo "<th>Payment</th>";

echo "</tr>";
while ( $starting_pmt <= $ending_pmt ) {
    echo "<tr><td>";
    echo $starting_pmt;
    echo "</td><td>";
    echo $p;
    echo "</td>";
    echo "<td>$prin</td>";
    echo "<td>$TTLprin</td>";
    echo "<td>$interest</td>"; 
    echo "<td>$TTLPmt</td> </tr>";

    $starting_pmt = $starting_pmt + 1;

    $p = $p -($p*$i);
    $prin = $pmt - ($p * $i);
    $interest = $pmt - $prin;
    $TTLPmt = $prin + $interest;
    //$cumTTL = $$pmt - ($p * $i);
    $TTLprin = $prin + $prin;
}
echo "</table>";
?>

每笔付款的余额以及两个值之间支付的总原则。我还想计算每笔付款的利息和两个价值之间的累计利息。

这就是我的开始。

我无法弄清楚如何在每次迭代期间让循环进行数学运算...............我一直在处理我的原始代码而且我越来越近了。我无法得到循环来计算本金和利息的累计总额。

<?php
$starting_pmt = 1;

$ending_pmt = 10;

$i = 0.0010316264327892;

$p = 410000;
$pmt = 916.84;

$num_pmts = $ending_pmt - $starting_pmt;

while($x < $num_pmts) {

    echo "<tr>";
    echo "<td>$x</td>";
    echo "<td>".$p - ($p*$i)."</td>";
    echo "<td></td>";
    echo "<td></td>";
    echo "<tr>";

    $x++;

我得到了数学。它正在通过while循环来完成我需要的工作。它没有计算支付原则的累计总数。

1 个答案:

答案 0 :(得分:0)

这是一种制作摊销脚本的方法,您需要将您想要的费率,付款日期和所有好东西都传回去。然后我返回一个关联数组,其中包含每个月的所有值。

public static function amortizeLoan($principal, $rate, $term, $extra_days, $payment_date){
    $payment = self::getPayment($principal,($rate*100),$term,$extra_days);

    $MonthlyInterestRate = $rate / 12;
    $Multiplier = 1 + $MonthlyInterestRate;
    $TermRate = 1;
    for($x=0;$x<$term;$x++){
        $TermRate = $TermRate * $Multiplier;
    }
    $Days = $extra_days - 30;
    $oddDayInt = $Days > 0 ? round($principal * ($rate / 365.25) * $Days,2) : 0;

    $amortization = [];
    $total_interest = 0;
    $total_principal = 0;
    $remaining_principal = $principal;
    $compareDay = date('d', strtotime($payment_date));



    for($x=0;$x<$term;$x++){
        $this_int_paid = round((($rate)/12) * $remaining_principal,2);
        if($x==0){$this_int_paid += $oddDayInt;}
        $this_princ_paid = round($payment - $this_int_paid,2);
        $this_rem_princ = round($remaining_principal - $this_princ_paid,2);
        if($x == ($term-1)){
            if($this_rem_princ < 0){
                $this_princ_paid-=abs($this_rem_princ);
                $payment=round($this_int_paid+$this_princ_paid,2);
            }else{
                $this_princ_paid+=$this_rem_princ;
                $payment=round($this_int_paid+$this_princ_paid,2);
            }
        }
        $remaining_principal -= $this_princ_paid;
        $total_interest += $this_int_paid;
        $total_principal += $this_princ_paid;

        // *** If month days < payment date, payment date falls on the last day of that month.***
        $nextMonth = strtotime("last day of next month", strtotime($payment_date));
        $lastDayNextMonth = date('d', $nextMonth);

        if ($compareDay >= $lastDayNextMonth) {
            $payment_date_time = $x ? $nextMonth : strtotime($payment_date);
        } else {
            $payment_date_time = $x ? strtotime("+1 month", strtotime($payment_date)) : strtotime($payment_date);
        }

        if ($lastDayNextMonth > date('d', strtotime($payment_date)) && date('t', strtotime($payment_date)) < $compareDay) {
            $date = date('Y-m-d', $nextMonth);
            $newDate = date_create($date);
            $newDate->format('Y-m-d');
            $finalDate = date_date_set(
                $newDate,
                date_format($newDate, 'Y'),
                date_format($newDate, 'm'),
                $compareDay
            );
            $formatedDate = $finalDate->format('m/d/y');

            $payment_date_time = strtotime($formatedDate);
        }
        $payment_date = gmdate("Y-m-d", $payment_date_time);

        $amortization[] = [
            "payment"=>($x+1),
            "amount"=>$payment,
            "principal"=>$this_princ_paid,
            "interest"=>$this_int_paid,
            "total_principal"=>$total_principal,
            "total_interest"=>$total_interest,
            "remaining_balance"=>round($remaining_principal,2),
            "payment_date"=>$payment_date
        ];
    }
    return $amortization;
}