Othman Boūlal
Othman Boūlal

Reputation: 81

Calculate difference in minutes between created_at and current time in Laravel

I want to show in a field the time someone is waiting, and I want to do that by subtracting created_at time by current time in minutes

this is my table

                                <thead>
                                <tr>
                                    <th scope="col">Ordernummer</th>
                                    <th scope="col">E-mail</th>
                                    <th scope="col">Kenteken</th>
                                    <th scope="col">Wachttijd</th>
                                    <th scope="col">Status</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($orders as $order)
                                    <tr>
                                        <th scope="row">{{ $order->ordernumber }}</th>
                                        <td scope="row">{{ $order->email }}</td>
                                        <th scope="row">{{ $order->numberplate }}</th>
                                        <td scope="row">{{ $order->created_at }}</td>
                                        <td scope="row">
                                            @if( $order->status == 0)
                                                Open
                                            @else
                                                Afgehandeld
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>

And where this is written <td scope="row">{{ $order->created_at }}</td> I want to show there the difference in minutes.

Thanks already

Upvotes: 1

Views: 3467

Answers (3)

OMR
OMR

Reputation: 12188

you can use Carbon::diffInMinutes to get the minutes differnt, or you can use Carbon::diffForHumans to get more readable statement

$order->created_at->diffInMinutes(Carbon::now());

// that would be: 5

$order->created_at->diffInMinutes(Carbon::now());

and this would be: 5 minuets ago

Upvotes: 1

Muhammad Dyas Yaskur
Muhammad Dyas Yaskur

Reputation: 8098

I assume your $order->created_at is Carbon instance, so you can use:

<td scope="row">{{ $order->created_at->diffInMinutes(\Carbon\Carbon::now()) }} minutes ago</td>

if $order->created_at is pure mysql format not Carbon instance just convert/create it:

\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $order->created_at)->diffInMinutes(\Carbon\Carbon::now())

Upvotes: 4

mmabdelgawad
mmabdelgawad

Reputation: 2545

You can use Carbon to calculate difference in minutes like so

<td scope="row">{{ now()->diffInMinutes($order->created_at) }}</td>

Upvotes: 1

Related Questions