Hello Devs,

In this tutorial, we are going to learn how to calculate time difference between two dates in hours and minutes in laravel.


Example: 1

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{

    $startTime = Carbon::parse('2020-02-11 04:04:26');
    $endTime = Carbon::parse('2020-02-11 04:36:56');

    $totalDuration = $endTime->diffForHumans($startTime);
    dd($totalDuration);
}

OUTPUT:

"32 minutes after"


Example: 2

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{

    $startTime = Carbon::parse('2020-02-11 04:04:26');
    $endTime = Carbon::parse('2020-02-11 04:36:56');

    $totalDuration =  $startTime->diff($endTime)->format('%H:%I:%S')." Minutes";
    dd($totalDuration);
}

OUTPUT:

"00:32:30 Minutes"

I hope this example helps you.