Common measurement conversions in PHP

One of the tricky things about being a web developer is that you are sometimes forced to accommodate visitors that use different measurement systems and formats.

Currency symbols, date formats and measurement systems can all differ from one user to the next.

The following PHP code snippets will help you to convert between some of the most common measurements. Most notably, those found in the Imperial System and the Metric System.

Kilograms to Pounds using PHP.

function kilogramsToPounds($kg){
    return $kg * 2.20462;

As you can see, it’s as simple as multiplying the number of KG by 2.20462. In fact, you’ll find that most of the subsequent examples are just as straight-forward.

Pounds to Kilograms.

While Pounds (lb) is a popular measurement for weight in the United States; in Europe, they tend to use Kilograms.

function poundsToKilograms($pounds){
    return $pounds * 0.453592;

Stone to Kilograms.

To convert Stone into KG, multiply by 6.35029.

function stoneToKilograms($stone){
    return $stone * 6.35029;

Kilometres to Miles.

KM x 0.621371 = Miles.

function kilometersToMiles($km){
    return $km * 0.621371;

Miles to Kilometres.

Miles x 1.60934 = KM

function milesToKilometers($miles){
    return $miles * 1.60934;

Pounds to Stone.

Pounds x 0.0714286 = Stone

function poundsToStone($pounds){
    return $pounds * 0.0714286;

Yards to Meters in PHP.

Yards x 0.9144 = Meters

function yardsToMeters($yards){
    return $yards * 0.9144;

Centimetres to Meters.

This one is pretty simple, as there are 100 centimetres in a meter.

function centimetersToMeters($centimeters){
    return $centimeters * 0.01;

Meters to Centimetres.

And vice versa.

function metersToCentimeters($meters){
    return $meters * 100;

Centimetres to Inches.

There are 39.3700787 inches in a meter.

function centimetersToInches($centimeters){
    return ($centimeters * 0.01) * 39.3700787;

Meters to Miles.

Meters x 0.000621371 = Miles.

function metersToMiles($meters){
    return $meters * 0.000621371;

Inches to Centimetres.

Centimetres = Inches x 2.54

function inchesToCentimeters($inches){
    return $inches * 2.54;

Fahrenheit to Celsius.

As you can see, converting Fahrenheit to Celsius using PHP is not as simple as the conversions shown above.

function fahrenheitToCelsius($fahrenheit){
    return ($fahrenheit - 32) * 5 / 9;

Celsius to Fahrenheit.

Converting Celsius into Fahrenheit isn’t exactly straight forward either.

function celsiusToFahrenheit($celsius){
    return $celsius * 9/5 + 32;