首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用"IntlDateFormatter“时,为什么这个时间差?

当我使用"IntlDateFormatter“时,为什么这个时间差?
EN

Stack Overflow用户
提问于 2018-05-03 09:06:45
回答 1查看 161关注 0票数 3

当我使用"IntlDateFormatter“时,为什么这个时间差?

代码语言:javascript
复制
<?php

// php v7.1

$pattern = 'yyyy-MM-dd HH:mm:ss';
$timezone = "Europe/Budapest";
$inputDateTimeStr = '1890-01-01 00:00:00';
$locale = 'hu_HU';

$intlDateFormatter = new \IntlDateFormatter( $locale,  \IntlDateFormatter::MEDIUM, \IntlDateFormatter::SHORT, \IntlTimeZone::createTimeZone($timezone), \IntlDateFormatter::GREGORIAN, $pattern
);

$dateTime = new \DateTime($inputDateTimeStr);
$dateTime->setTimezone(new \DateTimeZone($timezone));

$outputDateTimeStr = $intlDateFormatter->format($dateTime->getTimestamp());

print ' in: ' . $inputDateTimeStr
 . ' out: ' . $outputDateTimeStr // string(19) "1890-01-01 00:16:20"
 . ' diff: ' . ( strtotime($outputDateTimeStr) - strtotime($inputDateTimeStr) ) . ' seconds';

输出: in: 1890-01-01 : 00:00:00 :00: 1890-01-01:16:20 diff:980秒

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-19 10:37:08

我认为,intlDateFormatter和DateTime在1890年以前是不兼容的。欧洲中期时间(MET)是1890年以后在欧洲逐步引进的。1890年初,每个地方都有自己的时间,视其经度而定。下面的代码为4个示例演示了这一点。

代码语言:javascript
复制
$refDate = date_create('1890-01-01 UTC');
$ts = $refDate->getTimeStamp();
$pattern = 'yyyy-MM-dd HH:mm:ss';
$timezones = [
  "Europe/Paris",  //2.3522° E
  "Europe/Berlin", //13.4050° E
  "Europe/Prague", //14.4378° E
  "Europe/Budapest"//19.0402° E
];
$locale = 'hu_HU';

foreach($timezones as $timezone){
  $intlDateFormatter = new \IntlDateFormatter(
    $locale,  
    \IntlDateFormatter::MEDIUM, 
    \IntlDateFormatter::SHORT, 
    \IntlTimeZone::createTimeZone($timezone), 
    \IntlDateFormatter::GREGORIAN, 
    $pattern
  );

  $outputDateTimeStr = $intlDateFormatter->format($ts);
  echo $timezone.": ".$outputDateTimeStr."<br>";
}

输出

代码语言:javascript
复制
Europe/Paris: 1890-01-01 00:09:21
Europe/Berlin: 1890-01-01 00:53:28
Europe/Prague: 1890-01-01 00:57:44
Europe/Budapest: 1890-01-01 01:16:20

DateTime提供了其他结果。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50151076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档