有一个非常简单的问题。我有一个区域设置标识符,en、en_US、cs_CZ或其他什么。我只需要获得该区域设置的日期-时间格式。我知道我可以很容易地根据地区设置任何时间戳或日期对象的格式。但我只需要日期格式的字符串表示,比方说一个正则表达式。是否有管理此功能的功能?到目前为止我还没有找到。。。
示例:
$locale = "en_US";
$format = the_function_i_need($locale);
echo $format; // prints something like "month/day/year, hour:minute"
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.
从注释转换:
您将不得不构建一个包含各种可能性的数组。
http://en.wikipedia.org/wiki/Date_format_by_country应该会有所帮助。
完成后将该函数发布到某个地方,我相信它会对其他
我自己也在尝试做同样的事情。与其构建所有国家/地区的数组,不如使用regex来确定格式?
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()));
}
将strftime()
与setlocale()
结合使用。看起来这就是你想要的。