Calculadora del tiempo de descarga
Esta es una pequeña aplicación para calcular tiempos de descarga en función del tamaño del archivo a bajar y la velocidad de la transferencia.
Código Fuente:
Formulario:
<head>
<style type="text/css">
body {
background-color: transparent;
font-family:Verdana,Tahoma,"DejaVu Sans",sans-serif;
font-size:0.875em;
margin:0;
padding:0;
}
a:link {
color:#666666;
text-decoration:none;
}
a:visited {
color:#666666;
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
a:focus {
}
a:active {
}
</style>
</head>
<body>
<center>
<table cellpadding="6" style="border: solid #000000 1px; background-color:#ffffdd; font-size:0.875em;">
<form method="post" action="/transferspeed.php">
<tr>
<td>Tamaño del archivo</td>
<td>Uds.</td>
<td>Velocidad transferencia</td>
<td>Uds.</td>
<td>bits/bytes</td>
<td>Ud.Tiempo</td>
</tr>
<tr>
<td><input type="text" name="size" size="20" maxlength="20" value="" /> </td>
<td><select name="unidades1">
<option value="1">Bytes</option>
<option value="k">kB</option>
<option value="M" selected="selected">MB</option>
<option value="G">GB</option>
<option value="T">TB</option>
<option value="P">PB</option>
<option value="Ki">KiB</option>
<option value="Mi">MiB</option>
<option value="Gi">GiB</option>
<option value="Ti">TiB</option>
<option value="Pi">PiB</option>
</select></td>
<td><input type="text" name="velocidad" size="20" maxlength="20" value="" /> </td>
<td><select name="unidades2">
<option value="1">1</option>
<option value="k" selected="selected">k</option>
<option value="M">M</option>
<option value="G">G</option>
</select></td>
<td><select name="bits_or_bytes">
<option value="b">b [bit]</option>
<option value="B" selected="selected">B [Byte]</option>
</select> /</td>
<td><select name="udtiempo">
<option value="s" selected="selected">s</option>
<option value="min">min</option>
<option value="h">h</option>
<option value="d">d</option>
<option value="w">sem</option>
<option value="m">mes</option>
<option value="y">año</option>
</select></td>
<tr>
<td colspan="6" style="text-align:right;"><input type="submit" value="Calcular" />
</tr>
</form>
</table>
</center>
</body>Cálculo y resultados (transferspeed.php)
<?php
print '<head>
<style type="text/css">
body {
background-color: transparent;
font-family:Verdana,Tahoma,"DejaVu Sans",sans-serif;
font-size:0.875em;
margin:0;
padding:0;
}
a:link {
color:#666666;
text-decoration:none;
}
a:visited {
color:#666666;
text-decoration:none;
}
a:hover {
text-decoration:underline;
}
a:focus {
}
a:active {
}
</style>
</head>
<body>';
function Sec2Time($time){
if(is_numeric($time)){
if($time >= 604800){
$weeks = floor($time/604800);
$string .= $weeks . 'sem ';
$time = ($time%604800);
$complexity = 1;
}
if($time >= 86400){
$days = floor($time/86400);
$string .= $days . 'd, ';
$time = ($time%86400);
$complexity = 1;
}
if($time >= 3600){
$hours = floor($time/3600);
$string .= $hours . 'hr ';
$time = ($time%3600);
$complexity = 1;
}
if($time >= 60){
$minutes = floor($time/60);
$string .= $minutes . 'min ';
$time = ($time%60);
$complexity = 1;
}
if($complexity == 1){
$seconds = floor($time);
} else {
$seconds = round($time, 1);
}
$string .= $seconds . 's';
return $string;
}else{
return (bool) FALSE;
}
}
$filesize_bare = $_POST["size"];
$filesize_units = $_POST["unidades1"];
$speed_bare = $_POST["velocidad"];
$speed_units = $_POST["unidades2"];
$bits_or_bytes = $_POST["bits_or_bytes"];
$dif_time_units = $_POST["udtiempo"];
$filesize_bare = str_replace(",", ".", $filesize_bare);
$speed_bare = str_replace(",", ".", $speed_bare);
if (is_numeric($speed_bare) && is_numeric($filesize_bare) && substr_count($speed_bare, '.') < 2 && substr_count($filesize_bare, '.') < 2) {
if ($bits_or_bytes == "b") {
$bitconv = 8*1.17;
} elseif ($bits_or_bytes == "B") {
$bitconv = 1;
}
$units_array = array(
'1' => 1,
'Ki' => 1024,
'k' => 1000,
'M' => 1000 * 1000,
'Mi' => 1024 * 1024,
'G' => 1000 * 1000 * 1000,
'Gi' => 1024 * 1024 * 1024,
'T' => 1000 * 1000 * 1000 * 1000,
'Ti' => 1024 * 1024 * 1024 * 1024,
'P' => 1000 * 1000 * 1000 * 1000 * 1000,
'Pi' => 1024 * 1024 * 1024 * 1024 * 1024,
);
$time_units_array = array(
's' => 1,
'min' => 60,
'h' => 60*60,
'd' => 60*60*24,
'w' => 60*60*24*7,
'm' => 60*60*24*31,
'y' => 60*60*24*265,
);
$speed = $speed_bare * $units_array[$speed_units];
$filesize = $filesize_bare * $units_array[$filesize_units];
$time = Sec2Time($filesize * $time_units_array[$dif_time_units] * $bitconv / $speed);
if ($time == "0s") {
$time = "<0.1s";
}
if ($filesize_units != "1") {
$filesize_units = $filesize_units . "B";
} else {
$filesize_units = "B";
}
if ($speed_units != "1") {
$speed_units = $speed_units . $bits_or_bytes;
} else {
$speed_units = $bits_or_bytes;
}
if ($bits_or_bytes == "b") {
$persecond = "ps";
} else {
$persecond = "/s";
}
print "<p>La descarga de un archivo de <b>$filesize_bare $filesize_units</b> a una velocidad de transferencia de <b>$speed_bare $speed_units$persecond</b> tardaría:</p><center><b>$time</b>.</center><p style=\"text-align:center\"><a href=\"javascript:history.go(-1)\">Volver al formulario</a></p>";
} else {
print "<center>Input inválido</center><p style=\"text-align:center\"><a href=\"javascript:history.go(-1)\">Volver al formulario</a></p>";
}
print "</body>";
?>
Publicado bajo Licencia Creative Commons GNU General Public


Comentarios
Enviar un comentario nuevo