Skip to content

Commit 620265b

Browse files
committed
✨ add size() method for human readable sizes
Signed-off-by: otengkwame <[email protected]>
1 parent c0dd863 commit 620265b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Core/core/Helpers/Format.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ public function fixDatetime($datetime)
167167
return correct_datetime($datetime);
168168
}
169169

170+
/**
171+
* Get human readable size
172+
*
173+
* @param integer|float $bytes
174+
* @return string
175+
*/
176+
public function size(int|float $bytes)
177+
{
178+
$i = floor( log($bytes, 1024) );
179+
180+
return round(
181+
$bytes / pow(1024, $i),
182+
[0,0,2,2,3,3,3,3,3][$i]
183+
) . ['B','kB','MB','GB','TB','PB','EB','ZB','YB'][$i];
184+
}
185+
170186
// FORMATTING OUTPUT ---------------------------------------------------------
171187

172188
/**

0 commit comments

Comments
 (0)