I have store from_date and to_date in the database with MySQL DATE column type. How can I get date difference between from_date and to_date in format ..eg 1 Year 2 Months
2018-01-05
2019-02-22
2 Year 1 Month
I have store from_date and to_date in the database with MySQL DATE column type. How can I get date difference between from_date and to_date in format ..eg 1 Year 2 Months
2018-01-05
2019-02-22
2 Year 1 Month
Try this.
$datetime1 = new DateTime("2018-01-05");
$datetime2 = new DateTime("2019-02-22");
$difference = $datetime1->diff($datetime2);
echo !empty($difference->y) ? $difference->y.' Year ':'';
echo !empty($difference->m) ? $difference->m.' Month ':'';
The output will be
1 Year 1 Month
In carbon way
use Carbon\Carbon;
$startDate = Carbon::parse('2018-01-05');
$endDate = Carbon::parse('2019-02-22');
$diff = $startDate->diffInYears($endDate);
$diffs = $startDate->diffInMonths($endDate);
echo $diff;
echo $diffs;