Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

timestamp - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...