IntlDateFormatter returning the wrong year

777 views Asked by At

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

2

There are 2 answers

0
Ruslan Gataullin On BEST ANSWER

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

1
AudioBubble On

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');