I've been breaking my head for some time and need help. I have a code that retrieves a date from bd and compares the difference of days from that date to today.
If this difference is less than 7 the weekly record gets a value and if it is less than 30 the monthly value is
Follow the code:
$total_litros_semanal = 0;
$total_litros_mensal = 0;
$now = time(); // Data atual
$abastecimento = "SELECT * from abastecimento ";
$abastecimento .= "WHERE id_secretaria = '1'"; //mudar conforme o id da secretaria
$query = mysqli_query($conexao,$abastecimento);
if(!$query) {
die("Falha na consulta ao banco");
}
while ($exibir = mysqli_fetch_array($query)) {
$your_date = strtotime($exibir["data_abastecimento"]);
$datediff = $now - $your_date;
$diferenca = round($datediff / (60 * 60 * 24));
if($diferenca<=30){
$total_litros_mensal += $exibir["quantidade_litros"];
}
if($diferenca<=7){
$total_litros_semanal += $exibir["quantidade_litros"];
}
}
The only problem is that with this code I get the last 7 days and in case I want it to add only if we have started a new week, that is, from Sunday a new week starts.
If the day is before the last Sunday it does not add, only if it is this week, same thing per month.
If the date is in the previous month then it does not add, however, I'm tied up in how to do this.
Note: I'm using php + Mysqli procedural.
Hugs.