Today now in this blog i will show you how to get hours difference between two dates in laravel. When we work on laravel or any other programming application sometimes we need to get difference between two dates in hours . Then we can get it very easily by using Carbon. We know Laravel has by default provide Carbon class. Carbon class through we can calculate the difference between two dates in hours.
So now in Following example we can see, we had a two dates, at first one is $to variable and second one is in $from variable. Carbon class diffInhours function by using we can get the difference between two dates. I hope you are find your best solution.
$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
Thanks for read. I hope it help you. For more you can follow us on facebook.