0

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?

it works perfectly but now i need to specify the same same_id to not count all of the table but only the time referring to the same same_id

<table>
<thead>
<tr>
<th>id</th>
<th>timestamp</th>
<th>states</th>
<th>same_id</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>2023-01-11 15:26:23</td>
<td>NotAvailable</td>
<td>80</td>
<td>2</td>
<td>2023-01-11 15:26:55</td>
<td>ToBeAssigned</td>
<td>80</td>
<td>3</td>
<td>2023-01-11 15:27:06</td>
<td>Assigned</td>
<td>80</td>
<td>3</td>
<td>2023-01-11 15:27:19</td>
<td>TakingCharge</td>
<td>80</td>
<td>4</td>
<td>2023-01-11 15:29:05</td>
<td>Closed</td>
<td>80</td>
</tr>
</tbody>
</table>
Marco
  • 11
  • 4
  • can you provide more information on what exactly you are trying to accomplish? – RG Servers Jan 24 '23 at 08:39
  • I need to know how much time elapses between the various status changes and display it, if it is more than 15 minutes it is negative if it is more than 15 minutes it is positive – Marco Jan 24 '23 at 08:52

1 Answers1

0

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

Victor
  • 439
  • 3
  • 11
  • how to insert it to have the result in the whole table? I'm trying but I haven't managed to make it work automatically in the whole table – Marco Jan 24 '23 at 08:55
  • Do you have a `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? – Victor Jan 24 '23 at 09:12
  • @foreach((array)$tickets_logs as $ticketlog) {{ $ticketlog['id'] }} {{ __($ticketlog['states']) }} {{ $ticketlog['time_stamp'] }} echo (new Carbon('2023-01-11 15:26:23'))->diffInMinutes((new Carbon('2023-01-11 15:28:55'))); // 2 @endforeach – Marco Jan 24 '23 at 09:22
  • I've updated the answer, let me know if it works for you or if you have any other question regarding it – Victor Jan 24 '23 at 09:33
  • Thanks for the answer and the help you are providing me, now with the code you wrote me it gives me an error, Error Class "Carbon" not found – Marco Jan 24 '23 at 09:39
  • One small update, could you try it now? (I misspelled `$tickets_logs` variable) – Victor Jan 24 '23 at 09:45
  • it works perfectly but now i need to specify the same same_id to not count all of the table but only the time referring to the same same_id – Marco Jan 26 '23 at 08:11