Last active
February 21, 2024 11:34
-
-
Save jrivero/2232034 to your computer and use it in GitHub Desktop.
Human redable sizes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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