I have a column with id and a column with timestamp and a state column, every time the state changes it writes a time in the timestamp column, I want a calculation of the time difference between the various states, for example if it is less than 15 minutes it is ok, if more than 15 minutes is not ok, how can I do to automate it?
<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2023-01-11 15:26:23</td>
<td>NotAvailable</td>
<td>2</td>
<td>2023-01-11 15:26:55</td>
<td>ToBeAssigned</td>
<td>3</td>
<td>2023-01-11 15:27:06</td>
<td>Assigned</td>
<td>3</td>
<td>2023-01-11 15:27:19</td>
<td>TakingCharge</td>
<td>4</td>
<td>2023-01-11 15:29:05</td>
<td>Closed</td>
</tr>
</tbody>
</table>
If you use Carbon, you should take a look at diffInMinutes
method (read more about that here)
Example:
echo (new Carbon\Carbon('2023-01-11 15:26:23'))->diffInMinutes((new Carbon\Carbon('2023-01-11 15:28:55'))); // 2
If you want to do it with plain PHP, please have a look at this answer: https://stackoverflow.com/a/12382882/14714168
Update:
With the data you provided, I think one way to get it is the following:
<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
<th>delay</th>
</tr>
</thead>
<tbody>
@foreach((array)$tickets_logs as $i => $ticket_log)
<tr>
<td>{{ $ticket_log['id'] }}</td>
<td>{{ __($ticket_log['states']) }}</td>
<td>{{ $ticket_log['time_stamp'] }}</td>
<td>
@if(!$loop->first)
{{ (new Carbon\Carbon($tickets_logs[$i - 1]['time_stamp']))->diffInMinutes((new Carbon\Carbon($ticket_log['time_stamp']))) }}
@else
-
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
read more about loop variables here
foreach
loop that generates the rows? Could you add that in your code as well so I can show you what needs to be changed? $tickets_logs
variable)