根据PHP中的语言环境获取日期格式

时间:2012-01-11 22:17:35

标签: php datetime date format

有一个非常简单的问题。我有一个语言环境标识符,en,en_US,cs_CZ或其他。我只需要获取该语言环境的日期时间格式。我知道我可以根据语言环境轻松格式化任何时间戳或日期对象。但我只需要日期格式的字符串表示,让我们说一个正则表达式。是否有任何功能管理此功能?到目前为止我还没有找到...

Exapmle:

$locale = "en_US";
$format = the_function_i_need($locale);
echo $format; // prints something like "month/day/year, hour:minute"

4 个答案:

答案 0 :(得分:2)

从评论中转换:

您必须构建一个包含可能性列表的数组。

http://en.wikipedia.org/wiki/Date_format_by_country应该有帮助。

完成后在某个地方发布功能,我相信它会为其他人派上用场

答案 1 :(得分:2)

我正在尝试自己做同样的事情。而不是构建所有国家/地区的数组,如何使用正则表达式来确定格式?

setlocale(LC_TIME, "us_US");

// returns 'mdy'
$type1 = getDateFormat();

setlocale(LC_TIME, "fi_FI");

// returns 'dmy'
$type2 = getDateFormat();

setlocale(LC_TIME, "hu_HU");

// returns 'ymd'
$type3 = getDateFormat();

/**
 * @return string
 */
function getDateFormat()
{
    $patterns = array(
        '/11\D21\D(1999|99)/',
        '/21\D11\D(1999|99)/',
        '/(1999|99)\D11\D21/',
    );
    $replacements = array('mdy', 'dmy', 'ymd');

    $date = new \DateTime();
    $date->setDate(1999, 11, 21);

    return preg_replace($patterns, $replacements, strftime('%x', $date->getTimestamp()));
}

答案 2 :(得分:1)

function getDateFormat($locale)
{
    $formatter = new IntlDateFormatter($locale, IntlDateFormatter::SHORT, IntlDateFormatter::NONE);
    if ($formatter === null)
        throw new InvalidConfigException(intl_get_error_message());

    return $formatter->getPattern();
}

确保安装intl

答案 3 :(得分:0)

strftime()setlocale()结合使用。看起来就像是你正在寻找的东西。