Carbon: Is it a bug?

Created on 29 Mar 2018  路  4Comments  路  Source: briannesbitt/Carbon

I test the time around 2018-3-29~2018-3-31:
$month = Carbon::now()->subMonth(1);
the result is 3, but it should be 2.why this? Thanks

Most helpful comment

If today is 2018-03-29
SubMonth makes that 2018-02-29
2018 is not a leap year, so DateTime automatically corrects that to 2018-03-01

This is well-documented DateTime behavior that has existed for a very long time.

All 4 comments

If today is 2018-03-29
SubMonth makes that 2018-02-29
2018 is not a leap year, so DateTime automatically corrects that to 2018-03-01

This is well-documented DateTime behavior that has existed for a very long time.

To fix this use:
$month = Carbon::now()->firstOfMonth()->subMonth();

Or use Carbon::useMonthsOverflow(false);

Read more here: http://carbon.nesbot.com/docs/#api-addsub

Or $month = Carbon::now()->subMonthsNoOverflow(1);

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfn picture mfn  路  4Comments

chanux picture chanux  路  5Comments

Unibozu picture Unibozu  路  5Comments

scs-ben picture scs-ben  路  3Comments

Yo1L picture Yo1L  路  5Comments