Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in subMonth #639

Closed
cmachu opened this issue Mar 30, 2016 · 9 comments
Closed

Bug in subMonth #639

cmachu opened this issue Mar 30, 2016 · 9 comments

Comments

@cmachu
Copy link

cmachu commented Mar 30, 2016

Code:

$date = [
'now' => Carbon::parse('2016-03-30'),
'prev_month' => Carbon::parse('2016-03-30')->subMonth()
];

return:

[now] => Carbon\Carbon Object
    (
        [date] => 2016-03-30 00:00:00.000000
        [timezone_type] => 3
        [timezone] => Europe/Warsaw
    )

[prev_month] => Carbon\Carbon Object
    (
        [date] => 2016-03-01 00:00:00.000000
        [timezone_type] => 3
        [timezone] => Europe/Warsaw
    )

for other months work correctly.

@EselDompteur
Copy link

same issue here

Carbon::now('Europe/Berlin')-toDateTimeString() => 2016-03-30 20:11:31
Carbon::now('Europe/Berlin')->subMonth()->toDateTimeString() => 2016-03-01 20:11:31

Workaround:

Carbon::now()->startOfMonth()->subSecond()->startOfMonth() => 2016-02-01 00:00:00

regards

@lybc
Copy link

lybc commented Mar 31, 2016

I met the same question

today is 2016/3/31

$startTime = new Carbon('this month first day') // echo 2016-04-01
$now = Carbon::createFromDate(2016, 3, 1);
$last3Month = $now->subMonth(3);
$last2Month = $now->subMonth(2); 
$lastMonth = $now->subMonth(1);

Carbon\Carbon Object
(
    [date] => 2015-09-01 16:17:30.000000
    [timezone_type] => 3
    [timezone] => Etc/GMT-8
)
Carbon\Carbon Object
(
    [date] => 2015-09-01 16:17:30.000000
    [timezone_type] => 3
    [timezone] => Etc/GMT-8
)
Carbon\Carbon Object
(
    [date] => 2015-09-01 16:17:30.000000
    [timezone_type] => 3
    [timezone] => Etc/GMT-8
)

@davidianbonner
Copy link

Experiencing the same issue here. It happens with addMonth as well.

It appears the bug lies with php's DateTime and not Carbon. The Carbon::addMonthand Carbon::subMonth method directly use DateTime::modify.

A simple workaround to avoid this:

$nextMonth = (new Carbon($date))->endOfMonth()->addDay()->startOfDay();
$prevMonth = (new Carbon($date))->startOfMonth()->subDay()->endOfDay();

@dannyweeks
Copy link

I think the problem is deeper than this as I am having issues with even creating the correct object.

Carbon::createFromFormat('Y-m', '2016-02');
// or
Carbon::create(2016, 2, null, null, null, null, 'Europe/London');

The above returns an object of 2016-03-02;

Adding the first day of the month fixes the issue.

Carbon::create(2016, 2, 1, null, null, null, 'Europe/London');

@lucasmichot
Copy link
Collaborator

Everyone,
this is related to the fact that there was a change of time (winter time to summer time) on this period. This is the way \DateTime works too.
It's a bit tricky sometimes so I always take the dst flag into consideration and use the ...Month(s)NoOverflow functions as a workaround.

@lucasmichot
Copy link
Collaborator

Can it be closed @cmachu ?

@cmachu
Copy link
Author

cmachu commented May 5, 2016

Yeap, thanks !

@gerfin
Copy link

gerfin commented Sep 30, 2018

what i fixed it by add "->subDay(3)",becaue february is 2-3 days less than other months.
for example:
what i want:
Carbon::now()->subMonth()->endOfMonth()
fixed it by:
Carbon::now()->subMonth()->subDay(3)->endOfMonth()

@kylekatarnls
Copy link
Collaborator

Don't do that, if you're january the 1st, then you will get the end of november.

Simply do: Carbon::now()->startOfMonth()->subMonth()->endOfMonth()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants