当我使用"IntlDateFormatter“时,为什么这个时间差?
<?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秒
发布于 2019-08-19 10:37:08
我认为,intlDateFormatter和DateTime在1890年以前是不兼容的。欧洲中期时间(MET)是1890年以后在欧洲逐步引进的。1890年初,每个地方都有自己的时间,视其经度而定。下面的代码为4个示例演示了这一点。
$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>";
}输出
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:20DateTime提供了其他结果。
https://stackoverflow.com/questions/50151076
复制相似问题