Mailinglisten-Archive |
hi leute,
was haltet ihr von diesen funktionen? rein unter
programmiertechnischem aspekt (sauberkeit, programmierstil). ich
vermute das eine oder andere lässt sich verbessern (vieles erscheint
mir gemurkse, aber mir fällt nichts besseres ein). der
verwendungszweck ist einfach - man gibt beispielsweise
best_filesize($bytes) an und als return erhält man die zahl gekürzt
mit der besten einheit.
was kommt eigentlich nach TB?
<?
// Outputs a filesize in best human readable unit
// (c) daniel lorch <daniel_(at)_lorch.cc>, 2000
function best_filesize($bytes)
{
// intentionally used string keys, otherwise would exceed maximum array index
$units=array(
"1" => "Bytes",
"1024" => "KB",
(string)pow(1024,2) => "MB",
(string)pow(1024,3) => "GB",
(string)pow(1024,4) => "TB"
);
krsort($units); // big units first
while(list($base,$title)=each($units))
if(floor($bytes/$base) != 0)
{
if($base == 1) // Bytes
return $bytes." ".$title;
else
return number_format($bytes/$base, 1, ".", "'")." $title";
}
}
// Outputs a time in best human readable unit
// (c) daniel lorch <daniel_(at)_lorch.cc>, 2000
function modulo($a,$b) // needed for best_time()
{
$tmp=floor($a/$b);
return $a-($b*$tmp);
}
function best_time($seconds)
{
// intentionally used string keys, otherwise would exceed maximum array index
$units=array(
"1" => "Seconds",
"60" => "Minutes",
(string)pow(60,2) => "Hours"
);
krsort($units); // big units first
while(list($base,$title)=each($units))
if(floor($seconds/$base) != 0)
{
if($base == 1) // Seconds
return $seconds." ".$title;
else
{
$a=floor($seconds/$base);
$b=modulo($seconds,$base);
$b=sprintf("%02d", modulo($b,100)); // modulo ensures that there aren't more than 2 digits
return "$a:$b $title";
}
}
}
?>
cu
php::bar PHP Wiki - Listenarchive