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

Translated months names off #3097

Closed
PascaleBeier opened this issue Oct 31, 2024 · 2 comments
Closed

Translated months names off #3097

PascaleBeier opened this issue Oct 31, 2024 · 2 comments

Comments

@PascaleBeier
Copy link

PascaleBeier commented Oct 31, 2024

Hello,

I encountered an issue with the following code:

foreach (range(1,12) as $month) {
    echo \Carbon\Carbon::now()->setMonth($month)->getTranslatedMonthName() . '<br>';
}

Carbon version: 3.8.0

PHP version: 8.3.12

I expected to get:

January
February
March
April
May
June
July
August
September
October
November
Dezember

But I actually get:

January
March
March
May
May
July
July
August
October
October
December
December

Examples:

https://phpsandbox.io/e/x/6n2z7?layout=EditorPreview&defaultPath=%2F&theme=dark&showExplorer=no&openedFiles=

This works:

foreach (range(1,12) as $month) {
    echo \Carbon\Carbon::now()->setDay(1)->setMonth($month)->getTranslatedMonthName() . '<br>';
}
@PascaleBeier
Copy link
Author

Seems to be a duplicate of #2317

@kylekatarnls
Copy link
Collaborator

kylekatarnls commented Nov 1, 2024

Hello, if you want to get translated month name from month number, now is irrelevant, you'd better directly create a date from this number rather than creating an object then modifying it:

foreach (range(1, 12) as $month) {
    echo \Carbon\Carbon::create(2000, $month, 1)->getTranslatedMonthName() . '<br>';
}

2000 is a completely arbitrary year, any number would work the same.

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

2 participants