PHP 日期函数输出意大利语
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1114488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Date function output in Italian
提问by Shadi Almosri
I am trying to output dates in the Italian format using date()as follows:
我正在尝试使用date()以下方法以意大利语格式输出日期:
<?php
setlocale(LC_ALL, 'it_IT');
echo date("D d M Y", $row['eventtime']);
?>
However, it is still coming out in the English format. What else could I do? Is there something wrong?
但是,它仍然以英文格式出现。我还能做什么?有什么不对?
The solution has to be script specific and not server wide...
解决方案必须是特定于脚本的,而不是服务器范围内的...
Thanks
谢谢
Shadi
沙迪
回答by Stefan Gehrig
date()is not locale-aware. You should use strftime()and its format specifiers to output locale-aware dates (from the date()PHP manual):
date()不识别区域设置。您应该使用strftime()及其格式说明符来输出区域设置感知日期(来自date()PHP 手册):
To format dates in other languages, you should use the
setlocale()andstrftime()functions instead ofdate().
要格式化其他语言的日期,您应该使用
setlocale()和strftime()函数而不是date()。
Regarding Anti Veeranna's comment: he is absolutely right, since you have to be very careful with setting locales as they are sometimes not limited to the current script scope. The best way would be:
关于Anti Veeranna的评论:他是绝对正确的,因为您必须非常小心地设置区域设置,因为它们有时不限于当前脚本范围。最好的方法是:
$oldLocale = setlocale(LC_TIME, 'it_IT');
echo utf8_encode( strftime("%a %d %b %Y", $row['eventtime']) );
setlocale(LC_TIME, $oldLocale);
回答by Simon Epskamp
I found that setlocaleisn't reliable, as it is set per process, not per thread (the manual mentions this). This means other running scripts can change the locale at any time. A solution is using IntlDateFormatterfrom the intlphp extension.
我发现这setlocale不可靠,因为它是按进程设置的,而不是按线程设置的(手册中提到了这一点)。这意味着其他正在运行的脚本可以随时更改区域设置。的溶液使用IntlDateFormatter从intlphp扩展。
$fmt = new \IntlDateFormatter('it_IT', NULL, NULL);
$fmt->setPattern('d MMMM yyyy HH:mm');
// See: http://userguide.icu-project.org/formatparse/datetime for pattern syntax
echo $fmt->format(new \DateTime());
If it doesn't work you might need to:
如果它不起作用,您可能需要:
Install
intlphp extension (ubuntu example):sudo apt-get install php5-intlInstall the locale you want to use:
sudo locale-gen it_IT
安装
intlphp 扩展(ubuntu 示例):sudo apt-get install php5-intl安装您要使用的语言环境:
sudo locale-gen it_IT
回答by Anti Veeranna
it_IT locale has to be installed/enabled by your server admin, otherwise this will not work.
it_IT 语言环境必须由您的服务器管理员安装/启用,否则这将不起作用。
So, Jonathan's solution is probably the best.
所以,乔纳森的解决方案可能是最好的。
回答by Sampson
回答by GianJJ
After a lot of attempts, this is the solution works like charm:
经过多次尝试,这是解决方案的魅力所在:
STEP 1: We will set the localization and time zone (upper page)
第 1 步:我们将设置本地化和时区(上页)
// ====> Localizzazione
setlocale(LC_TIME, 'ita', 'it_IT'); // Italiano
date_default_timezone_set("Europe/Rome"); // Ora
STEP 2: We make a function for easily convert when we need (wherever into page)
第 2 步:我们制作了一个函数,以便在需要时轻松转换(无论何时进入页面)
//=============== Funzione formattazione data
function f_date_time($f_date, $f_date_info) {
if (strlen($f_date) > 7){
$dateToParse = $f_date;
}else{
$dateToParse = date("YmdHis");
}
switch ($f_date_info) {
case 1: //20191130
echo date("Ymd");
break;
case 2: //30/11/19
echo utf8_encode(strftime('%d/%m/%y', strtotime($dateToParse)));
break;
case 3: //30/11/2019
echo utf8_encode(strftime('%d/%m/%Y', strtotime($dateToParse)));
break;
case 4: //30/11/2019 15:27:50
echo utf8_encode(strftime('%d/%m/%Y %H:%M:%S', strtotime($dateToParse)));
break;
case 5: //Saturday 30
echo utf8_encode(strftime('%A %d', strtotime($dateToParse)));
break;
case 6: //Saturday 30 November
echo utf8_encode(strftime('%A %d %B', strtotime($dateToParse)));
break;
case 7: //Saturday 30 November 2019
echo utf8_encode(strftime('%A %d %B %Y', strtotime($dateToParse)));
break;
case 8: //Saturday 30 November 2019 ore 15:27:50
echo utf8_encode(strftime('%A %d %B %Y %H:%M:%S', strtotime($dateToParse)));
break;
default: //20191130152750
echo date("YmdHis");;
}
}
STEP 3: Now to convert your data just use the function (wherever into your code)
第 3 步:现在转换您的数据只需使用该功能(无论在您的代码中的任何位置)
$today = "20190320234500"; //Format yyyymmddhhmmss
echo f_date_time($today, 1) . "\n";
echo f_date_time($today, 2) . "\n";
echo f_date_time($today, 3) . "\n";
echo f_date_time($today, 4) . "\n";
echo f_date_time($today, 5) . "\n";
echo f_date_time($today, 6) . "\n";
echo f_date_time($today, 7) . "\n";
echo f_date_time($today, 8) . "\n";
OUTPUT: will be somethink like this:
输出:会像这样:
20191231
20/03/74
20/03/1974
20/03/1974 23:45:00
mercoledì 20
mercoledì 20 marzo
mercoledì 20 marzo 1974
mercoledì 20 marzo 1974 23:45:00
PS: happy birday to me :-)
PS:祝我小鸟快乐:-)
回答by Sunchaser
About the article on http://www.phpnews.it/articoli/ottenere-date-in-italiano/response, the blog suggest an alternative method, but the code is not working, here is the correct code:
关于http://www.phpnews.it/articoli/ottenere-date-in-italiano/response上的文章,博客提出了另一种方法,但代码不起作用,这里是正确的代码:
function timestamp_to_date_italian($date)
{
$months = array(
'01' => 'Gennaio',
'02' => 'Febbraio',
'03' => 'Marzo',
'04' => 'Aprile',
'05' => 'Maggio',
'06' => 'Giugno',
'07' => 'Luglio',
'08' => 'Agosto',
'09' => 'Settembre',
'10' => 'Ottobre',
'11' => 'Novembre',
'12' => 'Dicembre');
list($day, $month, $year) = explode('-',date('d-m-Y', $date));
return $day . ' ' . $months[$month] . ' ' . $year;
}

