PHP获取特定于语言环境的日期格式 [英] php get locale specific date format

查看:166
本文介绍了PHP获取特定于语言环境的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



dd-mm-yyyy



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

mm / dd / yyyy



等等...

解决方案

您可以使用 Intl PHP扩展程序可根据所选语言环境设置日期格式:

  $ locale ='nl_NL'; 

$ dateObj =新的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格式化程序的模式是:。 $ fmt-> getPattern();
echo‘First Formatted output is’。 $ fmt-> format(0);
$ fmt-> setPattern(’yyyymmdd hh:mm:ss z’);
echo‘格式化程序的现在模式为:’。 $ fmt-> getPattern();
echo第二格式的输出为。 $ fmt-> format(0);

这将输出:

 格式化程序的模式是:MM / dd / yyyy 
第一个格式化输出是1969年12月31日
现在,格式化程序的模式是:yyyymmdd hh:mm:ss z
第二格式化输出为19690031 04:00:00 PST


What is the best way to determine the short date format for the current given locale?

For example, if my script's locale was set to Dutch, I would like to somehow obtain the short date format used in that specific locale, it would be:

dd-mm-yyyy

If it was set to American, I would like to get the date format in the American locale:

mm/dd/yyyy

And so on...

解决方案

You can use the Intl PHP extension to format the date according to the chosen locale:

$locale = 'nl_NL';

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

echo $formatter->format($dateObj);

If you're just trying to get the pattern used for formatting the date, IntlDateFormatter::getPattern is what you need.

Example from the manual:

$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);

This will output:

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获取特定于语言环境的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