Soma horas durante período
Boa noite a todos!
Desde já peço desculpas caso esteja postando no lugar errado.
Bom, eu sei que aqui não é o Fórum "faz pra mim", mas estou precisando muito de ajuda para finalizar meu TCC (php e mysql não são minha area)
Tenho uma tabela no BD que guarda os valores de tensão, corrente, potência e data e hora.
Preciso fazer a soma das horas entre determinado período de tempo (15 dias por exemplo) e após isso fazer a média dos valores de outra coluna neste período.
Para isso, peguei um código na internet (perdi a referência) que estou tentando fazer um "range" no período.
filter.php
<?php
//filter.php
if(isset($_POST["from_date"], $_POST["to_date"]))
{
$connect = mysql_connect('localhost', 'usuário', 'senha');
@mysql_select_db('tabela') or die( "Unable to select database");
$output = '';
$query = "
SELECT * FROM medidas
WHERE horario BETWEEN '".$_POST["from_date"]."' AND '".$_POST["to_date"]."'
";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$output .= '
<table class="table table-bordered">
<tr align="center">
<th width="25%">Data</th>
<th width="25%">Corrente</th>
<th width="25%">Tensão</th>
<th width="25%">Potência</th>
</tr>
';
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$output .= '
<tr align="right">
<td><?php echo $row["horario"]; ?></td>
<td><?php echo $row["corrente"]; ?></td>
<td><?php echo $row["tensao"]; ?></td>
<td><?php echo $row["potencia"]; ?></td>
</tr>
';
}
}
else
{
$output .= '
<tr>
<td colspan="5">No Order Found</td>
</tr>
';
}
$output .= '</table>';
echo $output;
}
?>
e index.php
<?php
$connect = mysql_connect('localhost', 'usuário', 'senha');
@mysql_select_db('tebela') or die( "Unable to select database");
$query = "SELECT * FROM medidas ORDER BY horario ASC";
$result = mysql_query($query);
$row = mysql_fetch_array($result)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
</head>
<body>
<br /><br />
<div class="container" style="width:900px;">
<h2 align="center">Teste</h2>
<h3 align="center">Horario</h3><br />
<div class="col-md-3">
<input type="text" name="from_date" id="from_date" class="form-control" placeholder="De" />
</div>
<div class="col-md-3">
<input type="text" name="to_date" id="to_date" class="form-control" placeholder="Até" />
</div>
<div class="col-md-5">
<input type="button" name="filter" id="filter" value="Filtrar" class="btn btn-info" />
</div>
<div style="clear:both"></div>
<br />
<div id="order_table">
<table class="table table-bordered">
<tr>
<th width="25%">Data</th>
<th width="25%">Corrente</th>
<th width="25%">Tensão</th>
<th width="25%">Potência</th>
</tr>
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr align="right">
<td><?php echo $row["horario"]; ?></td>
<td><?php echo $row["corrente"]; ?></td>
<td><?php echo $row["tensao"]; ?></td>
<td><?php echo $row["potencia"]; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$.datepicker.setDefaults({
dateFormat: 'yy-mm-dd'
});
$(function(){
$("#from_date").datepicker();
$("#to_date").datepicker();
});
$('#filter').click(function(){
var from_date = $('#from_date').val();
var to_date = $('#to_date').val();
if(from_date != '' && to_date != '')
{
$.ajax({
url:"filter.php",
method:"POST",
data:{from_date:from_date, to_date:to_date},
success:function(data)
{
$('#order_table').html(data);
}
});
}
else
{
alert("Please Select Date");
}
});
});
</script>
Resumindo...
Nestes arquivos, quando aplico o flitro de data, não aparece absolutamente nada.
Não sei como somar a quantidade de horas (pode ser minutos, melhor ainda) em um período.
E ainda por cima preciso fazer uma média de valores em outra coluna, mas no periodo escolhido.Discussão (0)
Carregando comentários...