Home > Mobile >  Laravel : calculate before inserting into DB
Laravel : calculate before inserting into DB

Time:12-15

I have a form and before saving it in the DB is it possible to make a calculation?

I get a date of birth and want to make an account with the current year.

Example: 10-10-1990 and I want it to subtract from the current year.

1990 - 2021

$tabel->datenasc = $request->datenasc;

CodePudding user response:

You can still do your calculations before inserting into DB

// if $request->datenasc == 10-10-1990
$current_year = date("Y");
list($day, $month, $year) = explode("-", $request->datenasc);
$year_diff = $current_year - $year;

you can now insert $year_diff in the database;

CodePudding user response:

You can use setYear method from carbon library

$date = Carbon::parse("10-10-1990");

$dateWithCurrentYear =   $date->setYear(now()->format('Y'));
  • Related