skip to Main Content

At the end of each month i want to write down the month in serbian latin alphabeth format to write down the month in letters and the full year.

Example:

Januar 2024
Februar 2024 
...
Decembar 2024
etc.

This worked fine for all months except the edge case for December where the output resulted in:

Decembar 2025

Example:

$period = new DateTimeImmutable('2024-12-31');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL,  IntlDateFormatter::GREGORIAN, "MMMM Y");
$label =  datefmt_format($format, $period);

echo $label;

The output is: decembar 2025
the month is correct but the year changed

The result is the same even if the date is '2024-12-30', only when the date is lower then "2024-12-29" will the correct result show up "decembar 2024".

$period = new DateTimeImmutable('2024-12-30');
$format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL,  IntlDateFormatter::GREGORIAN, "MMMM Y");
$label =  datefmt_format($format, $period);

echo $label;

So my question is can anyone explain this weird interaction, i expected it has to do with Time zones, but that would explain why both 30 and 31st December would result the same. Also why does the year change but not he month, if it was a timezone but i would expect it to be January 2025, not December?

Tested on php7.4 and php8.2

Default timezone is: "Europe/Berlin"

2

Answers


  1. Look up what the Y symbol means:

    Symbol Meaning
    y year
    Y year in “Week of Year” based calendars in which the year transition occurs on a week boundary; may differ from calendar year ‘y’ near a year transition. This year designation is used with pattern character ‘w’ in the ISO 8601 year-week calendar, for example.

    Source: https://unicode-org.github.io/icu/userguide/format_parse/datetime/, linked to from PHP’s documentation.

    In other words, you want y, not Y.

    Login or Signup to reply.
  2. I believe it has todo with the formatter of the year, you should be able to use O instead of Y

    $period = new DateTimeImmutable('2024-12-31');
    $format = new IntlDateFormatter('sr-Latn', IntlDateFormatter::NONE, IntlDateFormatter::NONE, NULL,  IntlDateFormatter::GREGORIAN, "MMMM o");
    $label =  datefmt_format($format, $period);
    
    echo $label;
    

    This is what php writes about using o instead of y

    "ISO 8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead."

    https://www.php.net/manual/en/datetime.format.php

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search