arraypress/wp-conversion-utils

A comprehensive WordPress library for converting units, sizes, formats, and measurements

dev-main 2025-07-05 21:28 UTC

This package is auto-updated.

Last update: 2025-09-02 14:52:27 UTC


README

A comprehensive WordPress library for converting units, measurements, sizes, and formats. Provides utility functions for length, weight, volume, temperature, clothing sizes, data formats, and more.

Installation

composer require arraypress/wp-conversion-utils

Quick Start

// Global functions (recommended)
$meters   = wp_convert_units( 10, 'ft', 'm', 'length' );
$celsius  = wp_convert_temperature( 77, 'F', 'C' );
$filesize = wp_format_filesize( 1048576 ); // "1.00 MB"

// Or use classes directly
use ArrayPress\ConversionUtils\Unit;
use ArrayPress\ConversionUtils\Format;

$pounds   = Unit::weight( 50, 'kg', 'lb' );
$duration = Format::seconds_to_duration( 3661 ); // "1 hour 1 minute 1 second"

Features

Unit Conversions

  • Length: m, km, ft, in, mi, yd, cm, mm
  • Weight: kg, g, mg, lb, oz, t
  • Volume: l, ml, gal, qt, pt, cup, fl oz, tbsp, tsp
  • Temperature: Celsius, Fahrenheit
  • Speed: km/h, m/s, mph, knot
  • Data Storage: B, KB, MB, GB, TB, PB
  • Area: sq m, sq km, sq ft, acre, hectare
  • Time: s, min, h, day, week, month, year
  • Energy: J, kJ, cal, kcal, Wh, kWh, BTU
  • Pressure: Pa, kPa, bar, psi, atm
  • Cooking: tsp, tbsp, cup, ml, l, pint, quart, gallon

Size Conversions

  • Clothing Sizes: US, UK, EU, IT, FR, JP (men/women)
  • Shoe Sizes: US, UK, EU (men/women)
  • Ring Sizes: US, UK, EU, JP
  • Bra Sizes: US, UK, EU, FR, AU
  • Hat Sizes: US, UK, EU, CM
  • Glove Sizes: US, EU, CM
  • Watch Band Sizes: MM, Inches
  • Belt Sizes: US, UK, EU, CM, Inches (men/women)
  • Sock Sizes: US, UK, EU (men/women)

Format Conversions

  • Boolean formats: yes/no, on/off, true/false, 1/0
  • File sizes: bytes to human-readable
  • Duration: seconds to human-readable
  • Percentages: decimal to percentage

Examples

Unit Conversions

// Length conversions
$meters = wp_convert_units( 5, 'ft', 'm', 'length' ); // 1.524
$inches = Unit::length( 100, 'cm', 'in' ); // 39.37

// Weight conversions
$pounds = wp_convert_units( 70, 'kg', 'lb', 'weight' ); // 154.32
$grams  = Unit::weight( 1, 'lb', 'g' ); // 453.59

// Temperature conversions
$fahrenheit = wp_convert_temperature( 25, 'C', 'F' ); // 77
$celsius    = Unit::temperature( 100, 'F', 'C' ); // 37.78

// Volume conversions
$liters = Unit::volume( 1, 'gal', 'l' ); // 3.79
$cups   = Unit::cooking( 250, 'ml', 'cup' ); // 1.04

// Data storage conversions
$megabytes = Unit::data_storage( 1, 'gb', 'mb' ); // 1024
$gigabytes = wp_convert_units( 2048, 'mb', 'gb', 'data' ); // 2

// Speed conversions
$kmh = Unit::speed( 60, 'mph', 'km/h' ); // 96.56
$ms  = wp_convert_units( 100, 'km/h', 'm/s', 'speed' ); // 27.78

// Area conversions
$hectares = Unit::area( 1, 'acre', 'hectare' ); // 0.405
$sqft     = wp_convert_units( 100, 'sq m', 'sq ft', 'area' ); // 1076.39

