Skip to content

Instantly share code, notes, and snippets.

@nanashiRei
Last active December 24, 2015 07:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nanashiRei/6763011 to your computer and use it in GitHub Desktop.
Size Formatter
<?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