0

i have mySQL database table field :

description
unit
start_date
end_date

for example :

start_date : 05-07-2019

end_date : 10-08-2021

how I calculate number of years?

ipi bk
  • 60
  • 1
  • 11
  • Have you looked into Carbon. This might help https://stackoverflow.com/questions/39508963/calculate-difference-between-two-dates-using-carbon-and-blade – daugaard47 Oct 04 '19 at 02:51

3 Answers3

4

Try this with carbon,

use Carbon\Carbon;


$startDate = Carbon::parse('05-07-2019'); 
$endDate = Carbon::parse('10-08-2021'); 
$diff = $startDate->diffInYears($endDate);

Hope this helps :)

Maulik Shah
  • 1,040
  • 1
  • 7
  • 17
1

Try

 $datetime1 = new DateTime("05-07-2019");
 $datetime2 = new DateTime("10-08-2021");
 $difference = $datetime1->diff($datetime2);
 echo 'Difference: '.$difference->y.' years, ' 
               .$difference->m.' months, ' 
               .$difference->d.' days';

Output will be

Difference: 2 years, 1 months, 5 days

Hope this helps :)

Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17
0

Try to use date_diff, in your example its will return 1.

$start_date = date_create("05-07-2019");
$end_date = date_create("10-08-2021");
$diff = date_diff($start_date, $end_date);
echo $diff->y;
HVD
  • 231
  • 1
  • 13