简体   繁体   中英

PHP get continue date next month and get price loan every month with interest pay on first month

I'm continuing with my previous question .

Now I need to set loan every month with price value.

The price value is get from totalLoan * 5%

Example:

Total loan = 4,000,000 IDR format
Total month loan = 4
Interest = 5%

So We get, IDR 4,200,000

Now I need to set the interest 5% bill in the first month.

To be like this:

28-Apr-2019 - 1,200,000
28-May-2019 - 1,000,000
28-Jun-2019 - 1,000,000
28-Jul-2019 - 1,000,000

And the PHP like this below so far:

$totalLoan = "4000000";
$interest= "5"; //<-- 5%
$loan = 4;
$fixedDateEveryMonth = 28;
$start = new DateTime(date('Y-m-') . $fixedDateEveryMonth);
for($i = 1; $i <= $loan; $i++)
{
    $start->add(new DateInterval("P1M"));
    $dateLoan = $start->format('Y-m-d');

    echo $dateLoan." = ".$priceLoan; //<-- assume
}

How to do that?

$totalLoan = "4000000";
$interest= "5"; //<-- 5%
$loan = 4;
$fixedDateEveryMonth = 28;
$start = new DateTime(date('Y-m-') . $fixedDateEveryMonth);
$everyMonthPriceLoan = $totalLoan/$loan;

for($i = 1; $i <= $loan; $i++)
{
    $start->add(new DateInterval("P1M"));
    $dateLoan = $start->format('Y-m-d');
    $priceLoan = $everyMonthPriceLoan + ($totalLoan/100*$interest);

    echo $dateLoan." = ".$priceLoan; //<-- assume
    $interest = 0;
}

You could try this. It computes the loan instalments as the totalLoan divided by the number of loan periods, then adds the total interest (totalLoan * interest / 100) to the first instalment; resetting the instalment value after the first payment:

$totalLoan = "4000000";
$interest= "5"; //<-- 5%
$loan = 4;
$instalment = $totalLoan / $loan;
$priceLoan = $instalment + $totalLoan * $interest / 100;
$fixedDateEveryMonth = 28;
$start = new DateTime(date('Y-m-') . $fixedDateEveryMonth);
for ($i = 1; $i <= $loan; $i++) {
    $start->add(new DateInterval("P1M"));
    $dateLoan = $start->format('Y-m-d');
    echo "$dateLoan - " . number_format($priceLoan, 0) . "\n";
    $priceLoan = $instalment;
}

Output:

28-Apr-2019 - 1,200,000
28-May-2019 - 1,000,000 
28-Jun-2019 - 1,000,000 
28-Jul-2019 - 1,000,000

Demo on 3v4l.org

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM