Felix
Felix

Reputation: 2661

Getting time difference between 2 datetimes in Laravel blade?

I have 2 datetimes, the first is a user's last login date

{{ $message->recipient->last_login }}

which comes out like "2017-05-11 02:56:18" in the datetime format, and the current time

{{ date('Y-m-d H:i:s') }}

which comes out in the same format.

Essentially what I want is to get the time difference between the two times.

Something like

@if (the time difference between the 2 dates is less than 10 minutes)
do this
@endif

Upvotes: 3

Views: 9427

Answers (2)

Jaykumar Patil
Jaykumar Patil

Reputation: 359

You must choose Carbon dates. Add following in your controller and pass it on to blade.

$currentTime = Carbon::now();

In blade template use this as follows:

@if($currentTime->diffInMinutes($message->recipient->last_login) < 10)
    // your code
@endif

Upvotes: 7

Sahil Gulati
Sahil Gulati

Reputation: 15141

Try this simplest one, Here we here we are getting seconds from time using strtotime.

Laravel blade syntax:

{{intval((strtotime(date('Y-m-d H:i:s'))-strtotime("2017-05-11 02:56:18"))/60)}}

Try this code snippet here

<?php

//getting time difference in seconds.
$secondsDifference=strtotime(date('Y-m-d H:i:s'))-strtotime('2017-05-11 02:56:18');
//converting seconds to minutes.
echo intval($secondsDifference/60);

Upvotes: 0

Related Questions