PHP code example of brightcreations / money-converter
1. Go to this page and download the library: Download brightcreations/money-converter 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/ */
brightcreations / money-converter example snippets
// Or fetch current rates for a base currency
use BrightCreations\ExchangeRates\Facades\ExchangeRate;
ExchangeRate::storeExchangeRates('USD');
use BrightCreations\MoneyConverter\Enums\ExchangeRateProvidersEnum;
'default_provider' => ExchangeRateProvidersEnum::CONFIGURABLE->value,
use BrightCreations\MoneyConverter\Contracts\MoneyConverterInterface;
$service = app(MoneyConverterInterface::class);
// Current rate (from database via PDO provider)
$eurCents = $service->convert(10000, 'USD', 'EUR');
// Historical rate (pass a date; use startOfDay() for predictable date-only matching)
$eurCents = $service->convert(10000, 'USD', 'EUR', now()->subDays(7)->startOfDay());
use BrightCreations\MoneyConverter\Contracts\MoneyConverterInterface;
class InvoiceService
{
public function __construct(
private MoneyConverterInterface $moneyConverter
) {}
public function convertTotal(int $amountCents, string $from, string $to): int
{
return $this->moneyConverter->convert($amountCents, $from, $to);
}
}
$service = resolve(MoneyConverterInterface::class);
// or
$service = app()->make(MoneyConverterInterface::class);
use BrightCreations\MoneyConverter\Facades\MoneyConverter;
$eurCents = MoneyConverter::convert(10000, 'USD', 'EUR');
// Fluent options (see API reference)
$eurCents = MoneyConverter::fetchOnFail()
->interpolate()
->convertHistorical(10000, 'USD', 'EUR', now()->subDays(30)->startOfDay());
use Carbon\Carbon;
// Fill a gap between two stored dates (linear interpolation)
$service->interpolate()->convertHistorical(10000, 'USD', 'EUR', Carbon::parse('2024-01-08'));
// Target after latest stored rate (nearest previous rate)
$service->extrapolate()->convertHistorical(10000, 'USD', 'EUR', Carbon::parse('2024-06-01'));
// Target before earliest stored rate (nearest next rate)
$service->extrapolate()->convertHistorical(10000, 'USD', 'EUR', Carbon::parse('2024-01-01'));