Redeem time records for a specific date and calculate interval between times

0

I have a question about how to assemble the logic to calculate hour / minute difference.

I have the following return:

{timestamp}          {tipo}
2018-10-18 02:22:35    O
2018-10-18 02:22:34    OI
2018-10-18 02:22:32    II
2018-10-18 02:22:31    I

Where the first value is a timestamp field and the second is the tipo field.

Issue 1
This tipo field is for determining whether the time is input, output, or interval. Being:

I  = Entrada
II = Intervalo
OI = Retorno Intervalo
O  = Saída

I want to calculate the time difference between "I" and "O", decreasing with the difference of "II" and "OI".

Formatting and picking up only the times I can safely do with Carbon.

My biggest problem is being in how to compare tipo (knowing which is the first and the last) and also how not to mix the days, after all there are more results. That is, my query returns records for several days and I'm having difficulty separating only the records for a specific day, so I can apply the calculations of difference between the times of that day.

Issue 2

$points = Records::where('user_id', '=', Auth::user()->id)->get();

$groupByDay = $points->sortBy('date');

    $result = $groupByDay
        ->groupBy(function ($result, $key) {
            return $result->created_at->format('d');
        });

Return:

  Collection {#269 ▼
  #items: array:2 [▼
    18 => Collection {#285 ▼
      #items: array:4 [▶]
    }
    17 => Collection {#264 ▼
      #items: array:4 [▶]
    }
  ]
}

The idea would be for me to put together a table, with the day, start time, end time and that time which would be the result of the above calculation.

If someone could help me think about the LOGIC of the thing I would already be eternally grateful.

    
asked by anonymous 18.10.2018 / 08:16

0 answers