Skip to content

Instantly share code, notes, and snippets.

@nerdsrescueme
Created November 3, 2011 13:47
Show Gist options
  • Save nerdsrescueme/1336522 to your computer and use it in GitHub Desktop.
Save nerdsrescueme/1336522 to your computer and use it in GitHub Desktop.
Measurement
<?php
namespace Atom\Measurement;
abstract class Base {
protected static $baseUnit;
protected static $conversions = array();
protected static $offsets = array();
protected function add($baseUnit, array $conversions)
{
// Add base unit in as 1 so it can be formatted.
$conversions[$baseUnit] = 1;
foreach($conversions as $key => $conversion)
{
if(is_numeric($key))
{
static::$offsets[$key] = $conversion['offset'];
$conversion = $conversion[0];
}
if($conversion === 1)
{
static::$baseUnit = explode('/', $key);
}
list($short, $long, $plural) = explode('/', $key);
static::$conversions[$short] = $conversion;
static::$conversions[$long] = $conversion;
static::$conversions[$plural] = $conversion;
}
}
public function convert($from, $to)
{
$to = strtolower($to);
if(!isset(static::$conversions[$to]))
{
throw new \InvalidArgumentException($to.' is not a valid conversion unit type.');
}
list($fromInt, $fromUnit) = $this->parse($from);
// DO CONVERSION - Convert to base unit(1), convert to request unit.
}
public function parse($unit)
{
if(!preg_match('^([0-9\.]+)\s(.*)', $unit, $matches))
{
throw new \InvalidArgumentException('The unit input ['.$unit.'] could not be parsed.');
}
return array((int) $matches[1], $matches[2]);
}
public function __call($method, $parameters)
{
if(substr($method, 0, 2) == 'to')
{
$this->convert(array_shift($parameters), substr($method, 2))
}
}
}
/** End of file base.php */

Acceleration

BASE UNIT 'hm/ss;hectometer per square second;hectometers per square second'

attometer/square second: centigal: centimeter/square second: decigal: decimeter/square second: dekameter/square second: femtometer/square second: foot/square second: G: Gal: hectometer/square second: inch/square second: kilometer/hour second: kilometer/square second: meter/square second: micrometer/square second: mile/hour minute: mile/hour second: mile/square second: milligal: millimeter/square second: nanometer/square second: picometer/square second: yard/square second: 1E+20 1000000 10000 100000 1000 10 1E+17 328.083989501 10.1971621298 10000 1 3937.00787402 360 0.1 100 100000000 13421.617752 223.6936292 0.0621371192 10000000 100000 100000000000 100000000000000 109.361329834

Area

acre: are: arpent: barn: carree: catta: circular foot: circular inch: cuerda: dekare: donum: football field [US, NFL]: football field [soccer, 120x90]: hectare: hide: homestead: hout: hundred: kappland: labor: rood: section: square centimeter: square chain: square cubit: square decimeter: square dekameter: square digit: square fathom: square foot: square furlong: square hectometer: square inch: square kilometer: square league [nautical]: square league [US statute]: square light year: square link [gunter, survey]: square link [ramden, engineer]: square megameter: square meter: square microinch: square micrometer: square mil: square mile: square mile [nautical]: square mile [US, statute]: square millimeter: square paris foot [quebec]: square parsec: square perch: square perche: square pole: square rod: square yard: stremma: township: 247.105381 10000 247.44621648 1E+34 19580 14949.9 13705036 1973525200 254.45292621 1000 1088.1 186.8734447 92.5925925 100 2.0576131687 1.544401544 703.6 0.02 6482.56 1.394 988.42152587 0.3861021585 10000000000 2471.0538147 4783960.1852 100000000 10000 2755561000 298997.512 10763910.4 24.7105381 100 1550003100 1 0.0323948166 0.0429000686 1.1172508764E-26 24710439.365 10763867.316 1E-06 1000000 1.5500031E+21 1E+18 1.5500031E+15 0.3861021585 0.2915533496 0.3861006097 1000000000000 9478672.9858 1.0502647576E-27 39536.861035 19580.200501 39536.861035 39536.861035 1195990.05 1000 0.0107250599

Calorie

btu/cubic foot: calorie/cubic centimeter: chu/cubic foot: joule/cubic meter: kilocalorie/cubic meter: kilojoule/cubic meter: megajoule/cubic meter: therm/cubic foot: therm/gallon [UK]: 112.3703279450 1 62.4279599749 4186800 1000 4186.8000000086 4.1868000000 0.0011237032 0.0001804034

