PHPRO.ORG

This PHP function calculates the number of installments required to pay a given amount. It will also calculate the last payment which is always lower than the regular installments. This PHP function can be useful when a payment plan such as lay-by/lay-away is required or just when Imran owes you money. The number of installements is the total number of installments including the last payment.


<?php
    
/**
     *
     * @Calculate the installment regime for a given amount and payment amount
     *
     * @param float $amount The total amount to pay
     *
     * @param float $payment_amount The amount of installment payments
     *
     * @return array
     *
     */
    
function installmentRegime($amount$installments)
    {
        
/*** initialize number of payments required ***/
        
$num_installments 1;

        
/*** initialize the amount of the last payment ***/
        
$last_payment $amount;

        
/*** calculate the number of payments and the amount of the last payment ***/
        
while($last_payment $installments)
        {
            
/*** subtract from last payment ***/
            
$last_payment -= $installments;
            
$num_installments++;
        }

        
/*** return array has number of payments plus the amount of the final payment ***/
        
return array('num_installments'=>$num_installments'last_payment'=>$last_payment);
    }
?>

Example Usage


<?php
    
/*** amount to pay ***/
    
$amount 1234.31;

    
/*** amount of installments ***/
    
$installments 12.50;

    
$info installmentRegime($amount$installments);

    echo 
$info['num_installments'].' installments. Last installment is '.$info['last_payment'];
?>

Demonstration

99 installments. Last installment is 9.3099999999999