Comparison of hours in Laravel

Asked

Viewed 132 times

5

I’m trying to create a method that will return the amount of minutes since the last insertion in the bank. That’s the way I did it, but I’m making a mistake:

$now = Carbon::now();
$minutes = LusLeadUpdateStatus::select('created_at')
     ->orderby ('created_at', 'desc')
     ->get()
     ->toArray();

$totalDuration = $minutes->diffInMinutes($now);
(// o erro é aqui, pois dando print_r($minutes) o resultado é algo assim:
    [0] => Array
    (
        [created_at] => 2018-08-30 14:55:35
    )

    [1] => Array
    (
        [created_at] => 2018-08-30 14:55:20
    )
)

the created_at field is where I have the entry in the database, and it is in date / time format.

  • Try: $minutes = LusLeadUpdateStatus::select('created_at')
 ->orderby ('created_at', 'desc')
 ->first(), and after you initialize $totalDuration how you’re doing sees the return of dd($totalDuration);

1 answer

6

This was the final answer I got(working properly):

$lusLeadUpdateStatus = LusLeadUpdateStatus::select('created_at')
            ->orderby('created_at', 'desc')
            ->first();

        if ($lusLeadUpdateStatus) {
            $now = Carbon::now();

            $lastDateTime = $lusLeadUpdateStatus->toArray();
            $lastDateTime = $lastDateTime['created_at'];

            return $now->diffInMinutes($lastDateTime);
        }
        return false;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.