From 620265b9e432dbc678fb32cb8f9e6438b024909d Mon Sep 17 00:00:00 2001 From: otengkwame Date: Sat, 26 Nov 2022 15:10:30 +0000 Subject: [PATCH] :sparkles: add size() method for human readable sizes Signed-off-by: otengkwame --- Core/core/Helpers/Format.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Core/core/Helpers/Format.php b/Core/core/Helpers/Format.php index 0d57ec3..6808ebb 100644 --- a/Core/core/Helpers/Format.php +++ b/Core/core/Helpers/Format.php @@ -167,6 +167,22 @@ public function fixDatetime($datetime) return correct_datetime($datetime); } + /** + * Get human readable size + * + * @param integer|float $bytes + * @return string + */ + public function size(int|float $bytes) + { + $i = floor( log($bytes, 1024) ); + + return round( + $bytes / pow(1024, $i), + [0,0,2,2,3,3,3,3,3][$i] + ) . ['B','kB','MB','GB','TB','PB','EB','ZB','YB'][$i]; + } + // FORMATTING OUTPUT --------------------------------------------------------- /**