From: Mischa POSLAWSKY Date: Sat, 9 Sep 2017 00:08:33 +0000 (+0200) Subject: page: showsize() to format file sizes X-Git-Tag: v5.0~28 X-Git-Url: http://git.shiar.nl/minimedit.git/commitdiff_plain/cd96e61f53a57d94dfe673199a88b9acff4b1f1e page: showsize() to format file sizes Copied from Lijtweg [doclist precursor] introduced in commit v2.0-12-g040c5406df (2017-09-18) [com/bewoners: group file types by date]; unaltered since but no longer used. Could prove useful later, so move here to merge identically named files and share with other projects. --- diff --git a/format.inc.php b/format.inc.php index 10908f3..35ecebb 100644 --- a/format.inc.php +++ b/format.inc.php @@ -14,3 +14,15 @@ function showdate($parts) ])); } +function showsize($bytes) +{ + $units = ['', 'k', 'M', 'G', 'T']; + $log = strlen($bytes) - 1; + $order = floor($log / 3); + if (!isset($units[$order])) return $bytes; + return implode(' ', [ + number_format($bytes / (1000 ** $order), $log % 3 ? 0 : 1, ',', ''), + $units[$order], + ]); +} +