Library for converting units and sizes in PHP

Last update: Feb 20, 2022

php-conversion

Build Status

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:

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 . '<br>';
echo $length->add(1, LengthUnit::FOOT) . '<br>';
echo $length->add(5)->sub(2, LengthUnit::FOOT) . '<br>';
echo $length->to(LengthUnit::YARD) . '<br>';

echo '<br>';

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

echo '<br>';

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

echo '<br>';

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

echo '<br>';

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

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

GitHub

https://github.com/crisu83/php-conversion
You might also like...

Library for converting units and sizes in PHP

php-conversion Library for converting units and sizes in PHP. Units supported Acceleration Angle Area Digital information Electric current Frequency F

Mar 16, 2021

YCOM Impersonate. Login as selected YCOM user 🧙‍♂️in frontend.

YCOM Impersonate Login as selected YCOM user in frontend. Features: Backend users with admin rights or YCOM[] rights, can be automatically logged in v

Jan 19, 2022

Library to parse, format and convert byte units

Byte Units This is a utility component for parsing, formatting, converting and manipulating byte units in various formats. Usage ?php // Bytes manip

Jun 7, 2022

A library for handling physical quantities and the units of measure in which they're represented.

PHP Units of Measure master: Introduction This is a PHP library for representing and converting physical units of measure. The utility of this library

May 6, 2022

La Fattura is a system designed for companies and individuals of all sizes to manage invoices

La Fattura La Fattura is a system designed for companies and individuals of all sizes to manage invoices, it integrates with Stripe for payments and w

Feb 25, 2022

Collection of value objects that represent the PHP code units

sebastian/code-unit Collection of value objects that represent the PHP code units. Installation You can add this library as a local, per-project depen

Jun 25, 2022

A class to help convert bytes into other units (kb, mb, etc).

A class to help convert bytes into other units (kb, mb, etc). This package can be used to convert int|float values from bytes to KB, MB and GB as well

Mar 15, 2022

ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

Apr 8, 2022

Php library for converting emoji shortcodes to emoji

Emojify provides methods for checking if string is shortcode replace shortcode with emoji replacing all shortcodes in string with emoji Made for a md

May 19, 2022

PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule for validation Japanese string only full-width or only half-width.

Japanese String Helpers PHP Japanese string helper functions for converting Japanese strings from full-width to half-width and reverse. Laravel Rule f

Mar 22, 2022

Simple solution for converting excel rows into coordinates using Google Maps API

Package Usage Start by creating a new instance of ExcelToCoorindates class: $excel_to_cordinates = new ExcelToCoordinates(); Methods -setEx

Oct 16, 2021

PocketMine-MP Plugin for converting items & blocks name to your language.

PocketMine-MP Plugin for converting items & blocks name to your language.

ConvertName PocketMine-MP Plugin for converting items & blocks name to your language. Usage Due to license issue, I don't put the language files direc

Jan 16, 2022

Http-kernel - The HttpKernel component provides a structured process for converting a Request into a Response.

HttpKernel Component The HttpKernel component provides a structured process for converting a Request into a Response by making use of the EventDispatc

Jul 3, 2022

laminas-xml2json provides functionality for converting XML structures to JSON

laminas-xml2json This package is considered feature-complete, and is now in security-only maintenance mode, following a decision by the Technical Stee

Mar 4, 2022

PHP Exif Library - library for reading and writing Exif headers in JPEG and TIFF files using PHP.

PEL: PHP Exif Library README file for PEL: PHP Exif Library. A library with support for reading and writing Exif headers in JPEG and TIFF images using

May 5, 2022

Image optimization / compression library. This library is able to optimize png, jpg and gif files in very easy and handy way. It uses optipng, pngquant, pngcrush, pngout, gifsicle, jpegoptim and jpegtran tools.

Image Optimizer This library is handy and very easy to use optimizer for image files. It uses optipng, pngquant, jpegoptim, svgo and few more librarie

Jun 16, 2022

BeckhoffPLCSoapClient - SoapClient to communicate with BeckHoff PLC. Library made in PHP based on TcAdsWebService JavaScript Library.

BeckhoffPLCSoapClient - SoapClient to communicate with BeckHoff PLC. Library made in PHP based on TcAdsWebService JavaScript Library.

