PHP Classes

Use a PHP Finance Library to Perform Calculations with Interest Rates Using the Package PHP Math Finance: Perform math operations for financial applications

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-06-03 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
mathfinance 4.0.0Custom (specified...5Algorithms, PHP 5, Finances, Math
Description 

Author

This package can perform mathematical operations for financial applications.

It provides a bond accrued interest class based on rhossis/mathfinance package that provides setter functions to set parameters values and then performs several types of financial math operations. Currently, it can calculate:

- Accrued interest factor

- Compare dates

- Get the last day of a given month of a given year

- Add a number of months to a given month or year

It also provides a class based on the Alejandro Pedraza Math Finance class that can perform other types of financial math operations. Currently it can calculate:

- The number of between two dates

- The number of days in a given year

- Depreciation fixed declining

- Depreciation straight line

- Discount rate

- Effective rate

- Future value

- Interest and principal rate

- Interest payment

- Internal rate of return

- Modified internal rate of return

- Net present value

- Nominal rate

- Payment

- Periods

- Present value

- Price discount

- Principal payment

- Rate

- Bill equivalent yield

- Bill price

- Bill yield

Picture of Cyril Ogana
  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

 

Instructions

Documentation

? math

Package for financial math functions

? Description

This package contains assorted methods for fixed income instrument calculations, enabling developers to perform accurate and reliable financial math operations such as interest calculations, depreciation, bond valuation, and more.

? Installing

? Install application via Composer

require "rhossis/mathfinance" : "^4.0.0"

? Usage

? Overview

The math class provides several financial math functions. These methods largely mirror Microsoft Excel functions and extend the legacy PEAR math classes. The package also comes with a bond interest calculation class, which is compliant with interest calculations used in various exchanges worldwide, such as Central Bank of Kenyas 364 days calculation.

? Named Constants

// Precision level
define('MATHFINANCE_PRECISION', 1E-6);

// Payment types
define('MATHFINANCE_PAYEND', 0);
define('MATHFINANCE_PAYBEGIN', 1);

// Day count methods
define('MATHFINANCE_COUNTNASD', 0);
define('MATHFINANCE_COUNTACTUALACTUAL', 1);
define('MATHFINANCE_COUNTACTUAL360', 2);
define('MATHFINANCE_COUNTACTUAL365', 3);
define('MATHFINANCE_COUNTEUROPEAN', 4);

// Extended day count methods
define('MATHFINANCE_COUNTNEW_GERMAN', 1);
define('MATHFINANCE_COUNTNEW_GERMANSPEC', 2);
define('MATHFINANCE_COUNTNEW_ENGLISH', 3);
define('MATHFINANCE_COUNTNEW_FRENCH', 4);
define('MATHFINANCE_COUNTNEW_US', 5);
define('MATHFINANCE_COUNTNEW_ISMAYEAR', 6);
define('MATHFINANCE_COUNTNEW_ISMA99N', 7);
define('MATHFINANCE_COUNTNEW_ISMA99U', 8);

// Error flags
define('MATHFINANCE_ERROR_BADDCM', 1);
define('MATHFINANCE_ERROR_BADDATES', 2);

// Accrued interest calculation constants
define('MATHFINANCE_SWX_BOND_AI_GERMAN', 1);
define('MATHFINANCE_SWX_BOND_AI_SPEC_GERMAN', 2);
define('MATHFINANCE_SWX_BOND_AI_ENGLISH', 3);
define('MATHFINANCE_SWX_BOND_AI_FRENCH', 4);
define('MATHFINANCE_SWX_BOND_AI_US', 5);
define('MATHFINANCE_SWX_BOND_AI_ISMA_YEAR', 6);
define('MATHFINANCE_SWX_BOND_AI_ISMA_99N', 7);
define('MATHFINANCE_SWX_BOND_AI_ISMA_99U', 8);
define('MATHFINANCE_SWX_BOND_AI_KENYA', 9);
define('MATHFINANCE_SWX_BOND_AI_CBK_KENYA', 10);

? Using the Math Package

? Calculating Accrued Interest on Bonds

use rhossis\mathfinance\MathFinance\abstractclass\Swx\BondAccruedInterest;

$d1M1Y1 = '2013-12-31';
$d2M2Y2 = '2014-06-30';
$d3M3Y3 = '2014-12-31';
$fValue = 1;
$maturityDate = '2014-12-31';

$bondObj = new BondAccruedInterest();
$dCM = MATHFINANCE_SWX_BOND_AI_SPEC_GERMAN;

$interestFactor = $bondObj->accruedInterestFactor($dCM, $d1M1Y1, $d2M2Y2, $d3M3Y3, $fValue, $maturityDate);

? Bond Interest Calculation Guide

> A detailed guide to the formulas and logic for computing bond interest for fixed income instruments including actual/actual, 30/360, and RFR-based methods.

? View Full Guide

? Computing Financial Math

$mfo = new rhossis\mathfinance\MathFinance();
$mfo->effectiveRate($nominal_rate, $npery);
$mfo->nominalRate($effect_rate, $npery);
$mfo->presentValue($rate, $nper, $pmt, $fv = 0, $type = 0);
$mfo->futureValue($rate, $nper, $pmt, $pv = 0, $type = 0);
$mfo->payment($rate, $nper, $pv, $fv = 0, $type = 0);
$mfo->periods($rate, $pmt, $pv, $fv = 0, $type = 0);
$mfo->rate($nper, $pmt, $pv, $fv = 0, $type = 0, $guess = 0.1);
$mfo->interestPayment($rate, $per, $nper, $pv, $fv = 0, $type = 0);
$mfo->principalPayment($rate, $per, $nper, $pv, $fv = 0, $type = 0);
$mfo->interestAndPrincipal($rate, $per, $nper, $pv, $fv, $type);
$mfo->netPresentValue($rate, $values);
$mfo->internalRateOfReturn($values, $guess = 0.1);
$mfo->modifiedInternalRateOfReturn($values, $finance_rate, $reinvest_rate);
$mfo->daysDifference($date1, $date2, $basis);
$mfo->daysPerYear($year, $basis);
$mfo->tBillYield($settlement, $maturity, $pr);
$mfo->tBillPrice($settlement, $maturity, $discount);
$mfo->tBillEquivalentYield($settlement, $maturity, $discount);
$mfo->discountRate($settlement, $maturity, $pr, $redemption, $basis = 0);
$mfo->priceDiscount($settlement, $maturity, $discount, $redemption, $basis = 0);
$mfo->depreciationFixedDeclining($cost, $salvage, $life, $period, $month = 12);
$mfo->depreciationStraightLine($cost, $salvage, $life);

? Testing

Unit tests are provided using PHPUnit.

? Contribute

  • Fork the project :)
  • Email [@rhossis].
  • Contributors will be credited in the project.

?? License

BSD-3-Clause


  Files folder image Files (18)  
File Role Description
Files folder imagesrc (4 directories)
Files folder imagetests (2 directories)
Accessible without login Plain text file BOND_INTEREST_CALCULATION_GUIDE.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0