Hours difference between two dates in Laravel?

hours-difference-between-two-dates

In this blog, we will see how to get hours difference between two dates in Laravel.Whenever you need to get the difference between two dates in hours in laravel 5 then you can get easily using Carbon. Laravel by default provides the Carbon class. Carbon class through you can calculate the difference between two dates in hours.

In the Following example you can see, we have two dates, the first $to variable and second one $from the variable. Carbon class diffInhours function using you can get the difference between two dates. I hope you will find your solution.

Example

$to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-5 3:30:34');
$from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2015-5-5 9:30:34');
$diff_in_hours = $to->diffInHours($from);
print_r($diff_in_hours);// Output: 6
Do you want to hire us for your Project Work? Then Contact US.
Spread the love

2 thoughts on “Hours difference between two dates in Laravel?

Leave a Reply

Your email address will not be published. Required fields are marked *