PHPackages                             tomwright/currency-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tomwright/currency-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tomwright/currency-php
======================

A small package to facilitate currency conversions in PHP.

0.0.4(8y ago)0197PHP

Since Feb 2Pushed 8y ago1 watchersCompare

[ Source](https://github.com/TomWright/CurrencyPHP)[ Packagist](https://packagist.org/packages/tomwright/currency-php)[ RSS](/packages/tomwright-currency-php/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

CurrencyPHP
===========

[](#currencyphp)

[![Build Status](https://camo.githubusercontent.com/56348bf28008b04494ca3a9f7a66f430fb56521c3a2627aad919bfeb341ad53e/68747470733a2f2f7472617669732d63692e6f72672f546f6d5772696768742f43757272656e63795048502e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/TomWright/CurrencyPHP)[![Latest Stable Version](https://camo.githubusercontent.com/cd29ddda8330275de10e954622dc2b790914c8744b223dbf10972b4ae652a7b2/68747470733a2f2f706f7365722e707567782e6f72672f746f6d7772696768742f63757272656e63792d7068702f762f737461626c65)](https://packagist.org/packages/tomwright/currency-php)[![Total Downloads](https://camo.githubusercontent.com/5db8890a99e7a530278f65331a3e9d651e7262cc96b1366283108a84903a5938/68747470733a2f2f706f7365722e707567782e6f72672f746f6d7772696768742f63757272656e63792d7068702f646f776e6c6f616473)](https://packagist.org/packages/tomwright/currency-php)[![Monthly Downloads](https://camo.githubusercontent.com/3ae795566265999742c61987d6d7a83af85e26065cacb32fdc6c26d6782901b0/68747470733a2f2f706f7365722e707567782e6f72672f746f6d7772696768742f63757272656e63792d7068702f642f6d6f6e74686c79)](https://packagist.org/packages/tomwright/currency-php)[![Daily Downloads](https://camo.githubusercontent.com/42078011b191f00584b8da92fb45a64467366dd04dd60423f740d43ec2c03bd6/68747470733a2f2f706f7365722e707567782e6f72672f746f6d7772696768742f63757272656e63792d7068702f642f6461696c79)](https://packagist.org/packages/tomwright/currency-php)[![License](https://camo.githubusercontent.com/9f283c32e2f5b7e1707e58c1976073d622f464873387fb40a2d9a8f97045788d/68747470733a2f2f706f7365722e707567782e6f72672f746f6d7772696768742f63757272656e63792d7068702f6c6963656e73652e737667)](https://packagist.org/packages/tomwright/currency-php)

Installation
------------

[](#installation)

```
composer install tomwright/currency-php

```

Usage
-----

[](#usage)

CurrencyPHP is just a basic wrapper. It cannot do conversions out of the box... Saying that... you only need to provide it with the conversion rates.

```
$rateFetcher = new MyConversionRateFetcher();
$factory = new CurrencyFactory($rateFetcher);

$gbp = $factory->create('GBP');
$usd = $factory->create('USD');

$priceInGBP = 100;
$priceInUSD = $gbp->convertTo($usd, $priceInGBP);
echo $priceInUSD; // 126
```

Rate Fetchers
-------------

[](#rate-fetchers)

Rate Fetchers are what `CurrencyPHP` uses to get conversion rates. Any Rate Fetcher you create should implement `ConversionRateFetcherInterface`.

### Existing Rate Fetchers

[](#existing-rate-fetchers)

- [Fixer IO](https://github.com/TomWright/CurrencyPHPFixerIORateFetcher)
- [Yahoo Currency API](https://github.com/TomWright/CurrencyPHPYahooRateFetcher)

If you have created your own Rate Fetcher and want it included here, please submit a pull request.

### Creating Your Own

[](#creating-your-own)

The following Rate Fetcher gives you some fixed exchange rates:

- GBP to USD
- USD to GBP
- GBP to CAD
- CAD to USD

```
class FixedRateFetcher implements ConversionRateFetcherInterface
{

    /**
     * @param Currency $from
     * @param Currency $to
     * @return float
     */
    public function getConversionRate(Currency $from, Currency $to)
    {
        $rates = [
            [
                'from' => 'GBP',
                'to' => 'USD',
                'rate' => 1.2547,
            ],
            [
                'from' => 'USD',
                'to' => 'GBP',
                'rate' => 0.7974,
            ],
            [
                'from' => 'GBP',
                'to' => 'CAD',
                'rate' => 1.6612,
            ],
            [
                'from' => 'CAD',
                'to' => 'USD',
                'rate' => 0.7539,
            ],
        ];

        $result = null;

        foreach ($rates as $rate) {
            if ($rate['from'] === $from->getCurrencyCode() && $rate['to'] === $to->getCurrencyCode()) {
                $result = $rate['rate'];
            }
        }
        return $result;
    }
}
```

### Handling unknown conversion rates

[](#handling-unknown-conversion-rates)

#### One way conversion rates

[](#one-way-conversion-rates)

The above Rate Fetcher has rates for both GBP to USD, and USD to GBP and this works great... but you'll also notice that it has CAD to USD, but no USD to CAD conversion rates. There is some logic implemented so that you only need to store 1 way conversion rates and it will automatically invert the rate if required.

Thanks to this logic, you can run a USD to CAD conversion using the above Rate Fetcher with no problems. The full list of conversion that the above can handle is as follows:

- GBP to USD
- USD to GBP
- GBP to CAD
- CAD to GBP
- CAD to USD
- USD to CAD

#### Missing conversion rates

[](#missing-conversion-rates)

If no conversion rate exists at all between the 2 currencies, an `UnhandledConversionRate` Exception will be thrown.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~88 days

Total

4

Last Release

3127d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/de2053f4238374bd541fc3fc5711e331c91f3279f7a332ee1c3f3db13a1829d1?d=identicon)[TomWright](/maintainers/TomWright)

---

Top Contributors

[![TomWright](https://avatars.githubusercontent.com/u/935867?v=4)](https://github.com/TomWright "TomWright (4 commits)")[![tomwright-awin](https://avatars.githubusercontent.com/u/30627479?v=4)](https://github.com/tomwright-awin "tomwright-awin (2 commits)")

---

Tags

conversion-ratecurrencycurrency-convertercurrency-exchange-ratesphp

### Embed Badge

![Health badge](/badges/tomwright-currency-php/health.svg)

```
[![Health](https://phpackages.com/badges/tomwright-currency-php/health.svg)](https://phpackages.com/packages/tomwright-currency-php)
```

###  Alternatives

[ramsey/devtools

A Composer plugin to aid PHP library and application development.

7134.7k26](/packages/ramsey-devtools)[jimbojsb/workman

PHP process forking &amp; daemonizing library

608.8k](/packages/jimbojsb-workman)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
