PHP code example of apxcde / loan-amortization

1. Go to this page and download the library: Download apxcde/loan-amortization library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apxcde / loan-amortization example snippets


use Apxcde\LoanAmortization\LoanAmortization;

$loanAmount = 200000;
$termMonths = 60;
$annualInterestRate = 12;

$loanData = [
    'loan_amount' => $loanAmount,
    'interest' => $annualInterestRate,
    'term_months' => $termMonths,
    'starting_date' => new \DateTime('2024-01-01'),
    'remaining_months' => $termMonths, // Set to $termMonths for new loan, or less if partially paid
];

$loan = new LoanAmortization($loanData);

// Get all results (summary and schedule)
$results = $loan->getResults();

// Access summary
echo "Monthly Payment: $" . number_format($results['summary']['monthly_repayment'], 2) . "\n";
echo "Total Interest: $" . number_format($results['summary']['total_interest'], 2) . "\n";
echo "Total Payment: $" . number_format($results['summary']['total_pay'], 2) . "\n";

// Access payment schedule
foreach ($results['schedule'] as $payment) {
    [$status, $details] = $payment;
    echo sprintf(
        "%s - Date: %s, Payment: $%.2f, Principal: $%.2f, Interest: $%.2f, Balance: $%.2f\n",
        strtoupper($status),
        $details['date'],
        $details['payment'],
        $details['principal'],
        $details['interest'],
        $details['balance']
    );
}

$loanData = [
    'loan_amount' => 200000,
    'interest' => 12,
    'term_months' => 60,
    'starting_date' => new \DateTime('2024-01-01'),
    'remaining_months' => 36, // 24 months already paid, 36 remaining
];

$loan = new LoanAmortization($loanData);
$results = $loan->getResults();

// The schedule will show 24 months as 'paid' and 36 as 'not_paid'