Today now in this tutorial i will show you how to get months difference between two dates in laravel application. Whenever we want to get difference between two dates in months in laravel application then we can get this easily by using Carbon. In Laravel by default provide us Carbon class. we know Carbon class through we can calculate difference between two dates in months.
Just need to following example we can see this example, we have a two dates, first one is $to variable and second one is $from variable. Carbon class diffInMonths function by using we can get difference between two dates. So i hope you will find your needed solution.
$to = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2021-5-5 3:30:34'); $from = \Carbon\Carbon::createFromFormat('Y-m-d H:s:i', '2021-6-6 9:30:34'); $diff_in_months = $to->diffInMonths($from); print_r($diff_in_months); // Output: 1
Read Also :How to get last inserted id in Laravel?
Thanks for read. I hope it help you. For more you can follow us on facebook.