How to get hours difference between two dates in laravel?

By Hardik Savani | February 13, 2016 | | 48418 Viewer | Category : Laravel


Whenever you need to get difference between two dates in hours in laravel 5 then you can get easily using Carbon. Laravel by default provide Carbon class. Carbon class throught you can calculate difference between two dates in hours.

In Following example you can see, we have a two dates, first $to variable and second one $from variable. Carbon class diffInhours function using you can get 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



Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.