page: showsize() to format file sizes
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 9 Sep 2017 00:08:33 +0000 (02:08 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Sat, 19 Dec 2020 02:01:43 +0000 (03:01 +0100)
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

index 10908f3893afa07cfa09d9e176254ac27fbcf308..35ecebb954ecd1de2f88658bcc5043921cb3b100 100644 (file)
@@ -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('&thinsp;', [
+               number_format($bytes / (1000 ** $order), $log % 3 ? 0 : 1, ',', ''),
+               $units[$order],
+       ]);
+}
+