// Time conversions
$hours = Unit::time( 3600, 's', 'h' ); // 1
$days  = wp_convert_units( 168, 'h', 'day', 'time' ); // 7

// Energy conversions
$calories = Unit::energy( 1000, 'j', 'cal' ); // 239.01
$kwh      = wp_convert_units( 3600000, 'j', 'kwh', 'energy' ); // 1

// Pressure conversions
$psi = Unit::pressure( 1, 'bar', 'psi' ); // 14.5
$atm = wp_convert_units( 101325, 'pa', 'atm', 'pressure' ); // 1

Size Conversions

// Clothing sizes
$eu_dress = Size::clothing( '8', 'us', 'eu', 'women' ); // "36"
$uk_shirt = Size::clothing( 'L', 'us', 'uk', 'men' ); // "40" 

// Shoe sizes
$eu_shoe = Size::shoe( 9.5, 'us', 'eu', 'men' ); // 43
$uk_shoe = wp_convert_size( 8, 'us', 'uk', 'shoe', 'women' ); // 5.5

// Ring sizes
$uk_ring = Size::ring( '7', 'us', 'uk' ); // "N"
$eu_ring = wp_convert_size( 'M', 'uk', 'eu', 'ring' ); // "52"

// Bra sizes
$eu_bra = Size::bra( '34B', 'us', 'eu' ); // "75B"
$uk_bra = wp_convert_size( '32C', 'us', 'uk', 'bra' ); // "32C"

// Hat sizes
$eu_hat = Size::hat( '7 1/4', 'us', 'eu' ); // "58"
$cm_hat = wp_convert_size( '58', 'eu', 'cm', 'hat' ); // "58"

// Glove sizes
$eu_glove = Size::glove( 'M', 'us', 'eu' ); // "8"
$cm_glove = wp_convert_size( '19', 'cm', 'us', 'glove' ); // "M"

// Watch band sizes
$inches_band = Size::watch_band( '20', 'mm', 'inches' ); // "25/32"
$mm_band     = wp_convert_size( '7/8', 'inches', 'mm', 'watch' ); // "22"

// Belt sizes
$eu_belt    = Size::belt( '34', 'us', 'eu', 'men' ); // "85"
$women_belt = wp_convert_size( 'M', 'us', 'cm', 'belt', 'women' ); // "80"

// Sock sizes
$uk_sock = Size::sock( '9', 'us', 'uk', 'men' ); // "8.5"
$eu_sock = wp_convert_size( '7', 'us', 'eu', 'sock', 'women' ); // "37.5"

Format Conversions

// Boolean formats
$yes_no      = Format::to_yes_no( true ); // "yes"
$binary      = Format::to_binary( false ); // "0"
$string_bool = Format::to_string_boolean( 1, true ); // "True"
$on_off      = Format::to_on_off( true, true ); // "On"

// File sizes
$size1 = wp_format_filesize( 1024 ); // "1.00 KB"
$size2 = Format::bytes_to_size( 1048576, 1 ); // "1.0 MB"
$size3 = wp_format_filesize( 1073741824 ); // "1.00 GB"

// Duration formatting
$duration1 = wp_format_duration( 3661 ); // "1 hour 1 minute 1 second"
$duration2 = Format::seconds_to_duration( 7200, true ); // "2h"
$duration3 = wp_format_duration( 90, true ); // "1m 30s"

// Percentage formatting
$percent = Format::to_percentage( 0.75, 1 ); // "75.0%"
$decimal = Format::to_percentage( 0.3333, 2, false ); // "33.33"

// Date formatting
$formatted = Format::to_date( '2024-01-15', 'M j, Y' ); // "Jan 15, 2024"
$timestamp = Format::to_date( 1705276800, 'Y-m-d H:i:s' ); // "2024-01-15 00:00:00"

Global Functions

The library provides convenient WordPress-style functions:

