PHP code example of planetbiru / forex

1. Go to this page and download the library: Download planetbiru/forex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

planetbiru / forex example snippets


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);

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

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

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

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

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

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

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

// 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

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

$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');

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

$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";
    }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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