ofek Rozenkrantz
ofek Rozenkrantz

Reputation: 21

Carbon PHP add months

I want add Months from a date.
For example this my date 08/04/2019(D-M-Y)
I use the method addMonth() and I get: 08/05/2019(D-M-Y),
Which means carbon added exactly plus one to the value of M.
But in the calendar the date is 06/05/2019 (D-M-Y)...
This is my code:

$date = new Carbon('2019-04-08');
$date->addMonth() 

I want date will be after month like in the calendar.

Upvotes: 1

Views: 6118

Answers (2)

omar jayed
omar jayed

Reputation: 868

The difference between April 8, 2019 to May 6, 2019 is 28 days. Carbon's addMonth() adds 1 month to date provided. So, the output it provides is correct.

Sounds like you want to add 4 weeks to the date. For this you can use addWeeks() method.

Upvotes: 2

maximus1127
maximus1127

Reputation: 1036

just chain the methods:

$date =  Carbon::parse('2019-04-08')->addMonth()->format('m-d-Y');

Upvotes: 3

Related Questions