Skip to content

Instantly share code, notes, and snippets.

@jamesthomasonjr
Last active March 27, 2019 18:34
Show Gist options
  • Save jamesthomasonjr/30bf1b8ce398f7ee0a395ae532c818ef to your computer and use it in GitHub Desktop.
Save jamesthomasonjr/30bf1b8ce398f7ee0a395ae532c818ef to your computer and use it in GitHub Desktop.
A collection of unfinished code from rabbit holes I NEARLY went down but managed to save myself from... But maybe I'll finish them one day.
<?php
/**
* Converts a number of bytes to a human readable string
*
* Example:
* <code>
* $config->bytesToHuman(10); // 10 b
* $config->bytesToHuman(1000); // 1 kB
* $config->bytesToHuman(1024, 'iec'); // 1 KiB
* $config->bytesToHuman(10000); // 10 kB
* $config->bytesToHuman(10240); // 10 KiB
* $config->bytesToHuman(1000000); // 1 MB
* $config->bytesToHuman(1048576, 'iec'); // 1 MiB
* </code>
*
* @param number $value
* @param string $unitSystem SI or IEC
*
* @return string
*/
function bytesToHUman($value, $unitSystem = 'SI') {
if (strtoupper($unitSystem) === 'IEC') {
$step = 1024; // 2^10
$units = ['b', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
} else if (strtoupper($unitSystem) === 'SI') {
$step = 1000; // 10^3
$units = ['b', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB'];
} else {
return $value;
}
// Magic math here to return value
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment