英文:
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(
'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 ) );
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->parse($formatter->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(
'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/>';
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论