Laravel Calculate Time Difference Between Two Dates In Hours And Minutes

Laravel Calculate Time Difference Between Two Dates In Hours And Minutes

Hello Dev,

In this blog, i'm able to explain the way to calculate time difference among two dates in hours and mins in laravel,we will show calculate time distinction among dates in hours and mins.we can carbon the use of time difference between dates in hours and minutes.

I'm able to show difference among dates in hours and minutes the use of diffForHumans and diff techniques in laravel we will diffForHumans technique to reveal distinction between two dates in hours and mins.

Here following instance of calculate time distinction between two dates in hours and minutes in laravel

Example: 1

Now the example of diffForHumans method using time difference between two dates in hours and minutes in laravel.

/**
 * 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

Now the example of diff method using time difference between two dates in hours and minutes in laravel.

/**
 * 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"

It will help you...