按国家/地区获取 PHP date_format 字符串
Get PHP date_format string by country
我有一个 MySQL 格式的日期,我想显示本地化为我的用户所在县日期格式的日期。
我想知道是否有办法通过国家的 ISO 代码获取格式,例如:
$mysql_date = '2017-12-31';
echo print_localized_date($mysql_date,'it'); // 31/12/2017
echo print_localized_date($mysql_date,'de'); // 31.12.2017
echo print_localized_date($mysql_date,'us'); // 12/31/2017
我知道我可以直接传递格式,但我们的网站可能销往世界各地,我应该考虑每种日期格式。
另一个解决方案可能是在我的国家 table 中存储一个带有 PHP 日期格式字符串的列,在这种情况下,是否有我可以从中获取该信息的资源,希望是 CSV 或SQL 转储?
我使用 Laravel 和 Carbon,但我在那个库中没有看到解决方案,例如 moment.j
正是我要找的东西:["moment.js Multiple Locale Support][1]
但在 JavaScript,我需要它 PHP.
这就是 IntlDateFormatter
from the intl 扩展的用途:
$fmt = new IntlDateFormatter(
'it_IT',
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE,
'Europe/Rome',
IntlDateFormatter::GREGORIAN
);
$mysql_date = '2017-12-31';
$date = new DateTime($mysql_date);
echo $fmt->format($date);
31/12/17
一个如何做到这一点的例子是使用 PHPs setlocale
函数,如下所示:
setlocale(LC_TIME, "de_DE"); //sets to German locale
$today = strftime("%A, %e %B %Y"); //outputs the current time in this locale, to a string
setlocale(LC_TIME, "en_GB"); //revert the locale back to the page standard (in my case GB).
结合 strftime
功能,这将为您提供 PHP.
中完全可识别区域设置的日期和时间
从阅读关于这个主题的类似问题来看,使用上面的方法似乎是最简单的方法,但是 是一个很好的观点 -- 为什么不让前端处理这个前端结束问题?
完整的功能解决方案:
注意: 您应该 return 时间戳作为来自 MySQL 数据列的直接 9 时间戳](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp),使用:
SELECT ..., UNIX_TIMESTAMP(`date_column`) AS timeStampDate, ... FROM ...
这比让 PHP 生成 DateTime 对象或需要 post 处理日期字符串以获得相同值要高效得多。但是一旦达到这个值,就使用下面的函数:
function print_localized_date($timestamp, $locale){
if($timestamp > 0 && !empty($locale)){
//grab current locale
$currentLocale = setlocale(LC_TIME, 0);
// set to new locale.
setlocale(LC_TIME, $locale);
// format the date string however you wish, using the timestamp
$today = strftime("%A, %e %B %Y", $timestamp);
// revert to the current locale now date string is formatted.
setlocale(LC_TIME, $currentLocale);
// tidy up (probably un-needed)
unset(currentLocale);
// return the date value string.
return $today;
}
return false;
}
$timestamp = 1496061088;
$locale = "it_IT.UTF-8";
print print_localized_date($timestamp, $locale);
/***
* prints lunedì, 29 maggio 2017
***/
我会选择 Carbon 和 "setLocale()"。
setlocale(LC_TIME, config('app.locale')); // or 'en' ..
Carbon::now()->format('l j F Y H:i:s');
或者只在一个中心位置调用 setLocale,比如在 AppServiceProvider 的 der regsister() 方法中
我有一个 MySQL 格式的日期,我想显示本地化为我的用户所在县日期格式的日期。
我想知道是否有办法通过国家的 ISO 代码获取格式,例如:
$mysql_date = '2017-12-31';
echo print_localized_date($mysql_date,'it'); // 31/12/2017
echo print_localized_date($mysql_date,'de'); // 31.12.2017
echo print_localized_date($mysql_date,'us'); // 12/31/2017
我知道我可以直接传递格式,但我们的网站可能销往世界各地,我应该考虑每种日期格式。
另一个解决方案可能是在我的国家 table 中存储一个带有 PHP 日期格式字符串的列,在这种情况下,是否有我可以从中获取该信息的资源,希望是 CSV 或SQL 转储?
我使用 Laravel 和 Carbon,但我在那个库中没有看到解决方案,例如 moment.j
正是我要找的东西:["moment.js Multiple Locale Support][1]
但在 JavaScript,我需要它 PHP.
这就是 IntlDateFormatter
from the intl 扩展的用途:
$fmt = new IntlDateFormatter(
'it_IT',
IntlDateFormatter::SHORT,
IntlDateFormatter::NONE,
'Europe/Rome',
IntlDateFormatter::GREGORIAN
);
$mysql_date = '2017-12-31';
$date = new DateTime($mysql_date);
echo $fmt->format($date);
31/12/17
一个如何做到这一点的例子是使用 PHPs setlocale
函数,如下所示:
setlocale(LC_TIME, "de_DE"); //sets to German locale
$today = strftime("%A, %e %B %Y"); //outputs the current time in this locale, to a string
setlocale(LC_TIME, "en_GB"); //revert the locale back to the page standard (in my case GB).
结合 strftime
功能,这将为您提供 PHP.
从阅读关于这个主题的类似问题来看,使用上面的方法似乎是最简单的方法,但是
完整的功能解决方案:
注意: 您应该 return 时间戳作为来自 MySQL 数据列的直接 9 时间戳](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp),使用:
SELECT ..., UNIX_TIMESTAMP(`date_column`) AS timeStampDate, ... FROM ...
这比让 PHP 生成 DateTime 对象或需要 post 处理日期字符串以获得相同值要高效得多。但是一旦达到这个值,就使用下面的函数:
function print_localized_date($timestamp, $locale){
if($timestamp > 0 && !empty($locale)){
//grab current locale
$currentLocale = setlocale(LC_TIME, 0);
// set to new locale.
setlocale(LC_TIME, $locale);
// format the date string however you wish, using the timestamp
$today = strftime("%A, %e %B %Y", $timestamp);
// revert to the current locale now date string is formatted.
setlocale(LC_TIME, $currentLocale);
// tidy up (probably un-needed)
unset(currentLocale);
// return the date value string.
return $today;
}
return false;
}
$timestamp = 1496061088;
$locale = "it_IT.UTF-8";
print print_localized_date($timestamp, $locale);
/***
* prints lunedì, 29 maggio 2017
***/
我会选择 Carbon 和 "setLocale()"。
setlocale(LC_TIME, config('app.locale')); // or 'en' ..
Carbon::now()->format('l j F Y H:i:s');
或者只在一个中心位置调用 setLocale,比如在 AppServiceProvider 的 der regsister() 方法中