php获取区域设置特定的日期格式

use*_*025 4 php datetime date

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

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

DD-MM-YYYY

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

毫米/日/年

等等...

Ama*_*ali 7

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

$locale = 'nl_NL';

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

echo $formatter->format($dateObj);
Run Code Online (Sandbox Code Playgroud)

如果你只是想获得用于格式化日期的模式,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);
Run Code Online (Sandbox Code Playgroud)

这将输出:

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
Run Code Online (Sandbox Code Playgroud)