Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP IntlDateFormatter format returns wrong date

I'm getting incorrect results using IntlDateFormatter:

$dateFormater = \IntlDateFormatter::create(
'en_EN',
\IntlDateFormatter::LONG,
\IntlDateFormatter::NONE
);

$a = date('Y/m/d H:i:s',1332712800); //2012/03/26 00:00:00
$b = $dateFormater->format(1332712800); //March 25, 2012

But this only happens for dates between 2012/03/26 and 2012/10/28 and without hour (00:00:00).

I can't find out what is the problem.

Thanks for the help.

like image 727
marcosberm Avatar asked Apr 28 '26 16:04

marcosberm


1 Answers

http://userguide.icu-project.org/datetime/timezone#TOC-Factory-Methods-and-the-Default-Tim says

TimeZone maintains a static time zone object known as the default time zone. This is the time zone that is used implicitly when the user does not specify one. ICU attempts to match this to the host OS time zone.

In short, if you want to change the default timezone from intl to match what date() says, you must change the time zone on your operating system. But don't do that.

It is preferred that you specify the timezone in the call to IntlDateFormatter::create(). If you wish to use the default timezone that PHP is using elsewhere, that can be retrieved with date_default_timezone_get().

$dateFormater = \IntlDateFormatter::create(
    'en_EN',
    \IntlDateFormatter::LONG,
    \IntlDateFormatter::NONE,
    date_default_timezone_get()
);
like image 172
salathe Avatar answered May 01 '26 05:05

salathe