PHPRO.ORG

This function is the anitithesis of the Calculate Installment Payment Regime. This function will calulate the installment payment amounts for an amount, given the amount and the number of installments to make. This function will also calculate and return the amount of the final installment, which is always slightly lower than the regular installment amounts.


<?php
    
/**
     *
     * @calculate the installment amounts required to pay an amount
     * @given the amount and the number of installments
     *
     * @param float $amount The total amount to pay
     *
     * @param int $num_installments The number of installments to make
     *
     * @return array
     *
     */
    
function calcInstallmentAmounts($amount$num_installments)
    {
        
/*** check if the payments are exact ***/
        
if($amount $num_installments == 0)
        {
            
/*** if payments are exact, just some simple math ***/
            
$installment_amount $amount/$num_installments;
            
$final_installment $installment_amount;
        }
        else
        {
            
/*** get the installment percentage of amount ***/
            
$percent ceil(100/$num_installments)+1;

            
/*** calculate the installment amounts ***/
            
$installment_amount floor($amount*$percent/100);

            
/*** initialize the final payment amount ***/
            
$final_installment $amount;

            
/*** calculate the last payment ***/
            
while($final_installment $installment_amount)
            {
                
$final_installment -= $installment_amount;
            }
        }
        return array(
'installment_amount'=>$installment_amount'final_installment'=>$final_installment);
    }
?>

Example Usage


<?php
    $info 
calcInstallmentAmounts(1234.404);

    echo 
'Installment amount: '.$info['installment_amount'].'. Final Installment: '.$info['final_installment'];
?>

?>

Demonstration

Installment amount: 320. Final Installment: 274.4