PHP code example of stichoza / nbg-currency

1. Go to this page and download the library: Download stichoza/nbg-currency 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/ */

    

stichoza / nbg-currency example snippets


NbgCurrency::rate('usd'); // 2.7177
NbgCurrency::rate('usd', '2022-12-02'); // 2.7112

NbgCurrency::get('usd')->rate; // 2.7177
NbgCurrency::get('usd', '2022-12-02')->rate; // 2.7112
NbgCurrency::get('usd', '2022-12-02', 'en')->name; // US Dollar

NbgCurrency::date(Carbon::yesterday())->get('usd')->diff; // 0.0012
NbgCurrency::date('2022-12-02', 'en')->get('usd')->increased(); // true

use Stichoza\NbgCurrency\NbgCurrency;

NbgCurrency::rate(string $code, DateTimeInterface|string|null $date = null): float

NbgCurrency::rate('usd'); // Returns current rate of USD. Example: 2.7177
NbgCurrency::rate('usd', '2022-12-02'); // USD rate on 2022-12-02
NbgCurrency::rate('eur', 'yesterday'); // EUR rate from yesterday. Strings are parsed via Carbon::parse()
NbgCurrency::rate('eur', Carbon::yesterday()); // EUR rate from yesterday
NbgCurrency::rate('gbp', Carbon::today()->subDays(5)); // GBP rate 5 days ago
NbgCurrency::rate('gbp', new DateTime()); // GBP rate today

if (NbgCurrency::rate('usd') > 3) {
    echo 'Oh no!';
}

NbgCurrency::get(string $code, DateTimeInterface|string|null $date = null, string $language = 'ka'): Currency

$usd = NbgCurrency::get('usd'); // Currency object (Stichoza\NbgCurrency\Data\Currency)

$usd->code; // USD
$usd->rate; // 2.7112
$usd->name; // აშშ დოლარი
$usd->diff; // -0.0065
$usd->date; // Carbon object of date: 2022-12-01 17:45:12
$usd->validFrom; // Carbon object since when the rate is valid: 2022-12-02 00:00:00
$usd->change; // Currency rate change. -1 if decreased, 0 if unchanged and 1 if increased.

// Using methods available on Carbon objects
$usd->date->format('j F Y'); // 1 December 2022
$usd->date->diffForHumans(); // 3 days ago
$usd->validFrom->isPast(); // true

// Additional methods
$usd->increased(); // Returns true if rate has increased, false otherwise.
$usd->decreased(); // Returns true if rate has decreased, false otherwise.
$usd->unchanged(); // Returns true if rate hasn't changed, false otherwise.

// The changeString() method returns first parameter if rate was increased, second string if there was
// no change and third string if the rate went up. Useful for CSS classes, font icons, etc.
$class = $usd->changeString('text-red', 'text-gray', 'text-green');
$icon  = $usd->changeString('fa-arrow-down', 'fa-circle', 'fa-arrow-down');

NbgCurrency::date(DateTimeInterface|string|null $date = null, string $language = 'ka'): Currencies

$currencies = NbgCurrency::date('3 days ago');
$currencies = NbgCurrency::date(Carbon::now()->startOfMonth(), 'en');

$currencies->date; // Carbon object of date

$currencies->get('usd'); // Returns Currency object for USD
$currencies->has('eur'); // True if EUR currency is contained in $currencies collection
$currencies->count(); // Count of Currency objects in collection

$currencies->get('usd')->rate; // Currency rate of USD
$currencies->get('eur')->date->diffForHumans(); // 10 days ago

$currencies = NbgCurrency::date('2022-12-02', 'en'); // Currencies object (Stichoza\NbgCurrency\Data\Currencies) 

echo 'Total ' . count($currencies) . ' currencies for ' . $currencies->date->toFormattedDateString();
// Total 43 currencies for Dec 2, 2022

foreach ($currencies as $code => $currency) {
    echo $currency->code . ' costs ' . $currency->rate;
}
// AED costs 7.3662
// AMD costs 6.8453
// ...

try {
    $usdRate = NbgCurrency::date('10 days ago', 'en')->get('usd')->rate;
} catch (Exception) {
    // Whoops...
}

// Next 3 method calls will result in 1 HTTP request in total.
NbgCurrency::rate('usd');
NbgCurrency::rate('eur');
NbgCurrency::rate('gbp');

// Next 3 method calls will result in 3 HTTP requests in total.
NbgGurrency::rate('usd', '2022-10-10');
NbgGurrency::rate('eur', '2022-11-11', 'en');
NbgGurrency::rate('gbp', '2022-11-11');

// Next 3 method calls will result in 2 HTTP request in total.
NbgGurrency::rate('usd', '2022-11-11');
NbgGurrency::rate('eur', '2022-11-11');
NbgGurrency::rate('gbp', '2022-11-11', 'en');

NbgCurrency::disableCaching(); // Disable caching, also removes data stored in the property.
NbgCurrency::enableCaching(); // Enables caching in class property.

$codes = ['USD', 'EUR', 'GBP', 'UAH', 'JPY'];

foreach ($codes as $code) {
    echo NbgCurrency::rate($code);
}

$codes = ['USD', 'EUR', 'GBP', 'UAH', 'JPY'];

$currencies = NbgCurrency::date();

foreach ($codes as $code) {
    echo $currencies->get($code)->rate;
}