Last active
December 24, 2015 07:19
-
-
Save nanashiRei/6763011 to your computer and use it in GitHub Desktop.
Size Formatter
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 | |
class FormatHelper { | |
static public function niceSize($bytes, $digits = 2) { | |
$format = "%0.{$digits}f %s"; | |
$sizeObj = self::bytesHumnaized($bytes); | |
return sprintf($format, $sizeObj->value, $sizeObj->extension); | |
} | |
static public function bytesHumanized($bytes, $step = 1024) { | |
$out = new stdClass; | |
$sizes = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB']; | |
$sizeIndex = 0; | |
while ($bytes >= $step) { | |
$sizeIndex ++; | |
$bytes /= $step; | |
} | |
$out->value = $bytes; | |
$out->extension = $sizes[$sizeIndex]; | |
return $out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment