Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to get the difference between employment date and current date in month

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

0 likes
5 replies
a4ashraf's avatar

@noblemfd

Try this

 public function EmploymentDateDiff($emp_date)
 {
	$emp_date = Carbon::parse($emp_date);
	$now = Carbon::now();

	return  $emp_date->diffInDays($now);
}
Phread's avatar

or via SQL:

DB::raw('TIMESTAMPDIFF (YEAR,tableName.fieldName , CURDATE()) as main_age')

Just depends on where you want the work done.

amora2972's avatar
Level 1

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)

raheelkhan's avatar

You can use Carbon to perform that

use Carbon\Carbon;
$current_date = Carbon::now();
$difference = $current_date->diffInMonths($employee_date);

echo $difference;

That's it.

Please or to participate in this conversation.