Circular

circumference [full circle]: degree [deg]: grade [grad]: mil: minute ('): nautical rhumb: octant [1/8 circle]: point: quadrant [1/4 circle]: radian [rad]: second (''): sextant [1/6 circle]: 1/2 circle: 1 360 400 6400 21600 32 8 32 4 6.2831853071 1295999.99998963 6 2

Computer

bit: byte: exabit: exabyte: gigabit: gigabyte (gb): kilobit: kilobyte (kb): megabit: megabyte (mb): petabit: petabyte: terabit: terabyte (tb): 1073741824 134217728 9.313225746176E-10 1.1641532182656E-10 1 0.125 1048576 131072 1024 128 9.536743164032E-07 1.1920928955136E-07 0.0009765624 0.0001220703

Cooking

cup [US]: cup [metric]: dash: demi: dessertspoon: dram: drop: gallon [US]: gallon [US, dry]: gallon [UK]: jigger: kiloliter: liter: milliliter: ounce [US, fluid]: ounce [UK, fluid]: peck [US]: peck [UK]: pinch: pint [US]: pint [UK]: pony: quart [US, fluid]: quart [UK]: shot: tablespoon [US]: tablespoon [UK]: tablespoon [metric]: teaspoon [US]: teaspoon [UK]: teaspoon [metric]: 4226.75284 4000 1623073.0897 4000 100000 270512.18162 19476877.076 264.172052 227.02074457 219.9692483 22542.681801 1 1000 1000000 33814.0227 35195.079728 113.510367 109.98462415 3246146.1794 2113.37642 1759.753986 33814.0226 1056.68821 879.8769932 33814.022702 67628.0454 70390.159456 66666.666667 202884.136 281560.63782 200000

Bandwidth

adsl: adsl 2: adsl 2+: bit/second (bps): byte/second: bluetooth 1.1: bluetooth 2.0: bluetooth 3.0: blu-ray controller (1x): cable modem - docsis 1: cable modem - docsis 3: cdma 2000: DS0: DS1: DS3: ethernet: ethernet - fast: ethernet - gigabit: E1: E2: E3: FDDI: FireWire: gigabit/second (gbps): gigabyte/second: gprs: gsm - csd: isdn - basic rate: isdn - dual: kilobit/second (kbps): kilobyte/second: megabit/second (mbps): megabyte/second: modem - 33.6k: modem - 56k: OC-1: OC-3: OC-9: OC-12: OC-18: OC-24: OC-36: OC-48: OC-96: OC-192: OC-256: OC-768: OC-1536: OC-3072: PCI - 32bit/33Mhz: PCI - 64bit/33Mhz: PC floppy disk controller: PC CD controller (1x): PC DVD controller (1x): SCSI: SCSI - fast: STM-0: STM-1: STM-4: STM-16: STM-64: STM-256: STM-512: STM-1024: STS-1: terabit/second (tbps): terabyte/second: token ring: token ring - later: token ring - ieee 802.5t: token ring ieee 802.5v: T1: T2: T3: T4: T5: umts: USB 1.0: USB 1.1: USB 2.0: vdsl 1: vdsl 2: 802.11: 802.11b: 802.11a: 802.11g with nitro: 1 0.666666664 0.333333336 8388608 1048576 8 2.6666666666 0.3333333336 0.2222222224 0.2155789472 0.0512820512 53.54248384 125 5.181347152 0.1788268952 0.8 0.08 0.008 3.90625 0.946969696 0.2327746744 0.08 0.0203452590 0.0078125 0.0009765624 142.2222224 568.8888888 128 56.88888888 8192 1024 8 1 243.809524 146.2857144 0.154320988 0.0514403292 0.0171467764 0.0128600823 0.0085733882 0.0064308681 0.0042105263 0.0032154340 0.0016077170 0.0008037777 0.0006028181 0.0002009393 0.0001004696 5.023484792E-05 0.0074976569 0.0037505860 16 6.82666664 0.7207207208 0.2 0.1 0.154320988 0.0514403292 0.0128600823 0.0032154340 0.0008037777 0.0002009393 0.0001004696 5.023484792E-05 0.154320988 7.629394528E-06 9.5367432E-07 2 0.5 0.08 0.008 5.181347152 1.26742712 0.1788268952 0.029178338 0.0199824154 21.3333336 5.208333336 0.6666666664 0.0166666664 1562.5 1953.125 4 0.72727272 0.148148148 0.0571428571

DateTime

century: day: day [sidereal]: decade: fortnight: hour: hour [sidereal]: microsecond: millisecond: minute: month: month [anomalistic]: month [dracontic]: month [synodic]: month [syderic]: nanosecond: picosecond: second: week: year: year [dracontic]: year [sidereal]: year [tropical]: 2.73790926E-05 1 1.0027379039 0.0002737909 0.0714285714 24 24.065712766 86400000000 86400000 1440 0.0328549112 0.03629 0.03675 0.03386 0.0366 86400000000000 8.64E+16 86400 0.142857143 0.0027379092 0.002885 0.0027378031 0.0027379092

Density

attogram/liter: centigram/liter: decigram/liter: dekagram/liter: exagram/liter: femtogram/liter: gigagram/liter: grain/gallon [UK]: grain/gallon [US]: grain/cubic foot: gram/cubic centimeter: gram/cubic meter: gram/cubic millimeter: gram/liter: hectogram/liter: kilogram/cubic meter: kilogram/cubic centimeter: kilogram/liter: microgram/liter: milligram/cubic meter: milligram/liter : nanogram/liter: ounce/cubic foot: ounce/cubic inch: ounce/gallon [UK]: ounce/gallon [US]: petagram/liter: picogram/liter: pound/cubic foot: pound/cubic inch: pound/cubic yard: pound/gallon [UK]: pound/gallon [US]: slug/cubic foot: teragram/liter: ton(short)/cubic yard: ton(long)/cubic yard: 1E+21 100000 10000 100 1E-15 1E+18 1E-06 70156.919 58417.8314 436995.724 1 1000000 0.001 1000 10 1000 0.001 1 1000000000 1000000000 1000000 1000000000000 998.847369 0.578036672 160.358672 133.526472 1E-12 1E+15 62.4279606 0.036127292 1685.55494 10.022417 8.34540449 1.94032033 1E-09 0.842777468 0.752479882

Energy

attojoule: BTU: BTU [thermochemical]: calorie: calorie [nutritional]: dyne centimeter: electronvolt: erg: gigajoule: gigawatt hour: gram force centimeter: gram force meter: horsepower hour: joule: kilocalorie: kiloelectronvolt: kilogram force - centimeter: kilogram force meter: kilojoule: kilowatt hour: kilowatt second: megaelectronvolt: megacalorie: megajoule: megawatt hour: newton meter: ounce force inch: pound force foot: pound force inch: quad: therm [Europe]: therm [US]: watt hour: watt second: 4.18400000240998E+21 3.9656668323 3.9683217171 1000 0.9993312320 41840000024.0998 2.61157231264107E+22 41840000024.0998 4.18400000240999E-06 1.16222222382144E-09 42664926.376495 426649.26376495 0.0015585656 4184 1 2.61157231264107E+19 42664.926376495 426.6492637649 4.1840000024 0.0011622222 4.1840000024 2.61157231264107E+16 0.001 0.0041840000 1.16222222382144E-06 4184 592504.326632563 3085.9600344487 37031.5204133846 3.96559520228418E-15 3.96566627969966E-05 3.96661370491125E-05 1.1622222238 4184

Force

attonewton: centinewton: decigram force: decinewton: dekagram force: dekanewton: dyne: exanewton: femtonewton: giganewton: gram force: hectonewton: joule/centimeter: joule/meter: kilogram force: kilonewton: kip: meganewton: megapound: micronewton: millinewton: nanonewton: newton: ounce force: piconewton: pound force: poundal: teranewton: ton force (long): ton force (short): 1E+21 100000 1019716.21297793 10000 10197.1621297793 100 100000000 1E-15 1E+18 1E-06 101971.621297793 10 100000 1000 101.9716212977 1 0.2248066363 0.001 0.1019716212 1000000000 1000000 1000000000000 1000 3596.9431049339 1E+15 224.8089429111 7233.0140771823 1E-09 0.1003604696 0.1124033181

Frequency

attohertz: centihertz: decihertz: degree hour: degree minute: degree second: dekahertz: exahertz: femtohertz: gigahertz: hectohertz: hertz: kilohertz: megahertz: microhertz: millihertz: nanohertz: petahertz: picohertz: radian/hour: radian/minute: radian/second: revolution/hour: revolution/minute: terrahertz: 1E+21 100000 10000 1296000000 21600000 360000 100 1E-15 1E+18 1E-06 10 1000 1 0.001 1000000000 1000000 1000000000000 1E-12 1E+15 22619467 376991.12 6283.1853 3600000 60000 1E-09

Power

attowatt: BTU/hour: BTU/minute: BTU/second: calorie/hour: calorie/minute: calorie/second: centiwatt: deciwatt: dekawatt: erg/hour: erg/minute: erg/second: exawatt: femtowatt: gigawatt: hectowatt: horsepower: horsepower [electric]: horsepower [metric]: horsepower [water]: joule/hour: joule/minute: joule/second: kilocalorie/hour: kilocalorie/minute: kilocalorie/second: kilogram meter/hour: kilogram meter/minute: kilogram meter/hour: kilojoule/hour: kilojoule/minute: kilojoule/second: kilowatt: megajoule/hour: megajoule/minute: megajoule/second: megawatt: microwatt: milliwatt: newton meter/hour: newton meter/minute: newton meter/second: nanowatt: petawatt: picowatt: pound foot/hour: pound foot/minute: pound foot/second: terawatt: watt: 1E+21 3412.14163 56.8690272 0.94781712 860420.65 14340.3442 239.005736 100000 238.84589663 100 36000000000000 600000000000 10000000000 1E-15 1E+18 1E-06 10 1.34102209 1.3404825737 1.3596215249 1.3404053118 3600000 60000 1000 860.42065 14.3403442 0.2390057736 367097.83668 6118.2972777 101.9716213 3600 60 1 1 3.6 0.06 0.001 0.001 1000000000 1000000 3600000 60000 1000 1000000000000 1E-12 1E+15 2655223.74 44253.729 737.562149 1E-09 1000

Pressure

atmosphere (atm): attopascal: bar: centimeter mercury (0°C): centimeter water (4°C): centipascal: decipascal: dekapascal: dyne/square centimeter: exapascal: femtopascal: foot water (60°F): gigapascal: hectopascal: inch mercury (32°F): inch water (60°F): kilogram/square centimeter: kilogram/square meter: kilogram/square millimeter: kilopascal: kilonewton/square centimeter: kilonewton/square meter: kilonewton/square millimeter: kip/square inch: megapascal: microbar: micropascal: millibar: millimeter mercury (0°C): millipascal: nanopascal: newton/square centimeter: newton/square meter: newton/square millimeter: pascal: petapascal: picopascal: pound/square foot: pound/square inch (psi): terapascal: ton/square foot: ton/square inch: torr (mmHg): 0.0098692326 1E+21 0.01 0.7500616827 10.1974428892 100000 10000 100 10000 1E-15 1E+18 0.3348872099 1E-06 10 0.2952998750 4.0186465198 0.0101971621 101.971621298 0.0001019716 1 0.0001 1 1E-06 0.0001450377 0.001 10000 1000000000 10 7.50061683 1000000 1000000000000 0.1 1000 0.001 1000 1E-12 1E+15 20.8854342331 0.145037738 1E-09 0.0104427171 7.2518868865E-05 7.50061683

Sound

Bel (B): Decibel (dB): Neper (Np): 0.1 1 0.1151277918

Speed

centimeter/hour: centimeter/minute: centimeter/second: foot/hour: foot/minute: foot/second: inch/hour: inch/minute: inch/second: kilometer/hour (kph): kilometer/minute: kilometer/second: knot: mach: meter/hour: meter/minute: meter/second: mile/hour (mph): mile/minute: mile/second: millimeter/hour: millimeter/minute: millimeter/second: speed of light [air]: speed of light [glass]: speed of light [vacuum]: speed of light [water]: speed of sound [sea level]: yard/hour: yard/minute: yard/second: 360000000 6000000 100000 11811023.622 196850.394 3280.8399 141732283.47 2362204.725 39370.0787 3600 60 1 1943.8445 2.9387 3600000 60000 1000 2236.93629 37.28227154 0.621371192 3600000000 60000000 1000000 3.3366416469E-06 5.0034614447E-06 3.335640952E-06 4.4364024692E-06 2.938669958 3937007.9 65616.7979 1093.6133

Temperature

Celsius (C): Fahrenheit (F): Kelvin (K): Reaumur (R): Rankine: -272.15 -457.87 1 -217.72 1.8000000000

Torque

dyne centimeter: dyne meter: dyne millimeter: gram force centimeter: gram force meter: gram millimeter: kilogram force centimeter: kilogram force meter: kilogram force millimeter: kilonewton meter: long tone force foot: newton centimeter: newton meter: newton millimeter: ounce force foot: ounce force inch: pound force foot: pound force inch: short tone force foot: 98066500 980665 980665000 100000 1000 1000000 100 1 1000 0.00980665 0.003229 980.665 9.80665 9806.65 115.72821857 1388.7386229 7.23301385 86.7961662 0.003617

Volume (Split large and small?)

acre inch: barrel [UK]: barrel [US liquid]: barrel [US dry]: barrel [US oil]: bucket [UK]: bucket [US]: bushel [UK]: bushel [US dry]: centiliter: cubic centimeter: cubic decimeter: cubic dekameter: cubic foot: cubic inch: cubic kilometer: cubic meter: cubic mile: cubic millimeter: cubic yard: cup: cup [metric]: dash [US]: deciliter: dekaliter: dessertspoon: dessertspoon [UK]: dram: dram [UK]: drop [US liquid]: drum [US oil]: drum [metric oil]: fifth [US liquid]: firkin [UK]: firkin [US]: gallon [UK]: gallon [US dry]: gallon [US liquid]: gigaliter: gill [UK]: gill [US]: hectoliter: hogshead [UK]: hogshead [US]: kiloliter: liter: megaliter: microliter: milliliter: minim [UK]: minim [US]: ounce [UK liquid]: ounce [US liquid]: peck [UK]: peck [US]: picoliter: pinch [US]: pint [UK]: pint [US dry]: pint [US liquid]: pipe [UK]: pipe [US]: quart [UK]: quart [US dry]: quart [US liquid]: stere: tablespoon [UK]: tablespoon [US]: tablespoon [metric]: teaspoon [UK]: teaspoon [US]: teaspoon [metric]: 9728.55833 6110256.8972 8386414.3603 8648489.8096 6289810.77 54992312.075 52834410.472 27496156.037 28377593.071 100000000000 1000000000000 1000000000 1000 35314666.7 61023744100 0.001 1000000 0.0002399127 1E+15 1307950.62 4226752840 4000000000 1623073088000 10000000000 100000000 101442068110 84468156400 270512181620 281560620360 12173048000000 4803128.2247 5000000 1320860261.8 24441000 29352500 219969248.3 227020744.57 264172052 1 7039015945.6 8453505675.5 10000000 3491575.3698 4193207.18 1000000 1000000000 1000 1E+15 1000000000000 16893638269000 16230730897000 35195079728 33814022700 109984624.15 113510367 1E+21 3246146176000 1759753986.4 1816165956.5 2113376420 2036752.2991 2096603.5902 879876993.2 908082978.26 1056688210 1000000 70390159456 67628045405 66666666667 168936372210 202884136000 200000000000

Weight/Mass

carat: centigram: decigram: dekagram: dram: dyne: grain: gram: hectogram: hundredweight [long]: hundredweight [short]: kilogram: kiloton [long]: kiloton [short]: kiloton [metric]: megagram: microgram: milligram: nanogram: ounce: ounce [troy]: pennyweight: petagram: picogram: pound: pound [troy]: quarter [UK]: quarter [US]: stone: ton [long]: ton [short]: ton [metric]: 5000000 100000000 10000000 100000 564383.391 980665002.86 15432358.353 1000000 10000 19.684130552 22.046226218 1000 0.0009842065 0.0011023113 0.001 1 1000000000000 1000000000 1E+15 35273.9619 32150.7466 643014.931 1E-09 1E+18 2204.62262 2679.2288807 78.736522209 88.184904874 157.47304442 0.984206528 1.10231131 1

<?php
namespace Atom\Measurement;
class Distance extends Base {
public function __construct()
{
$this->add('km/kilometer/kilometers', array(
'm/meter/meters' => 1000,
'dm/decimeter/decimeters' => 10000,
'cm/centimeter/centimeters' => 100000,
'mm/millimeter/millimeters' => 1000000,
'mi/mile/miles' => 0.62137,
'in/inch/inches' => 39370,
'ft/foot/feet' => 3280.8,
'yd/yard/yds' => 1093.6,
'nm/nautical mile/nautical miles' => 0.53996,
));
}
}
/** End of file distance.php */
<?php
namespace Atom;
class Measurement {
public static function distance($unit, $from, $to)
{}
}
/** End of file measurement.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment