Skip to content

IntlDateFormatter::format() displays weekday and week number incorrectly. #14500

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

Closed
Wiimm opened this issue Jun 7, 2024 · 2 comments
Closed

Comments

@Wiimm
Copy link

Wiimm commented Jun 7, 2024

Description

Class IntlDateFormatter displays weekday and week number incorrectly. The example date is 2021-01-02, which belongs to the last week of 2020. In addition, the number of the day of the week is displayed incorrectly.

<?PHP

# tested with PHP 7.2.5 + 7.4.6 + 8.3.6 + 8.3.7
# output always:
#> SFT: 2021-01-02 12:00:00 / wday: 6=Sat / week: 2020-53
#> IDF: 2021-01-02 12:00:00 / wday: 7=Sat / week: 2021-01
# ERROR:                            ^             ^^^^ ^^

$locale   = 'en_US.UTF-8';
$timezone = 'UTC';
$time     = strtotime('2021-01-02 12:00');

setlocale(LC_TIME,$locale);
date_default_timezone_set($timezone);

echo strftime('SFT: %F %T / wday: %u=%a / week: %G-%V%n',$time);

$idf = new IntlDateFormatter(
		$locale,
		\IntlDateFormatter::FULL,
		\IntlDateFormatter::FULL,
		$timezone,
		\IntlDateFormatter::GREGORIAN,
		"yyyy-MM-dd HH:mm:ss '/ wday:' e=E '/ week:' Y-ww\n" );
echo 'IDF: ',$idf->format($time);
?>

Resulted in this output:

SFT: 2021-01-02 12:00:00 / wday: 6=Sat / week: 2020-53
IDF: 2021-01-02 12:00:00 / wday: 7=Sat / week: 2021-01
                                 ^             ^^^^ ^^

But I expected this output instead:

SFT: 2021-01-02 12:00:00 / wday: 6=Sat / week: 2020-53
IDF: 2021-01-02 12:00:00 / wday: 6=Sat / week: 2020-53
                                 ^             ^^^^ ^^

PHP Version

PHP 7.2.5 .. 8.3.7

Operating System

openSUSE Leap + Tumbleweed, Debian 11

@damianwadley
Copy link
Member

damianwadley commented Jun 7, 2024

The issue is the definition of the IntlDateFormatter::GREGORIAN calendar: it has the first week of the ISO year using a minimum of 1 day, instead of the usual 4 days. It also has Sunday as the first day of the week instead of Monday. So that's why it says Saturday is 7 and the week is 2021-01.

If I create an IntlCalendar with Monday as the first day of the week and a minimum of 4 days in the first week, like the 8601 standard typically uses, and pass that to IntlDateFormatter, I get 6=Sat and 2020-53.

"Typically". We've seen this problem before.

@Wiimm Wiimm closed this as completed Jun 8, 2024
@Wiimm
Copy link
Author

Wiimm commented Jun 8, 2024

If I add

    $ic = IntlCalendar::createInstance('UTC');
    $ic->setMinimalDaysInFirstWeek(4);
    $ic->setFirstDayOfWeek(IntlCalendar::DOW_MONDAY);

and use the new object instead of IntlDateFormatter::GREGORIAN, then it works like expected.

@damianwadley Thank you for the info.

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

No branches or pull requests

3 participants