menu

Questions & Answers

Laravel 9 Calculate minute difference (Resolved)

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>
Comments:
2023-01-24 23:30:11
can you provide more information on what exactly you are trying to accomplish?
2023-01-24 23:30:11
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
Answers(1) :

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

Comments:
2023-01-24 23:30: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
2023-01-24 23:30:11
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?
2023-01-24 23:30:11
@foreach((array)$tickets_logs as $ticketlog) <td>{{ $ticketlog['id'] }}</td> <td>{{ __($ticketlog['states']) }}</td> <td>{{ $ticketlog['time_stamp'] }}</td> <td><td> echo (new Carbon('2023-01-11 15:26:23'))->diffInMinutes((new Carbon('2023-01-11 15:28:55'))); // 2 </td></td> </tr> @endforeach </tbody> </table>
2023-01-24 23:30:11
I've updated the answer, let me know if it works for you or if you have any other question regarding it
2023-01-24 23:30:11
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
2023-01-24 23:30:11
i forgot to add the carbon alias in the config file, loaded the code you suggested but I get this message ErrorException Undefined array key 0
2023-01-24 23:30:11
One small update, could you try it now? (I misspelled $tickets_logs variable)
2023-01-24 23:30:11
Thanks, now I see the results