IntlDateFormatter returning the wrong year
Asked Answered
S

2

5

If you run the following code

<?php

$date = new DateTime('2019-12-30 19:45:00', new DateTimeZone('Europe/London'));
$formatter = new IntlDateFormatter(
    'en_GB',
    null,
    null,
    'Europe/London',
    IntlDateFormatter::GREGORIAN,
    'MMMM YYYY'
);

echo $formatter->format($date);

it'll echo December 2020. Is this a bug with the class, or have I got something wrong, as I'd expect it to return December 2019

Sundin answered 30/11, 2019 at 10:10 Comment(0)
M
10

According to: https://unicode-org.github.io/icu/userguide/format_parse/datetime/

Y stand for year of "Week of Year"(whatever it means) and y is regular year reference we got used to. So to get current year replace YYYY with yyyy and it should do the job

Midden answered 30/11, 2019 at 10:14 Comment(0)
K
1

You are using this function in a bad way. I understand that the ideea is to format the Date and Time in a local specific way.

To write the date and time like a Englishman would do :

Monday, 30 December 2019 at 19:45:00 Greenwich Mean Time

in the USA Mister Trump would say :

Monday, December 30, 2019 at 7:45:00 PM Greenwich Mean Time

and for Germany I would say something like :

Montag, 30. Dezember 2019 um 19:45:00 Mittlere Greenwich-Zeit

Torwald Linus prolly would say in Finland :

måndag 30 december 2019 kl. 19:45:00 Greenwichtid

and finally in Honduras Pedro de Alvarado would say:

lunes 30 de diciembre de 2019, 19:45:00 hora del meridiano de Greenwich

(I love this function :) )

because if you desire a result like 2019-12-30 you probably would use format

$date = new DateTime('2019-12-30 19:45:00', new DateTimeZone('Europe/London'));

echo $date->format('Y-m-d');
Katabasis answered 30/11, 2019 at 11:0 Comment(1)
The code is actually a simplified version of how it's actually used in production, the format 'MMMM YYYY' is actually passed into a function, so it could be used for changing to a local specific way.Sundin

© 2022 - 2024 — McMap. All rights reserved.