Function Description
wp_convert_units($value, $from, $to, $type) Convert between measurement units
wp_convert_temperature( $value, $from, $to ) Convert temperature (C ↔ F)
wp_format_filesize($bytes, $precision) Format bytes to human readable
wp_format_duration($seconds, $short) Format seconds to duration
wp_convert_size( $size, $from, $to, $type, $gender) Convert clothing/shoe/ring sizes

Global Function Examples

// Unit conversions
$meters  = wp_convert_units( 100, 'ft', 'm', 'length' ); // 30.48
$pounds  = wp_convert_units( 50, 'kg', 'lb', 'weight' ); // 110.23
$liters  = wp_convert_units( 1, 'gal', 'l', 'volume' ); // 3.79
$celsius = wp_convert_temperature( 212, 'F', 'C' ); // 100

// File and duration formatting
$readable   = wp_format_filesize( 2048576 ); // "2.00 MB"
$time       = wp_format_duration( 3725 ); // "1 hour 2 minutes 5 seconds"
$short_time = wp_format_duration( 3725, true ); // "1h 2m 5s"

// Size conversions
$eu_clothing = wp_convert_size( 'M', 'us', 'eu', 'clothing', 'men' ); // "48"
$uk_shoe     = wp_convert_size( 9, 'us', 'uk', 'shoe', 'women' ); // "6.5"
$eu_ring     = wp_convert_size( '7', 'us', 'eu', 'ring' ); // "54"
$mm_watch    = wp_convert_size( '3/4', 'inches', 'mm', 'watch' ); // "19"

Class Methods

Unit Class

  • Unit::length( $value, $from, $to ) - Length conversions
  • Unit::weight( $value, $from, $to ) - Weight conversions
  • Unit::volume( $value, $from, $to ) - Volume conversions
  • Unit::temperature( $value, $from, $to ) - Temperature conversions
  • Unit::speed( $value, $from, $to ) - Speed conversions
  • Unit::data_storage( $value, $from, $to ) - Data size conversions
  • Unit::area( $value, $from, $to ) - Area conversions
  • Unit::time( $value, $from, $to ) - Time conversions
  • Unit::energy( $value, $from, $to ) - Energy conversions
  • Unit::pressure( $value, $from, $to ) - Pressure conversions
  • Unit::cooking( $value, $from, $to ) - Cooking measurements

Size Class

  • Size::clothing( $size, $from, $to, $gender ) - Clothing size conversions
  • Size::shoe( $size, $from, $to, $gender ) - Shoe size conversions
  • Size::ring( $size, $from, $to ) - Ring size conversions
  • Size::bra( $size, $from, $to ) - Bra size conversions
  • Size::hat( $size, $from, $to ) - Hat size conversions
  • Size::glove( $size, $from, $to ) - Glove size conversions
  • Size::watch_band( $size, $from, $to ) - Watch band size conversions
  • Size::belt( $size, $from, $to, $gender ) - Belt size conversions
  • Size::sock( $size, $from, $to, $gender ) - Sock size conversions

Format Class

  • Format::to_yes_no( $value, $title_case ) - Convert to yes/no
  • Format::to_on_off( $value, $title_case ) - Convert to on/off
  • Format::to_binary( $value ) - Convert to 1/0
  • Format::to_string_boolean( $value, $title_case ) - Convert to true/false
  • Format::bytes_to_size( $bytes, $precision ) - Format file sizes
  • Format::seconds_to_duration( $seconds, $short ) - Format duration
  • Format::rgb_to_hex( $red, $green, $blue ) - RGB to hex color
  • Format::hex_to_rgb( $hex ) - Hex to RGB color
  • Format::to_percentage( $number, $decimals, $add_symbol ) - Format percentage
  • Format::to_date( $value, $format ) - Format dates

Use Cases

E-commerce

// Product dimensions
$length_cm = wp_convert_units( 12, 'in', 'cm', 'length' ); // 30.48
$weight_kg = wp_convert_units( 5, 'lb', 'kg', 'weight' ); // 2.27

