Loan EMI Calculator and Payment Schedule Generator using PHP

Loans are an integral part of human life in the present scenario. We all have been utilizing the services of loans for various purposes like buying a house, starting our own business or even buying cars or vehicles. The loan we receive has to be paid back which is achieved by diving the payment amounts over a period of time.

Whenever we talk about loans, a special term has to be introduced namely EMI. EMI stands for Equated Monthly Installment.

What is EMI?

EMI is a fixed payment amount is made by a borrower to a lender at a specified date each calendar month. Equated monthly installments are used to pay off both interest and principal each month so that over a specified number of years, the loan is fully paid off along with interest.”

How is EMI calculated?

The mathematical formula to calculate EMI is:

 

PHP Function to Calculate EMI

function PMT($interest,$period,$loan_amount){
  $interest = (float)$interest;
  $period = (float)$period;
  $loan_amount = (float)$loan_amount;
  $period = $period * 12;
  $interest = $interest / 1200;
  $amount = $interest * -$loan_amount * pow((1+$interest),$period) / (1 - pow((1+$interest), $period));
  return $amount;
}

Complete Source Code

index.php

<?php include_once('functions.php');?>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.0/css/bootstrap.min.css">

<style type="text/css">
  body{
    background-color: #a8d8cd;
  }
  .container{
    margin-top:30px;
  }
  .box{
    background-color: aliceblue;
    padding:30px;
    margin-bottom:20px;
  }
  .form-group{
    margin-bottom:20px;
    margin-top:20px;
  }
</style>

<?php
  if(isset($_POST['amount'])){
    $_POST['amount'] = str_replace(',','',$_POST['amount']);
    $emi = PMT($_POST['interest'],$_POST['period'],$_POST['amount']);
    $balance = $_POST['amount'];
    $payment_date = $_POST['start_date'];
  }
?>

<div class="container">
  <h1 class="text-center">Loan EMI Calculator and Schedule Generator</h1>
  <hr>
  <div class="box">
    <h3 class="text-center">Loan Details</h3>
    <hr>
    <form action="" method="post">
      <div class="row">
        <div class="col-6">
          <div class="form-group">
            <label>Loan Amount</label>
            <input type="text" name="amount" class="form-control" value="<?php echo number_format(@$_POST['amount'],0);?>" required>
          </div>
          <div class="form-group">
            <label>Interest (%)</label>
            <input type="text" name="interest" class="form-control" value="<?php echo @$_POST['interest'];?>" required>
          </div>
        </div>
        <div class="col-6">
          <div class="form-group">
            <label>Period (Years)</label>
            <input type="text" name="period" class="form-control" value="<?php echo @$_POST['period'];?>" required>
          </div>
          <div class="form-group">
            <label>Start Date</label>
            <input type="date" name="start_date" value="<?php echo @$_POST['start_date'];?>" class="form-control" required>
          </div>
        </div>
      </div>
      <input type="submit" class="btn btn-primary" value="Calculate">
    </form>
  </div>

  <div class="box">
    <div class="row">
      <div class="col-12">
        <div class="alert alert-info" role="alert">
          Monthly EMI Payment for the loan details amounts to <?php echo showValue($emi);?>
        </div>
        <h3 class="text-center">EMI Schedule</h3>
        <hr>
        <table class="table table-striped table-bordered">
          <thead>
            <tr>
              <th>SN</th>
              <th>Payment Date</th>
              <th>Monthly EMI</th>
              <th>Interest Paid</th>
              <th>Principal Paid</th>
              <th>Balance</th>
            </tr>
          </thead>
          <tbody>
            <?php for($i=1;$i<=$_POST['period']*12;$i++){?>
              <?php 
                $interest = (($_POST['interest']/100)*$balance)/12;
                $principal = $emi - $interest;
                $balance = $balance - $principal;
                $payment_date = date('Y-m-d',strtotime("+1 month",strtotime($payment_date)));
              ?>
              <tr>
                <td><?php echo $i;?></td>
                <td><?php showDate($payment_date);?></td>
                <td><?php showValue($emi);?></td>
                <td><?php showValue($interest);?></td>
                <td><?php showValue($principal);?></td>
                <td><?php showValue($balance);?></td>
              </tr>
            <?php } ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>

</div>


functions.php

<?php

function PMT($interest,$period,$loan_amount){
  $interest = (float)$interest;
  $period = (float)$period;
  $loan_amount = (float)$loan_amount;
  $period = $period * 12;
  $interest = $interest / 1200;
  $amount = $interest * -$loan_amount * pow((1+$interest),$period) / (1 - pow((1+$interest), $period));
  return $amount;
}

function showValue($value){
  echo number_format($value,2);
}

function showDate($date){
  echo date('jS F, Y',strtotime($date));
}

Follow this video for complete guidance :

 

Recommended For You

About the Author: Ritesh Ghimire

Ritesh is a Computer Engineer, Web Developer and an active cricket follower.