0
I’m having a question of how to set up the logic to calculate hours/minutes 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 field timestamp
and the second the field tipo
.
Issue 1
That field tipo
is to determine 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 of the "I" with the "O", decreasing with the difference of "II" and "OI".
Format and pick only the schedules I can do quietly with Carbon.
My biggest problem is being how to compare the tipo
(know which the first and which the last) and also how not to mix the days, after all there are more results. That is, my query returns records of several days and I’m having difficulty to separate only the records of a specific day, to be able to 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');
});
Returns:
Collection {#269 ▼
#items: array:2 [▼
18 => Collection {#285 ▼
#items: array:4 [▶]
}
17 => Collection {#264 ▼
#items: array:4 [▶]
}
]
}
The idea would be I assemble a table, with the day, start time, end time and that time that would be the result of the above calculation.
If anyone could help me think of the LOGIC of the thing I would be eternally grateful.
But what is the purpose of this second 'type' field (that is the name of the field?) and what values can it contain? He will always have only letters, and the order is alphabetical? And, let me see if I understand: Given a date, 18/10/2018, for example, you want to subtract the smallest time from the biggest time of that day (max - min), and then, from that result, you want to subtract all the intervals that exist between the smallest and the largest times of that day?
– Pedro Gaspar
Thanks for the interest, this type can have those 4 values only, it shows if it is input, interval, back of interval or output.
– Guto G
Can you post in the question the code snippet you already have? I understand the problem better now, but I still don’t understand exactly what your question is. Are you already able to return these time records from a certain date? You don’t know the code to do the math to subtract these times?
– Pedro Gaspar
Then this, I’m not getting the logic of this, I’m just getting all the user records, I wanted to know how I’m going to get, the 4 records of day x to calculate, remembering that I don’t spend a day as a specific parameter, because I will have to calculate for all system records (in case every day).
– Guto G
So, but how do you get these records? How are they? In an array? As I said, put an excerpt of the code in your question, which will be easier to understand.
– Pedro Gaspar
Yes, each record has a record_id, type, date (timestamp), and an id of who created it. The return is in an array with all records.
– Guto G
I thought in some way, group the records by date, being 4 records by date allowed, I would have all types grouped by date. That would be consistent?
– Guto G
Briefly, I was able to group the four records of a day. $points = Records::Where('user_id', '=', Auth::user()->id)->get(); $pointsByDate = $points->groupBy('date');
– Guto G