PHPackages                             planetbiru/forex - 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. planetbiru/forex

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

planetbiru/forex
================

Foreign Exchange

0.0.3(2mo ago)02MITPHP

Since Aug 30Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Planetbiru/Forex)[ Packagist](https://packagist.org/packages/planetbiru/forex)[ RSS](/packages/planetbiru-forex/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)DependenciesVersions (5)Used By (0)

Forex
=====

[](#forex)

Foreign Exchange

A PHP library to fetch and parse currency exchange rates from the European Central Bank (ECB). This library provides methods to convert currencies based on the retrieved exchange rates.

Important Performance Note
--------------------------

[](#important-performance-note)

Fetching historical data (especially via `loadHistory` or `loadByDate`) involves processing large XML datasets. This operation should **only** be performed by back-end processes (e.g., cron jobs, background workers) and **must not** be triggered synchronously by front-end user actions. To ensure optimal performance, always prefer fetching the minimal dataset required (e.g., `loadDaily` or `loadLast90Days`) over the full history.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Initialization](#initialization)
- [Loading Data](#loading-data)

    - [loadDaily](#loaddaily)
    - [loadLast90Days](#loadlast90days)
    - [loadHistory](#loadhistory)
    - [loadByDate](#loadbydate)
    - [load](#load)
- [Data Access &amp; Manipulation](#data-access--manipulation)

    - [get](#get)
    - [set](#set)
    - [convert](#convert)
    - [getAllRates](#getallrates)
    - [getDate](#getdate)

---

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

[](#installation)

Install the library using [Composer](https://getcomposer.org/):

```
composer require planetbiru/forex
```

---

Initialization
--------------

[](#initialization)

Ensure that the class file is included or that an autoloader is being used.

```
use Planetbiru\Forex\Forex;

// Example 1: Initialization without loading initial data
$forex = new Forex();
$forex->loadDaily(); // Manually load data afterwards

// Example 2: Direct initialization with daily data
$forex = new Forex(Forex::ECB_DAILY);

// Example 3: Direct initialization with last 90 days data
$forex = new Forex(Forex::ECB_90DAYS);
```

---

Loading Data
------------

[](#loading-data)

### loadDaily

[](#loaddaily)

Loads the latest daily reference exchange rates.

**Example 1: Basic usage**

```
$forex = new Forex();
$forex->loadDaily();
echo "Daily data loaded. Date: " . $forex->getDate();
```

**Example 2: Reload daily data**

```
$forex = new Forex();
// ... perform other operations ...
$forex->loadDaily(); // Refresh to the latest daily data
```

**Example 3: Check currency availability after loading**

```
$forex = new Forex();
$forex->loadDaily();
if ($forex->getDate() == date('Y-m-d')) {
    echo "Today's data is already available.";
}
```

---

### loadLast90Days

[](#loadlast90days)

Loads exchange rate references from the last 90 days file (using the most recent date available in that file).

**Example 1: Basic usage**

```
$forex = new Forex();
$forex->loadLast90Days();
```

**Example 2: Using class constants**

```
$forex = new Forex();
$forex->load(Forex::ECB_90DAYS); // Equivalent to loadLast90Days()
```

**Example 3: Validation after loading**

```
$forex = new Forex();
try {
    $forex->loadLast90Days();
    echo "Successfully loaded 90-day data.";
} catch (Exception $e) {
    echo "Failed: " . $e->getMessage();
}
```

---

### loadHistory

[](#loadhistory)

Loads exchange rate references from the full historical file (since 1999). By default, it retrieves the most recent available date.

**Example 1: Basic usage**

```
$forex = new Forex();
$forex->loadHistory();
```

**Example 2: Preparation for loadByDate**

```
// Typically, loadHistory is not called directly when searching for a specific date,
// but this file is used internally by loadByDate.
$forex = new Forex();
$forex->loadHistory(); // Loads the most recent data from the large history file
```

**Example 3: Handling large files**

```
$forex = new Forex();
// Caution: this loads a relatively large XML file
$forex->loadHistory();
echo "Number of available currencies: " . count($forex->getAllRates());
```

---

### loadByDate

[](#loadbydate)

Loads exchange rates for a specific date (YYYY-MM-DD) from the full historical dataset.

**Example 1: Load a valid date**

```
$forex = new Forex();
$forex->loadByDate('2024-01-03'); // Ensure the date is not a holiday/weekend
echo "USD rate on 2024-01-03: " . $forex->get('USD');
```

**Example 2: Handle a date that is not found**

```
$forex = new Forex();
try {
    $forex->loadByDate('2025-01-01'); // Future date
} catch (Exception $e) {
    echo "Data not found: " . $e->getMessage();
}
```

**Example 3: Searching dates in a loop**

```
$forex = new Forex();
$dates = ['2023-05-01', '2023-05-02', '2023-05-03'];
foreach ($dates as $date) {
    try {
        $forex->loadByDate($date);
        echo "EUR to USD rate on $date: " . $forex->get('USD') . "\n";
    } catch (Exception $e) {
        echo "No data available for $date\n";
    }
}
```

---

### load

[](#load)

Loads data from a custom XML URL.

**Example 1: Load from a custom URL**

```
$forex = new Forex();
$forex->load('https://example.com/custom-ecb-mirror.xml');
```

**Example 2: Load from a local file**

```
$forex = new Forex();
$forex->load('file:///path/to/local/eurofxref-daily.xml');
```

**Example 3: Reload with a different source**

```
$forex = new Forex();
$forex->load(Forex::ECB_DAILY);
// Switch source
$forex->load(Forex::ECB_90DAYS);
```

---

Data Access &amp; Manipulation
------------------------------

[](#data-access--manipulation)

### get

[](#get)

Retrieves the exchange rate for a specific currency.

**Example 1: Get USD rate**

```
$rate = $forex->get('USD');
echo "1 EUR = $rate USD";
```

**Example 2: Get IDR (Indonesian Rupiah) rate**

```
$rate = $forex->get('IDR');
echo "1 EUR = $rate IDR";
```

**Example 3: Case insensitive**

```
echo $forex->get('usd'); // Same as get('USD')
```

---

### set

[](#set)

Manually sets an exchange rate.

**Example 1: Override an existing rate**

```
$forex->set('USD', 1.12); // Force USD rate to 1.12
```

**Example 2: Add a new currency**

```
$forex->set('BTC', 0.000035); // Add Bitcoin
echo $forex->get('BTC');
```

**Example 3: Update IDR rate**

```
$forex->set('IDR', 16500);
echo "IDR rate manually updated.";
```

---

### convert

[](#convert)

Converts an amount from one currency to another.

**Example 1: Convert USD to IDR**

```
$usd = 100;
$idr = $forex->convert($usd, 'USD', 'IDR');
echo "$usd USD = $idr IDR";
```

**Example 2: Convert EUR to GBP**

```
$eur = 50;
$gbp = $forex->convert($eur, 'EUR', 'GBP');
echo "$eur EUR = $gbp GBP";
```

**Example 3: Convert JPY to USD**

```
$jpy = 10000;
$usd = $forex->convert($jpy, 'JPY', 'USD');
echo "$jpy JPY = $usd USD";
```

---

### getAllRates

[](#getallrates)

Returns all available exchange rates as an array.

**Example 1: Dump all data**

```
$rates = $forex->getAllRates();
print_r($rates);
```

**Example 2: Count available currencies**

```
$count = count($forex->getAllRates());
echo "$count currencies available.";
```

**Example 3: Loop through all rates**

```
$rates = $forex->getAllRates();
foreach ($rates as $code => $rate) {
    echo "1 EUR = $rate $code\n";
}
```

---

### getDate

[](#getdate)

Retrieves the date of the currently loaded exchange rate data.

**Example 1: Display the date**

```
echo "Exchange rate date: " . $forex->getDate();
```

**Example 2: Format the date**

```
$date = date('d F Y', strtotime($forex->getDate()));
echo "Last updated: $date";
```

**Example 3: Date validation**

```
if ($forex->getDate() === null) {
    echo "Data has not been loaded yet.";
} else {
    echo "Data is ready.";
}
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance88

Actively maintained with recent releases

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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 ~188 days

Total

4

Last Release

62d ago

Major Versions

0.0.3 → v2.x-dev2026-03-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/6c81065e666aa58e7edf5cb52a81145a39bb5f89c789fb37a71d6dda41b469c8?d=identicon)[kamshory](/maintainers/kamshory)

---

Top Contributors

[![kamshory](https://avatars.githubusercontent.com/u/6594971?v=4)](https://github.com/kamshory "kamshory (12 commits)")

### Embed Badge

![Health badge](/badges/planetbiru-forex/health.svg)

```
[![Health](https://phpackages.com/badges/planetbiru-forex/health.svg)](https://phpackages.com/packages/planetbiru-forex)
```

PHPackages © 2026

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