Generate report between two dates with php and mysqli

1

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.

    
asked by anonymous 20.06.2018 / 17:14

1 answer

1

If it helps, you can check the day of the week, and add if checking if it is Sunday, I used something similar for a system of vacancies.

I'll post a code below if it helps, if not I try to do it with your logic.

echo 'Proximo dia util: '. date('d/m/Y', strtotime('+1 day')) ."<br>";
$domingo = 0;
$segunda =1;
$terça =2;
$quarta=3;
$quinta=4
$sexta = 5;
$sabado = 6;// sabado 6ºdia - fim da semana


$tx=0;
$ty=0;

$dia_atual=date('w'); //pego o dia atual
$data_atual = date('d/m/Y'); //armazena a data atual
$dias = $sabado - $dia_atual;//verifica quantos dias falta pro sabado

if ($dia_atual == $sexta){
    $data_next = date('d/m/Y', strtotime('+3 day'));
}else if ($dia_atual == $sabado){
    $data_next = date('d/m/Y', strtotime('+2 day'));
}else if ($dia_atual == $domingo or $dias > 0){
    $data_next = date('d/m/Y', strtotime('+1 day'));
}

$inicio = strtotime("-$dia_atual days");
$fim = strtotime("+$dias days");
$ini_sem =date('d/m/Y',$inicio); //data inicial DIA UTIL(domingo)
$fim_sem = date('d/m/Y',$fim); //data final PROXIMO DIA UTIL(sabado)
    
20.06.2018 / 17:47
Multiplication of arrays and store in another array? ___ ___ erkimt jQuery search field datatable ______ qstntxt ___

I'm using the jQuery Datatable component. In the preview on the computer table with the search field is ok, but when viewing the mobile field exceeds the panel where it is inserted, as can be seen in the image below:

HowcanIresolvethis?

    
_________azszpr308642

Enter in your CSS style one to limit the width of %code% %:

%pre%

The selector %code% handle the default search datatables element. Adjust the value of %code% more or less as does not exceed the panel.

    
___