Php IntlDateFormatter 类不显示 unix 时间戳

标签 php intldateformatter

我正在尝试获取unix timestampIntlDateFormatter .

我已经尝试过了

$formatter = new IntlDateFormatter(
        'fr_FR',
        IntlDateFormatter::FULL,
        IntlDateFormatter::FULL,
        'Europe/Paris',
        IntlDateFormatter::TRADITIONAL,
        'unix'
    );
echo $formatter->format(strtotime('now'));

给我2022 ,而我试图以这种格式获取它 1644601950 .

我也尝试过改变unixUu等等,但我找不到 unix timestamp 的正确关键字在IntlDateFormatter类。

如果我更改'unix''YY-MM-d'它会给我 22-02-11 ,但它不在 unix timestamp 中格式。

最佳答案

快速回答

使用IntlDateFormatter类获取unix时间戳。使用此代码。

$formatter = new IntlDateFormatter(
    'fr_FR',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'Europe/Paris',
    IntlDateFormatter::TRADITIONAL
);
echo $formatter->parse($formatter->format(strtotime('now')));

看来从此类获取时间戳的唯一方法只能使用 IntlDateFormatter::parse()方法。

时间戳

时间戳始终以自 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)以来的秒数来衡量,无论您使用什么函数或类来获取它。示例time() , mktime() , DateTime::getTimestamp() .

获取特定日期/时间的时间戳。

在此示例中,假设我的服务器 php.ini 时区为 亚洲/曼谷,并且您在亚洲/澳门中获取此日期/时间。

// server php.ini timezone is Asia/Bangkok
// set the date/time input.
// assume that it is Asia/Macau timezone.
$datetime = '2025-03-12 15:34:26';

$timestamp = strtotime($datetime);
echo $timestamp.'<br>';// 1741768466

// if you use function `date()`, the result will be wrong! because the timestamp is incorrectly parse using server timezone.
echo 'use date(): ' . date('Y-m-d H:i:s P', $timestamp) . '<br>';// 2025-03-12 15:34:26 +07:00 WRONG! incorrect timezone.

// even if you use `\IntlDateFormatter()` class, with or without set timezome the result still be wrong!
$formatter = new \IntlDateFormatter(
    'en',
    \IntlDateFormatter::FULL,
    \IntlDateFormatter::FULL,
    new \DateTimeZone('Asia/Macau'),
    \IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
echo $formatter->format($timestamp).'<br>';// 2025-03-12 16:34:26 +08:00 WRONG! time does not matched.
$formatter->setTimeZone(new \DateTimeZone('Asia/Macau'));
echo $formatter->format($timestamp).'<br>';// 2025-03-12 16:34:26 +08:00 WRONG! time does not matched.

上面的所有示例都是错误的,因为时间戳是根据服务器时区(php.ini)解析的。

获取正确时区的时间戳。

正如 @Álvaro González 评论的那样,您可以使用 strtotime('date/time timzone'); 来获取特定 timzone 中的 timstamp,但您也可以使用 \Datetime()\IntlDateFormatter 类。

示例:

// server php.ini timezone is Asia/Bangkok
// set the date/time input.
// assume that it is Asia/Macau timezone.
$datetime = '2025-03-12 15:34:26';

$timestamp = strtotime($datetime . ' Asia/Macau');
echo $timestamp .'<br>';// 1741764866
echo 'use date(): ' . date('Y-m-d H:i:s P', $timestamp) . '<br>';// 2025-03-12 14:34:26 +07:00 CORRECT! but time zone is Bangkok as specified in php.ini (-1 hour for Macau to Bangkok).

$formatter = new \IntlDateFormatter(
    'en',
    \IntlDateFormatter::FULL,
    \IntlDateFormatter::FULL,
    new \DateTimeZone('Asia/Macau'),
    \IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
echo $formatter->format($timestamp).'<br>';// 2025-03-12 15:34:26 +08:00 CORRECT!

// use `DateTime()` class to get timestamp in certain timezone.
$date = new DateTime($datetime, new \DateTimeZone('Asia/Macau'));
$timestamp2 = (int) $date->getTimestamp();
echo $timestamp2.'<br>';// 1741764866

$formatter = new \IntlDateFormatter(
    'en',
    \IntlDateFormatter::FULL,
    \IntlDateFormatter::FULL,
    new \DateTimeZone('Asia/Macau'),
    \IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
echo $formatter->format($timestamp2) . '<br>';// 2025-03-12 15:34:26 +08:00 CORRECT!

// use `\IntlDateFormatter()` class to get timestamp in certain timezone.
$formatter = new IntlDateFormatter(
    'en-US',
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    new \DateTimeZone('Asia/Macau'),
    IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd kk:mm:ss'
);
$timestamp3 = (int) $formatter->parse($datetime);
echo $timestamp3 . '<br>';// 1741764866 CORRECT!

要从 \IntlDateFormatter() 类获取时间戳,您必须在构造函数中设置与日期/时间源格式匹配的模式。

跨时区转换日期/时间

在此示例中,我将根据从亚洲/澳门时区获取的日期/时间跨时区转换日期/时间结果。

我将使用 IntlDateFormatter() 类进行转换。

// server php.ini timezone is Asia/Bangkok
// set the date/time input.
// assume that it is date/time from Asia/Macau timezone.
$datetime = '2025-03-12 15:34:26';

$tsmacau = strtotime($datetime . ' Asia/Macau');// 1741764866

$formatter = new \IntlDateFormatter(
    'en',
    \IntlDateFormatter::FULL,
    \IntlDateFormatter::FULL,
    new \DateTimeZone('Asia/Macau'),// timezone for input timestamp.
    \IntlDateFormatter::GREGORIAN,
    'yyyy-MM-dd kk:mm:ss ZZZZZ'
);
// convert to Asia/Bangkok timezone.
$formatter->setTimezone(new \DateTimeZone('Asia/Bangkok'));
echo $formatter->format($tsmacau).'<br>';// 2025-03-12 14:34:26 +07:00

// convert to Europe/Paris timezone
$formatter->setTimezone(new \DateTimeZone('Europe/Paris'));
echo $formatter->format($tsmacau).'<br>';// 2025-03-12 08:34:26 +01:00

在这种情况下,您必须知道时间戳的时区,因为您必须将输入时区设置到类构造函数中,并在调用 format() 之前更改要转换的时区。

关于Php IntlDateFormatter 类不显示 unix 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71084601/

相关文章:

php - 使用 php 或 ruby​​ 在 linux 中操作 word 文档(doc、docx 等)

php - 在 Prestashop 版本 1.5.3 中的产品管理页面中添加新选项卡,如功能选项卡、属性选项卡等

c# - 输出无效

用于查询 unixODBC 的 PHP 脚本在浏览器中失败,但可以在 Linux 命令提示符下运行

php - else 语句的问题

php - 如何强制 PHP 的 IntlDateFormatter 仅忽略输出中的年份?