Skip to content

Instantly share code, notes, and snippets.

@jrivero
Last active February 21, 2024 11:34
Show Gist options
  • Save jrivero/2232034 to your computer and use it in GitHub Desktop.
Save jrivero/2232034 to your computer and use it in GitHub Desktop.
Human redable sizes
<?php
// http://www.geekality.net/2010/11/17/php-output-a-number-of-bytes-in-a-human-readable-way/
function make_pretty($bytes)
{
$symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$exp = floor(log($bytes) / log(1024));
return sprintf('%.2f '.$symbols[$exp], $bytes/pow(1024, floor($exp)));
}
// Examples
echo make_pretty(167892598784); // 156.36 GiB
echo make_pretty(719267016); // 685.95 MiB
echo make_pretty(114893); // 112.20 KiB
echo make_pretty(6218); // 6.07 KiB
echo make_pretty(42); // 42.00 B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment