简体   繁体   English

PHP与IntlDateFormatter

[英]PHP with IntlDateFormatter

There's a script: 有一个脚本:

for($i=80; $i<=89; $i++)
{
    $date = '01.04.19'.$i;
    $intlDateFormatter = new \IntlDateFormatter('ru', 2, -1, 'Europe/Moscow', 1, 'dd.MM.yyyy');
    $intlDateFormatter->setLenient(false);
    $timestamp = $intlDateFormatter->parse($date);
    echo $date . ' = ' . var_export($timestamp, 1);
    echo '<br>' . PHP_EOL;
}

And script outputs: 和脚本输出:

01.04.1980 = 323384400
01.04.1981 = false
01.04.1982 = false
01.04.1983 = false
01.04.1984 = false
01.04.1985 = 481147200
01.04.1986 = 512683200
01.04.1987 = 544219200
01.04.1988 = 575841600
01.04.1989 = 607377600

What's wrong with dates: 01.04.1981, 01.04.1982, 01.04.1983, 01.04.1984? 日期有什么问题:01.04.1981,01.04.1982,01.04.1983,01.04.1984? PHP 5.5.9-1 PHP 5.5.9-1

I think it is because in those years Moscow changed to Daylight Savings Time on April 1st so the time between 00:00:00 and 00:01:00 didn't exist. 我想这是因为那些年莫斯科在4月1日改为夏令时,所以00:00:00到00:01:00之间的时间不存在。 In 1985 they changed a day earlier and in 1980 they didn't change at all 1985年,他们提前一天改变,1980年他们根本没有改变

You can see details of time/date changes at; 您可以看到时间/日期变化的详细信息; http://www.timeanddate.com/time/change/russia/moscow?year=1982 http://www.timeanddate.com/time/change/russia/moscow?year=1982

For resolve the problem, need to use UTC. 要解决此问题,需要使用UTC。

In Symfony's 2.1.* case: 在Symfony的2.1。*案例中:

    'data_timezone' => 'UTC',
    'user_timezone' => 'UTC',

Symfony 2.3+: Symfony 2.3+:

    'model_timezone' => 'UTC',
    'view_timezone' => 'UTC',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM