Codebank

Here you’ll find all the code ever posted on TimSpangler.com in one convenient, easy-to-copy place.

money.php - Calculate your compounded wealth

With a few simple parameter changes, you can see the glory of compounding over the course of your lifetime. With an average return of 10% (when invested with a moderate degree of risk), $100 now will be nearly $15,000 in 50 years. Is it worth it to horde it all?

Usage: php money.php from the commandline, or can be run from a server.

<?php
define("START_SUM", 1); // how much money are you starting with?
define("START_YEAR", 2008); // beginning in what year?
define("DOB", 1900); // when were you born?
define("RATE", .10); // 10% ROI / year
define("YEARS", 70); // how many years to run predictions for
 
setlocale(LC_MONETARY, 'en_US'); // so PHP formats money properly
 
for($x = 0; $x < YEARS; $x++)
  {
      // At a return of 10%, your money should approximately double every seven years. Let's mark it.
      if(($x % 7) == 0) {
          echo "-------------------\\n";
      }
 
      $theYear = START_YEAR + $x;
          if($x == 0) { // if first year
              $thisYear = START_SUM;
          }
 
          $thisYear = (RATE * $thisYear) + $thisYear;
          echo $theYear . ": " . money_format('%i', $thisYear) . "     (age " . ($theYear - DOB) . ")\\n";
 
      if(($x % 7) == 0) {
          echo "-------------------\\n";
      }
 
}
?>