Level 33
Try this
public function EmploymentDateDiff($emp_date)
{
$emp_date = Carbon::parse($emp_date);
$now = Carbon::now();
return $emp_date->diffInDays($now);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have this model:
class HrEmployee extends Model
{
protected $table = 'hr_employees';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'hired_date',
'firs_name',
'last_name',
];
public function getEmploymentDatettribute($date)
{
return Carbon::parse($date);
}
}
The hired_date is getEmploymentDatettribute($date)
In my Controller, how do I get the difference between the employment date and current date (today) in months (for example 7)?
Thank you
if you want to do that on a collections you can use appends array in the model and set a new accessor.
in the accessor you can write something like this
Carbon::now()->diffInMonths($employee->employee_date)
Please or to participate in this conversation.