Jump to content

Recommended Posts

I tried using a for loop but I'm trying to think of a way to calculate interest for a number of years but after I have the amount of interest for one year I'm unsure of how to add the interest to the principle then calculate it again for the next year, to hold that number like $Principle + $Interest = $NewPrinciple then $NewPrinciple + $Interest for multiple years.  

<?php
if (isset($_POST['submit'])) {
	$InvestmentFrequency = $_POST['InvestmentFrequency'];
	$CompoundFrequency = $_POST['CompoundFrequency'];
	$InitialInvestment = preg_replace("/[^0-9]/", '', $_POST['InitialInvestment']);
	$Contributions = preg_replace("/[^0-9]/", '', $_POST['Contributions']);
	$InvestmentLength = preg_replace("/[^0-9]/", '', $_POST['InvestmentLength']);
	$InterestRate =  $_POST['InterestRate'];
	
	if ($InvestmentFrequency == "Weekly") {
	$InvestmentFrequency = "52";
	} elseif ($InvestmentFrequency == "Monthly") {
	$InvestmentFrequency = "12";
	} else {
	$InvestmentFrequency = "1";
	}
	
	if ($CompoundFrequency == "Annually") {
	$CompoundFrequency = "1";
	} elseif ($CompoundFrequency == "Semiannually") {
	$CompoundFrequency = "2";
	} elseif ($CompoundFrequency == "Monthly") {
	$CompoundFrequency = "12";
	} elseif ($CompoundFrequency == "Weekly") {
	$CompoundFrequency = "52";
	} else {
	$CompoundFrequency = "365";
	}
	
	if ($CompoundFrequency == "1") {
	$AdditionalInvestments = $Contributions * $InvestmentFrequency;
	$Principle = $InitialInvestment + $AdditionalInvestments;
	$Interest = $Principle * $InterestRate;
	$NewPrinciple = $Principle + $Interest;
	
		for ($x = 0; $x <= $InvestmentLength; $x++){
			echo "$x - $NewPrinciple</br>";
		}
	}
	
}

This is the output I get the principle and interest added together for the number of years I entered.

fff.png

Edited by dataxspy
Link to comment
https://forums.phpfreaks.com/topic/311228-calculating-compound-interest/
Share on other sites

Adapt the standard compound interest formula:

A=P(1+r/n)^(nt)

Where:

A= amount

P=principle

r= interest rate

n= number of compoundings per year

t= number of time periods

For continuous compounding use:

A=Pe^(rt)

Where:

t=time in years (can be a fraction)

e= mathematical constant (2.71828)

Edited by gw1500se
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.