Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/Crisu83/php-conversion

Library for converting units and sizes in PHP
https://github.com/Crisu83/php-conversion

Last synced: 3 days ago
JSON representation

Library for converting units and sizes in PHP

Awesome Lists containing this project

README

        

php-conversion
==============
[![Build Status](https://travis-ci.org/crisu83/php-conversion.png?branch=master)](https://travis-ci.org/crisu83/php-conversion)

Library for converting units and sizes in PHP.

## Units supported

* Acceleration
* Angle
* Area
* Digital information
* Electric current
* Frequency
* Fuel consumption
* Length
* Luminous Intensity
* Mass
* Power
* Pressure
* Speed
* Temperature
* Time
* Velocity
* Voltage
* Volume

## Sizes supported

* Hat size
* Child shoe size

## Number base supported

* Binary
* Octal
* Decimal
* Hexadecimal

## Usage

Example usage:

```php
use Crisu83\Conversion\Quantity\DigitalInformation\DigitalInformation;
use Crisu83\Conversion\Quantity\Length\Length;
use Crisu83\Conversion\NumberBase\NumberBase;
use Crisu83\Conversion\Size\HatSize\HatSize;
use Crisu83\Conversion\Size\ShoeSize\ChildShoeSize;

use Crisu83\Conversion\Quantity\Length\Unit as LengthUnit;
use Crisu83\Conversion\Quantity\DigitalInformation\Unit as DIUnit;
use Crisu83\Conversion\Size\HatSize\System as HatSizeSystem;
use Crisu83\Conversion\Size\ShoeSize\System as ShoeSizeSystem;

require(dirname(__DIR__) . '/vendor/autoload.php');

$length = new Length(1, LengthUnit::METRE);
echo $length . '
';
echo $length->add(1, LengthUnit::FOOT) . '
';
echo $length->add(5)->sub(2, LengthUnit::FOOT) . '
';
echo $length->to(LengthUnit::YARD) . '
';

echo '
';

$di = new DigitalInformation(1000, DIUnit::MEGABYTE);
echo $di . '
';
echo $di->to(DIUnit::BIT)->out(2, '.', '') . '
';
echo $di->to(DIUnit::GIGABIT) . '
';
echo $di->to(DIUnit::TERABYTE)->out(10) . '
';

echo '
';

$number = new NumberBase("0xff", NumberBase::HEXADECIMAL);
echo $number . '
';
echo $number->to(NumberBase::DECIMAL) . '
';
echo $number->to(NumberBase::OCTAL) . '
';
echo $number->to(NumberBase::BINARY) . '
';

echo '
';

$hatSize = new HatSize(40, HatSizeSystem::CENTIMETRE);
echo $hatSize . '
';
echo $hatSize->to(HatSizeSystem::AMERICAN) . '
';
echo $hatSize->to(HatSizeSystem::BRITISH) . '
';
echo $hatSize->to(HatSizeSystem::INCH) . '
';

echo '
';

$shoeSize = new ChildShoeSize(20, ShoeSizeSystem::EUROPEAN);
echo $shoeSize . '
';
echo $shoeSize->to(ShoeSizeSystem::AMERICAN) . '
';
echo $shoeSize->to(ShoeSizeSystem::BRITISH) . '
';
echo $shoeSize->to(ShoeSizeSystem::INCH) . '
';
```

Sample output:

```
1.00 m
1.30 m
5.70 m
6.23 yd

1,000.00 MB
8388629474.89 b
7.81 Gb
0.0009534451 TB

0xff
255
o377
b11111111

40 cm
4 US
4 7/8 UK
15.748 in

20 EUR
4.5 - 5 US
3.5 - 4 UK
4.625 in
```