Nasma Najeeb
Nasma Najeeb

Reputation: 17

how to get current year date and the year count by passing a date in Laravel Carbon?

I'm using Carbon, I expect two functions in my application and I hope those can be done using any Carbon methods. I tried to find it on the internet and could not find an exact answer to my question. So I'm posting it here with the hope someone might help me. Thank you.

what are the functions I expect is:

for example, an event that happened on 2014-05-15

using above date I need to find the event date from current year and how many years for the event now as below,

event from the current year - 2021-05-15 years for the event - 7 years

is there a possible way to do these using any Carbon inbuilt functions?

(I expect this function to a big process in my code and here it's a simple example what should happen)

Upvotes: 0

Views: 1176

Answers (1)

habeebdev
habeebdev

Reputation: 278

Use Carbon::diffInYears

$date = Carbon::parse('2021-05-15');
$diffYears = Carbon::now()->diffInYears($date);

Refer Carbon doc for more diff methods https://carbon.nesbot.com/docs/#api-difference

Upvotes: 1

Related Questions