From cd96e61f53a57d94dfe673199a88b9acff4b1f1e Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sat, 9 Sep 2017 02:08:33 +0200 Subject: [PATCH] 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. --- format.inc.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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], + ]); +} + -- 2.30.0