1

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
jones
  • 749
  • 7
  • 34
Ye Htun Z
  • 2,079
  • 4
  • 20
  • 31
  • Hope you're looking for this. https://stackoverflow.com/questions/39508963/calculate-difference-between-two-dates-using-carbon-and-blade – jones Oct 10 '19 at 10:14

1 Answers1

0

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;
Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17