May 18, 2022

EmailReplyParser is a PHP library for parsing plain text email content, based on GitHub's email_reply_parser library written in Ruby

EmailReplyParser EmailReplyParser is a PHP library for parsing plain text email content, based on GitHub's email_reply_parser library written in Ruby.

Jun 16, 2022

ImageWorkshop is a PHP5.3+ library that helps you to manage images based on GD library

================================ ImageWorkshop class ================================ Summary and features Really flexible and easy-to-use PHP class t

Jun 16, 2022
Comments
  • 1. Currency

    • Added currency converter
    • Added more tests, and made 100% code coverage
    • Removed toNativeUnit() as this didn't was used at all
    • Updated codeception to version 2 (meaning some namespace changes and generated a new build file)
    • Travis test for 5.5 and 5.6
    • Added adult shoesizes for EUR and UK
    Reviewed by lsv at 2014-08-27 23:37
  • 2. Bugfix: Acres conversion factor

    Hi guys,

    Found a bug in area units conversion. Current acres to meters conversion factor is invalid. Here is a wikipedia link with correct one: https://en.wikipedia.org/wiki/Acre#Equivalence_to_other_units_of_area

    It should be 4046.8564224 instead of 247.105. Would you be so kind to accept my PR?

    With Best Regards, Alexey Petrov

    Reviewed by alexhelkar at 2016-01-28 15:54
  • 3. Autoupdate packagist

    ping @crisu83 I just tagged a new release and packagist did not show the new release. Seems like autoupdate is not enabled. Can you enable it in packagist.org?

    Reviewed by ojhaujjwal at 2014-07-17 09:39
  • 4. digital information: support for base number

    Is there support planned for distinguishing 1024 or 1000 based units? https://en.wikipedia.org/wiki/Mebibyte

    1 MB = 1000 kB 1 MiB = 1024 KiB

    As I see it, it can be done by simply adding these units to the "DigitalInformation" conversion map. Maybe create a separate Unit "SiDigitalInformation" to avoid confusion and allow users to keep using the old units as 1024-based.

    Reviewed by rauwebieten at 2018-09-13 09:51
Library to parse, format and convert byte units

Byte Units This is a utility component for parsing, formatting, converting and manipulating byte units in various formats. Usage <?php // Bytes manip

Jun 7, 2022
A library for handling physical quantities and the units of measure in which they're represented.

PHP Units of Measure master: Introduction This is a PHP library for representing and converting physical units of measure. The utility of this library

May 6, 2022
Jun 21, 2022
Tensor is a library and extension that provides objects for scientific computing in PHP.
Tensor is a library and extension that provides objects for scientific computing in PHP.

Tensor is a library and extension that provides objects for scientific computing in PHP. The multithreaded extension is especially suited for computing large sets of numbers. In some cases, the extension is 230X faster than the same operation in PHPland. Tensor is used by libraries such as Rubix ML to build and accelerate machine learning algorithms such as linear regression, dimensionality reduction, and neural networks.

Jun 17, 2022
PHP version of Google's phone number handling library

libphonenumber for PHP What is it? A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on

Jun 26, 2022
A PHP 5.3+ mathematics library, providing functionality for large numbers

Moontoast Math Library Moontoast\Math is useful for working with integers that are larger than (or may become larger than, through mathematical comput

Jun 15, 2022
Advanced Mathematics Library for PHP (port of Numbers.js)

Numbers.php Numbers.php - an advanced mathematics toolkit for PHP >= 5.3. It is a port of Numbers.js - same toolkit for JavaScript. There is a version

Dec 22, 2021
BigNum library for PHP compatible with bn.js

BigNum library for PHP Information This library provides a PHP Big Number API compatible with bn.js and is used in Fast PHP ECC library elliptic-php.

Jun 4, 2022
Arbitrary-precision arithmetic library for PHP
Arbitrary-precision arithmetic library for PHP

Arbitrary-precision arithmetic library for PHP

Jun 26, 2022
Unit converter and calculator for php

Unit converter and calculator This library uses the awesome lisachenko/z-engine to allow mathematical operations on objects, allowing to do stuff like

Apr 8, 2022