// International sizing
$eu_size = wp_convert_size( 'M', 'us', 'eu', 'clothing', 'men' ); // "48"
$uk_shoe = wp_convert_size( 9, 'us', 'uk', 'shoe', 'women' ); // 6.5
$eu_bra  = wp_convert_size( '34B', 'us', 'eu', 'bra' ); // "75B"

File Management

// Display file sizes
$readable_size = wp_format_filesize( $file_size_bytes );
echo "File size: " . $readable_size; // "File size: 2.5 MB"

// Convert storage units
$gb_available = Unit::data_storage( $mb_available, 'mb', 'gb' );
$tb_total     = wp_convert_units( $gb_total, 'gb', 'tb', 'data' );

Recipe Sites

// Convert cooking measurements
$metric_cups = Unit::cooking( 250, 'ml', 'cup' ); // 1.04
$tablespoons = Unit::cooking( 0.5, 'cup', 'tbsp' ); // 8
$liters      = wp_convert_units( 4, 'qt', 'l', 'volume' ); // 3.79

International Retail

// Clothing conversions for global customers
$sizes = [
	'us' => '8',
	'uk' => Size::clothing( '8', 'us', 'uk', 'women' ), // "12" 
	'eu' => Size::clothing( '8', 'us', 'eu', 'women' ), // "36"
	'fr' => Size::clothing( '8', 'us', 'fr', 'women' )  // "36"
];

// Shoe size chart
$shoe_sizes = [
	'us_women' => '8',
	'uk_women' => Size::shoe( 8, 'us', 'uk', 'women' ), // 5.5
	'eu_women' => Size::shoe( 8, 'us', 'eu', 'women' )  // 38.5
];

Technical Applications

// Network monitoring
$bandwidth_mbps = Unit::data_storage( $bytes_per_second * 8, 'b', 'mb' );
$storage_tb     = wp_convert_units( $total_bytes, 'b', 'tb', 'data' );

// Performance metrics
$load_time = wp_format_duration( $milliseconds / 1000 );
$file_size = wp_format_filesize( $asset_bytes );

Weather & Environmental

// Temperature display
$celsius      = wp_convert_temperature( $fahrenheit, 'F', 'C' );
$display_temp = round( $celsius, 1 ) . '°C';

// Wind speed
$kmh = wp_convert_units( $mph_wind, 'mph', 'km/h', 'speed' );
$ms  = Unit::speed( $kmh, 'km/h', 'm/s' );

// Pressure readings
$mb = Unit::pressure( $inches_hg, 'psi', 'kpa' ) * 10; // Convert to millibars

Advanced Usage

Chaining Conversions

// Convert through multiple units
$temp_f = 68; // Fahrenheit
$temp_c = wp_convert_temperature( $temp_f, 'F', 'C' ); // 20°C
$temp_k = $temp_c + 273.15; // Kelvin conversion

// Multistep size conversion
$us_shoe = 9.5;
$uk_shoe = Size::shoe( $us_shoe, 'us', 'uk', 'men' ); // 9.0
$eu_shoe = Size::shoe( $uk_shoe, 'uk', 'eu', 'men' );  // 43

Batch Processing

// Convert multiple measurements
$measurements = [
	[ 'value' => 10, 'from' => 'in', 'to' => 'cm' ],
	[ 'value' => 5, 'from' => 'lb', 'to' => 'kg' ],
	[ 'value' => 2, 'from' => 'gal', 'to' => 'l' ]
];

$converted = array_map( function ( $m ) {
	return wp_convert_units( $m['value'], $m['from'], $m['to'], 'length' );
}, $measurements );

Error Handling

// Check for successful conversions
$result = wp_convert_units( 100, 'invalid_unit', 'm', 'length' );
if ( $result === null ) {
	echo "Conversion failed - invalid units";
} else {
	echo "Result: " . $result . " meters";
}

// Size conversion validation
$bra_size = Size::bra( '34B', 'us', 'invalid_system' );
if ( $bra_size === null ) {
	echo "Invalid size system";
}

Requirements

  • PHP 7.4+
  • WordPress 5.0+

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the GPL-2.0-or-later License.

Support