Calculate the average in minutes?

1

I am trying to calculate the average time between the column created_at and updated_at, but I am having problems with the foreach, since it only takes the last value I send it, and ignores all the others.

public function tiempo()
{
    $creaciones = Deposito::where('autorizacion','=','1')->get();
    $prome= count($creaciones);
    foreach($creaciones as $creacion)
    {
        $timein = $creacion->created_at;
        $timefn = $creacion->updated_at;
        $dif = $timein->diffInMinutes($timefn); 
        $suma = $dif;
        $pro = (int)$suma;
        $tiempo = $pro/$prome;  
    } 
    return view('graficas.sucursales',compact('tiempo'));   
}

They could tell me what I should do to go through all the results of the consultation.

    
asked by user93672 19.07.2018 в 01:17
source

1 answer

0

The problem is that the average must be calculated outside the foreach . Also you are not adding all the values. It would be something like this:

public function tiempo()
{
    $creaciones = Deposito::where('autorizacion','=','1')->get();
    $prome= count($creaciones);
    $suma = 0;
    foreach($creaciones as $creacion)
    {
        $timein = $creacion->created_at;
        $timefn = $creacion->updated_at;
        $dif = $timein->diffInMinutes($timefn); 
        $suma += $dif;            
    }
    $pro = (int)$suma;
    $tiempo = $pro/$prome;  
    return view('graficas.sucursales',compact('tiempo'));   
}
    
answered by 19.07.2018 / 01:53
source