Laravel 7/6 Calculate Time Difference Between Two Dates In Hours And Minutes

Laravel 6 , Laravel , Laravel 7

Nicesnippets

1384

21-02-2020


Hi Guys,

In this example, I will explain how to calculate time difference between two dates in hours and minutes in laravel,we will show calculate time difference between two dates in hours and minutes.we will carbon using time difference between two dates in hours and minutes.

I will show difference between two dates in hours and minutes using diffForHumans and diff methods in laravel we will diffForHumans method to show difference between two dates in hours and minutes.

Here following example of calculate time difference 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...


Recommended Posts