// create two Carbon instances $date1 = Carbon::parse('2020-10-01'); $date2 = Carbon::parse('2018-01-15'); // calculate the difference between the two dates $diff = $date1->diff($date2); // output the difference in years, months and days echo "The difference between the two dates is " . $diff->y . " years, " . $diff->m . " months and " . $diff->d . " days.";
// create two Carbon instances $date1 = Carbon::parse('2020-10-01 12:00:00'); $date2 = Carbon::parse('2020-10-01 16:30:00'); // calculate the difference between the two dates $diff = $date1->diff($date2); // output the difference in hours and minutes echo "The difference between the two dates is " . $diff->h . " hours and " . $diff->i . " minutes.";This example uses the Carbon package library as well. In both example codes shown, the Carbon package library was used.