php 获取语言环境特定的日期格式

标签 php date datetime

确定当前给定语言环境的短日期格式的最佳方法是什么?

例如,如果我的脚本的语言环境设置为荷兰语,我想以某种方式获取该特定语言环境中使用的短日期格式,它将是:

年月日

如果设置为美国,我想获取美国语言环境中的日期格式:

月/日/年

等等……

最佳答案

您可以使用 Intl根据所选区域设置格式化日期的 PHP 扩展:

$locale = 'nl_NL';

$dateObj = new DateTime;
$formatter = new IntlDateFormatter($locale, 
                        IntlDateFormatter::SHORT, IntlDateFormatter::SHORT);

echo $formatter->format($dateObj);

如果您只是想获取用于格式化日期的模式,IntlDateFormatter::getPattern正是您所需要的。

手册中的示例:

$fmt = new IntlDateFormatter(
    'en_US',
    IntlDateFormatter::FULL,
    IntlDateFormatter::FULL,
    'America/Los_Angeles',
    IntlDateFormatter::GREGORIAN,
    'MM/dd/yyyy'
);
echo 'pattern of the formatter is : ' . $fmt->getPattern();
echo 'First Formatted output is ' . $fmt->format(0);
$fmt->setPattern('yyyymmdd hh:mm:ss z');
echo 'Now pattern of the formatter is : ' . $fmt->getPattern();
echo 'Second Formatted output is ' . $fmt->format(0);

这将输出:

pattern of the formatter is : MM/dd/yyyy
First Formatted output is 12/31/1969
Now pattern of the formatter is : yyyymmdd hh:mm:ss z
Second Formatted output is 19690031 04:00:00 PST

关于php 获取语言环境特定的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737272/

相关文章:

php - 从表中获取 session_user 排名

java - 与 Bash 相比,Java 中的语言环境区分大小写不一致

mysql - 从 MySQL/数据库获取 DateTime 中的 -0200

php - 挣扎于 recaptcha v2 和表单提交

PHPExcel 返回损坏的二进制文件

php - 如何设置1 :many relationships in MySQL

javascript - 如何通过单击按钮将日期和时间插入输入字段

javascript - 每三次迭代将日期增加 1

c# - 使用 datetime.now 以 YYYYMM 格式检索年份和月份

java - 在Android上计算本周两天之间的时差