PHP code example of grahamsutton / moneyman

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

    

grahamsutton / moneyman example snippets


use MoneyMan\Money;
use MoneyMan\Currency;

$money = new Money(123400, new Currency('USD'));

// Get human readable value based on locale , default is 'en_US'
echo $money->getFormatted();  // "$1,234.00"

// Get human readable value based on specified locale
echo $money->getFormatted('de_DE');  // "1.234,00 $"

use MoneyMan\Exchange;
use MoneyMan\ServiceFactory;

// Available services
$yahoo_service = ServiceFactory::getService('yahoo');
$fixer_service = ServiceFactory::getService('fixer');

// Pass to a \MoneyMan\Exchange object
$exchange = new Exchange($fixer_service);

use MoneyMan\Money;
use MoneyMan\Currency;
use MoneyMan\Exchange;
use MoneyMan\ServiceFactory;

$service = ServiceFactory::getService('fixer');  // use Fixer.io
$exchange = new Exchange($service);

$money = new Money(5000, new Currency('USD'));

// Pretend USD->EUR exchange rate is 0.92142
$exchanged_money = $exchange->exchange($money, new Currency('EUR'));

// Print the new money object value
echo $exchanged_money->getFormatted();  // "€46.07"

// Print it in different locale
echo $exchanged_money->getFormatted('de_DE');  // "46,07 €"

use MoneyMan\Money;
use MoneyMan\Currency;

$money1 = new Money(12300, new Currency('USD'));
$money2 = new Money(4500, new Currency('USD'));

// Returns a brand new Money object
$new_money = $money1->add($money2);

// Get human readable value of the new Money object
echo $new_money->getFormatted();  // "$168.00"

use MoneyMan\Money;
use MoneyMan\Currency;

$money1 = new Money(12300, new Currency('USD'));
$money2 = new Money(4500, new Currency('USD'));

// Returns a brand new Money object
$new_money = $money1->subtract($money2);  // think $money1 - $money2

// Get human readable value of the new Money object
echo $new_money->getFormatted();  // "$78.00"

use MoneyMan\Money;
use MoneyMan\Currency;
use MoneyMan\Exchange;
use MoneyMan\ServiceFactory;

// Get a service that will be used to perform the exchange
$service  = ServiceFactory::getService('yahoo');  // will use Yahoo Finance
$exchange = new Exchange($service);

$money1 = new Money(4300, new Currency('USD'));
$money2 = new Money(6700, new Currency('EUR'));

// $money2 will be converted to $money1's currency and then added together.
// Pretend EUR->USD exchange rate is 1.09124
$exchanged_money = $exchange->add($money1, $money2);

// Print the new money object's value
echo $exchanged_money->getFormatted();  // "$116.11"

// Print the new money object in different locale
echo $exchanged_money->getFormatted('de_DE');  // "116,11 $"

...

$money1 = new Money(10000, new Currency('EUR'));
$money2 = new Money(5300, new Currency('USD'));

// $money2 will be converted to $money1's currency and then subtracted from $money1
// Pretend USD->EUR exchange rate is 0.94235
$exchanged_money = $exchange->subtract($money1, $money2);  // think $money1 - $money2

// Print the new money object's value
echo $exchanged_money->getFormatted();  // "€50.05"

// Print the new money object in different locale
echo $exchanged_money->getFormatted('de_DE');  // "50,05 €"