php 更改php中日期的语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6910912/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:35:04  来源:igfitidea点击:

Change the language for date in php

phpdate

提问by Emil

I have this MySQL query, which returns two dates (which are both formatted as a-m-Y). Now I want to translate this date into my own language (Danish). How can I do that. I have tried both the setlocale() and strftime() functions, but it won't work. I know it's a very basic question, but i really need help :) Thanks a lot!

我有这个 MySQL 查询,它返回两个日期(都格式为 amY)。现在我想把这个日期翻译成我自己的语言(丹麦语)。我怎样才能做到这一点。我已经尝试了 setlocale() 和 strftime() 函数,但它不起作用。我知道这是一个非常基本的问题,但我真的需要帮助:) 非常感谢!

回答by Sander Bakkum

Use setlocaleand strftimetogether:

使用setlocalestrftime在一起:

setlocale(LC_TIME, array('da_DA.UTF-8','da_DA@euro','da_DA','danish'));
echo strftime("%A"); // outputs 'tirsdag'

Works on my php installation on Windows.

适用于我在 Windows 上的 php 安装。

回答by Mudaser Ali

Use

http://php.net/manual/en/function.strftime.php

http://php.net/manual/en/function.strftime.php

<?php
setlocale(LC_ALL, 'da_DA');

echo strftime("%A %e %B %Y");
?>

回答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不可靠,因为它是按进程设置的,而不是按线程设置的(手册中提到了这一点)。这意味着其他正在运行的脚本可以随时更改区域设置。的溶液使用IntlDateFormatterintlphp扩展。

  • Install intlif necesarry (ubuntu): sudo apt-get install php5-intl

  • Install the locale you want to use (I'm using italian as an example): sudo locale-gen it_IT

  • Generate a locally formatted date:

  • intl必要时安装(ubuntu):sudo apt-get install php5-intl

  • 安装您要使用的语言环境(我以意大利语为例): sudo locale-gen it_IT

  • 生成本地格式化的日期:

$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()); 
// Output: 6 gennaio 2016 12:10

回答by Pascal MARTIN

I don't think the date()function is quite evolved enough for you, here.

我认为这个date()功能对你来说还不够进化,在这里。

Instead, I would recommend you take a look at the IntlDateFormatter1class (quoting):

相反,我建议您查看1(引用)IntlDateFormatter

Date Formatter is a concrete class that enables locale-dependent formatting/parsing of dates using pattern strings and/or canned patterns.

Date Formatter 是一个具体的类,它使用模式字符串和/或固定模式启用依赖于语言环境的日期格式/解析。

There are a couple of examples on the manual page of IntlDateFormatter::format(), where that method is used to display a date in two different languages, by just setting the desired locale.

的手册页上有几个示例IntlDateFormatter::format(),该方法用于通过设置所需的语言环境以两种不同的语言显示日期。


1.bundled with PHP >= 5.3


1.捆绑PHP >= 5.3

回答by Los Educadores

If you are trying to convert a datetimetry this:

如果您正在尝试转换,请datetime尝试以下操作:

$fecha = $dateConsulta->format('d-M-Y');  
$fecha = str_replace('Jan','Ene',$fecha);
$fecha = str_replace('Apr','Abr',$fecha);
$fecha = str_replace('Aug','Ago',$fecha);
$fecha = str_replace('Dec','Dic',$fecha);