Conversions

Bytes to SI-units

// BEGIN: borrowed and modified from http://de3.php.net/manual/en/function.filesize.php
function size_translate($filesize) {
    $array = array(
        'TiB' => 1024 * 1024 * 1024 * 1024,
        'GiB' => 1024 * 1024 * 1024,
        'MiB' => 1024 * 1024,
        'KiB' => 1024,
    );
    if($filesize <= 1024) {
        return $filesize . ' B';
    }
    foreach ($array as $name=>$size) {
        if($filesize >= $size) {
            return round((round($filesize / $size * 100) / 100), 2) . ' ' . $name;
        }
    }
    return false;
}
// END: borrowed and modified from http://de3.php.net/manual/en/function.filesize.php
echo size_translate(10559006985);

will show:

9.83 GiB

Seconds to Timespan

// BEGIN: borrowed and modified from http://de3.php.net/manual/en/function.filesize.php
static function time_translate($seconds) {
    $array = array(
        'y' => 60 * 60 * 24 * 365.25,
        'M' => 60 * 60 * 24 * 30.5,
        'w' => 60 * 60 * 24 * 7,
        'd' => 60 * 60 * 24,
        'h' => 60 * 60,
        'm' => 60,
        's' => 1,
    );
    foreach ($array as $name=>$secs) {
        if ($seconds < $secs && $secs != end($array)) continue;
        $resv = floor($seconds / $secs);
        $res .= ' ' . $resv . $name;
        $seconds -= $resv*$secs;
    }
    return trim($res);
}
// END: borrowed and modified from http://de3.php.net/manual/en/function.filesize.php

Takes an integer value and returns a string like:

1y 3M 2w 4d 14h 45m 12s

 
snippets/php/conversions.txt · Last modified: 2008-11-26 15:25.53 by mbirth
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki Contents powered by Club-Mate Contents powered by BassDrive.com Labelled with ICRA