Unit Converter Info - thepoopooman652/proishs-webtools GitHub Wiki
Unit Converter
This tool is a general-purpose unit converter implemented as a single PHP page: unit-converter/index.php.
It accepts a numeric input value, a unit category (distance, mass, volume, temperature, area, speed, time, pressure, energy, power, data, angle), and a source unit. When you submit the form the tool converts the value into every supported unit in the chosen category and displays a tidy results table.
Goals / design
- Simple, self-contained PHP UI that works without JavaScript (JS enhances UX but is not required).
- Each category has a stable base unit. Conversions go: input unit -> base -> output unit. Temperature uses explicit formulae because it's not a pure multiplier.
- Output precision is configurable.
Location
- Tool code:
unit-converter/index.php - Documentation:
unit-converter/unit-converter.md
How to use (UI)
- Open
unit-converter/index.phpin your web server (for example, http://localhost/proishs-webtools/unit-converter/). - Select a category from the
Categorydrop-down. - Enter a numeric value and pick the
From unitfor that value. - (Optional) Change
Precision— number of decimal places in the table output. - Click
Convert.
The page will display a table converting your input to all supported units in the chosen category.
Programmatic API
The page uses POST form parameters. You can also call it with GET for convenience.
Parameters:
category— category key (distance, mass, volume, temperature, area, speed, time, pressure, energy, power, data, angle).value— numeric value to convert.from— unit code (see the supported units table below).precision— integer number of decimal places (0–12).
Example (GET):
/unit-converter/index.php?category=distance&value=1&from=km&precision=4
This will show conversion of 1 kilometer into every supported distance unit.
Supported categories and units
The tool attempts to cover common SI and imperial units. Each entry shows the unit code used in the UI.
-
distance (base: meter
m)mmMillimetercmCentimetermMeterkmKilometerinInchftFootydYardmiMile
-
mass (base: kilogram
kg)mgMilligramgGramkgKilogramtMetric tonozOuncelbPound
-
volume (base: liter
l)mlMilliliterlLiterm3Cubic metertspTeaspoon (US)tbspTablespoon (US)cupCup (US)flozFluid ounce (US)ptPint (US)qtQuart (US)galGallon (US)
-
temperature (base: Kelvin
K) — special handlingCCelsiusFFahrenheitKKelvinRRankine
-
area (base: square meter
m2)mm2Square millimetercm2Square centimeterm2Square meterkm2Square kilometerin2Square inchft2Square footacreAcrehaHectare
-
speed (base: m/s)
m/sMeters per secondkm/hKilometers per hourmphMiles per hourknotKnotft/sFeet per second
-
time (base: second
s)msMillisecondsSecondminMinutehHourdayDay
-
pressure (base: Pascal
Pa)PaPascalkPaKilopascalbarBaratmAtmospherepsiPound per square inch
-
energy (base: Joule
J)JJoulekJKilojoulecalCaloriekcalKilocalorieWhWatt hourkWhKilowatt hour
-
power (base: Watt
W)WWattkWKilowattMWMegawatthpHorsepower
-
data (base: Byte
B)BByteKBKilobyte (1000)KiBKibibyte (1024)MBMegabyte (1000^2)MiBMebibyte (1024^2)GBGigabyte (1000^3)GiBGibibyte (1024^3)TBTerabyte
-
angle (base: radian
rad)degDegreeradRadiangradGrad
Implementation notes
- The conversion table for non-temperature categories uses multiplicative factors relative to a base unit. Example: inches -> meters uses factor 0.0254.
- Temperature conversions use helper functions to convert to Kelvin and back because they require offsets and scaling.
- Precision is clamped to a reasonable range (0–12) in the UI.
Extending the tool
To add a category or a new unit:
- Open
unit-converter/index.php. - Update the
units_definitions()array: add a category key, definebaseandunitswithcode=>['name'=>..., 'factor'=>...]. For temperature-like categories add special handlers. - Save and reload the page.
Notes on adding temperature-like categories: any unit that requires more than a linear multiplier needs its own pair of conversion functions (to/from base). Follow the temp_to_kelvin() and kelvin_to_temp() pattern.
Examples
- Convert 5 kilometers to all distance units: choose category
distance, value5, fromkm. - Convert 32 Fahrenheit to Celsius & Kelvin: category
temperature, value32, fromF.
Tests
Minimal test checklist:
- UI test: select each category and convert a sample value (1 or 100) to ensure the page returns a table with no errors.
- Sanity checks for well-known conversions:
- distance: 1 km -> 1000 m
- mass: 1 lb -> 0.45359237 kg
- temperature: 0 C -> 273.15 K and 32 F -> 0 C
- data: 1 KiB -> 1024 B
- Edge cases:
- Very large numbers (1e12) and very small numbers (1e-12) — check precision formatting.
- Invalid category or unit — the UI falls back to a safe default (distance).
Caveats and limitations
- The tool is intentionally simple and synchronous. For very large data sets or many batched conversions, consider implementing an API endpoint that streams results or a background job.
- Unit lists are not exhaustive. Add or tweak factors in
units_definitions()as needed.
Next improvements (suggested)
- Add copy-to-clipboard buttons beside each result.
- Support selecting a subset of target units to show.
- Add CSV/JSON export of results.
- Add automated unit tests (PHPUnit) to verify conversion factors.
Contact / contribution
Contributions are welcome — open a pull request with unit additions or fixes. When adding units, include a brief source for conversion factors (official SI tables or authoritative references).