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

when i input carbon month February but its return mar #2737

Closed
alif989 opened this issue Dec 29, 2022 · 1 comment
Closed

when i input carbon month February but its return mar #2737

alif989 opened this issue Dec 29, 2022 · 1 comment
Labels
duplicate Solution/explanation on this issue has already been given in an other issue php bug Related to a bug of PHP

Comments

@alif989
Copy link

alif989 commented Dec 29, 2022

Hello,

I encountered an issue with the following code:

Carbon::parse('February')->format('M');

Carbon version: PUT HERE YOUR CARBON VERSION (exact version, not the range)

PHP version: PUT HERE YOUR PHP VERSION

I expected to get:

Feb

But I actually get:

Mar

Thanks!

@alif989 alif989 changed the title when i input carbon month February then its returns mar when i input carbon month February but its returns mar Dec 29, 2022
@alif989 alif989 changed the title when i input carbon month February but its returns mar when i input carbon month February but its return mar Dec 29, 2022
@kylekatarnls kylekatarnls added duplicate Solution/explanation on this issue has already been given in an other issue php bug Related to a bug of PHP labels Dec 29, 2022
@kylekatarnls
Copy link
Collaborator

Hello,

This comes directly from PHP parent class:

echo (new DateTime('February'))->format('M');

https://3v4l.org/4M2nr

It uses "now" as the start point and wants to keep the same day number so 29 then it does not find it in February and overflow to March.

The solution is to use an explicit start of point that does not trigger overflow such as the start of the month:

echo Carbon::now()->startOfMonth()->modify('February')->format('M');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Solution/explanation on this issue has already been given in an other issue php bug Related to a bug of PHP
Projects
None yet
Development

No branches or pull requests

2 participants