php – IntlDateFormatter()和DateTime()输出相比,时间戳错误/怪异。

huangapple go评论67阅读模式
英文:

php - IntlDateFormatter() wrong/weird timestamp in comparison to DateTime() output

问题

我注意到IntlDateFormatter()函数返回的时间戳与DateTime()函数的相同类型的输出相比是错误的。

PHP:

$formatter = new IntlDateFormatter(
	'en_GB', 
	IntlDateFormatter::SHORT, 
	IntlDateFormatter::SHORT, 
	'Europe/London', 
	IntlDateFormatter::GREGORIAN,
	'dd MMMM YYYY, HH:mm'
);

$now = new DateTime('01-03-2023 17:00');						
echo '<b>DateTime() String:</b> ' . $now->format('d F Y, H:i') . '<br/>';
echo '<b>IntlDateFormatter() String:</b> ' . $formatter->format($now) . '<br/><br/>';
echo '<b>DateTime() Timestamp:</b> ' . $now->getTimestamp() . '<br/>';
echo '<b>IntlDateFormatter() Timestamp:</b> ' . $formatter->parse($formatter->format($now));

输出:

DateTime() String: 01 March 2023, 17:00

IntlDateFormatter() String: 01 March 2023, 17:00

DateTime() Timestamp: 1677690000

IntlDateFormatter() Timestamp: 1672074000

如上所示,IntlDateFormatter()返回了正确的字符串,但是与相同来源的时间戳值不一致。为什么会发生这种情况?

英文:

I noticed that IntlDateFormatter() function returns wrong timestamp in comparison to same type of output from DateTime() function.

PHP:

	$formatter = new IntlDateFormatter(
		&#39;en_GB&#39;, 
		IntlDateFormatter::SHORT, 
		IntlDateFormatter::SHORT, 
		&#39;Europe/London&#39;, 
		IntlDateFormatter::GREGORIAN,
		&#39;dd MMMM YYYY, HH:mm&#39;
	);

	$now = new DateTime(&#39;01-03-2023 17:00&#39;);						
	echo &#39;&lt;b&gt;DateTime() String:&lt;/b&gt; &#39; . $now-&gt;format(&#39;d F Y, H:i&#39;) . &#39;&lt;br/&gt;&#39;;
	echo &#39;&lt;b&gt;IntlDateFormatter() String:&lt;/b&gt; &#39; . $formatter-&gt;format( $now ) . &#39;&lt;br/&gt;&lt;br/&gt;&#39;;
	echo &#39;&lt;b&gt;DateTime() Timestamp:&lt;/b&gt; &#39; . $now-&gt;getTimestamp() . &#39;&lt;br/&gt;&#39;;
	echo &#39;&lt;b&gt;IntlDateFormatter() Timestamp:&lt;/b&gt; &#39; . $formatter-&gt;parse( $formatter-&gt;format( $now ) );

OUTPUT:

> DateTime() String: 01 March 2023, 17:00
>
> IntlDateFormatter() String: 01 March 2023, 17:00
>
> DateTime() Timestamp: 1677690000
>
> IntlDateFormatter() Timestamp: 1672074000

As is visible above, IntlDateFormatter() returns good string, but a bad timestamp value from the same source. Why is that happen?

答案1

得分: 1

当调用 $formatter->parse($formatter->format($now)) 时,解析方法将周年份值解释为年份值,导致时间戳与 DateTime 对象的时间戳不同。

为了获得一致的输出,您可以在 IntlDateFormatter 中使用小写的 yyyy 而不是大写的 YYYY

$formatter = new IntlDateFormatter(
    'en_GB', 
    IntlDateFormatter::SHORT, 
    IntlDateFormatter::SHORT, 
    'Europe/London', 
    IntlDateFormatter::GREGORIAN,
    'dd MMMM yyyy, HH:mm'
);

$now = new DateTime('01-03-2023 17:00', new DateTimeZone('Europe/London'));

echo '<b>DateTime() String:</b> ' . $now->format('d F Y, H:i') . '<br/>';
echo '<b>IntlDateFormatter() String:</b> ' . $formatter->format( $now ) . '<br/><br/>';

echo '<b>DateTime() Timestamp:</b> ' . $now->getTimestamp() . '<br/>';
echo '<b>IntlDateFormatter() Timestamp:</b> ' . $formatter->parse( $formatter->format( $now ) ) . '<br/><br/>';

echo '<b>Default Timezone:</b> ' . date_default_timezone_get() . '<br/>';
echo '<b>DateTime() Timezone:</b> ' . $now->getTimezone()->getName() . '<br/>';
echo '<b>IntlDateFormatter() Timezone:</b> ' . $formatter->getTimeZone()->getID() . '<br/>';
英文:

When call $formatter-&gt;parse($formatter-&gt;format($now)), the parse method is interpreting the week year value as the year value, resulting in a different timestamp value than the DateTime object's timestamp.

To get consistent output, you can use lowercase yyyy instead of uppercase YYYY in IntlDateFormatter.

$formatter = new IntlDateFormatter(
    &#39;en_GB&#39;, 
    IntlDateFormatter::SHORT, 
    IntlDateFormatter::SHORT, 
    &#39;Europe/London&#39;, 
    IntlDateFormatter::GREGORIAN,
    &#39;dd MMMM yyyy, HH:mm&#39;
);

$now = new DateTime(&#39;01-03-2023 17:00&#39;, new DateTimeZone(&#39;Europe/London&#39;));

echo &#39;&lt;b&gt;DateTime() String:&lt;/b&gt; &#39; . $now-&gt;format(&#39;d F Y, H:i&#39;) . &#39;&lt;br/&gt;&#39;;
echo &#39;&lt;b&gt;IntlDateFormatter() String:&lt;/b&gt; &#39; . $formatter-&gt;format( $now ) . &#39;&lt;br/&gt;&lt;br/&gt;&#39;;

echo &#39;&lt;b&gt;DateTime() Timestamp:&lt;/b&gt; &#39; . $now-&gt;getTimestamp() . &#39;&lt;br/&gt;&#39;;
echo &#39;&lt;b&gt;IntlDateFormatter() Timestamp:&lt;/b&gt; &#39; . $formatter-&gt;parse( $formatter-&gt;format( $now ) ) . &#39;&lt;br/&gt;&lt;br/&gt;&#39;;

echo &#39;&lt;b&gt;Default Timezone:&lt;/b&gt; &#39; . date_default_timezone_get() . &#39;&lt;br/&gt;&#39;;
echo &#39;&lt;b&gt;DateTime() Timezone:&lt;/b&gt; &#39; . $now-&gt;getTimezone()-&gt;getName() . &#39;&lt;br/&gt;&#39;;
echo &#39;&lt;b&gt;IntlDateFormatter() Timezone:&lt;/b&gt; &#39; . $formatter-&gt;getTimeZone()-&gt;getID() . &#39;&lt;br/&gt;&#39;;

huangapple
  • 本文由 发表于 2023年3月4日 06:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632355.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定