Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Carbon error with year in difference between dates

Good afternoon... I try to make the difference in years between the current date and a date in the database like this:

$final = '2016-05-10'; //date database
$hoy = \Carbon\Carbon::now(); //today
$fecha_ingreso = \Carbon\Carbon::createFromFormat('Y-m-d', $final);

$diff = $hoy->diffInYears($fecha_ingreso); //diff

But when doing a var_dump() of $fecha_ingreso it shows me the following:

object(Carbon\Carbon)#259 (3) { ["date"]=> string(26) "2155-05-10 22:27:09.000000" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }

The day and the month are ok but the year is not, why can this error be ?? Thank you very much for the help.

like image 969
awdesarrollos Avatar asked Sep 15 '25 19:09

awdesarrollos


1 Answers

I assume you want the difference in number of years, try this.

$dbDate = \Carbon\Carbon::parse('2016-05-10');
$diffYears = \Carbon\Carbon::now()->diffInYears($dbDate);

// $diffYears is 1
like image 122
Sandeesh Avatar answered Sep 18 '25 08:09

Sandeesh