2016-07-23 82 views
3

有了这个:有没有将日期转换为西班牙文的快捷方式?

date('d F Y', strtotime($row["datestart"])) 

我得到这个:

08 July 2016 

但我需要得到这个:

08 Julio 2016 

胡里奥是七月西班牙语。

我已经加入这个PHP页面的顶部:

setlocale(LC_TIME, 'es_ES'); 

,但它不工作。

那么我该怎么办?

+0

'的setlocale(LC_TIME,“es_ES “);'但它往往不起作用。编写你自己的功能 – splash58

+0

@ splash58为什么它经常不起作用? – xRobot

+0

我认为这可能是重复的: [http://stackoverflow.com/questions/22635303/get-day-from-string-in-spanish-php](http://stackoverflow.com/questions/22635303/ get-day-from-string-in-spanish-php) [http://stackoverflow.com/questions/25718115/trying-to-display-a-date-in-spanish](http://stackoverflow.com/questions/25718115 /试着用西班牙语显示日期) –

回答

4

这为我工作:

setlocale(LC_TIME, 'es_ES', 'Spanish_Spain', 'Spanish'); 
$date = $date = str_replace("/","-","08/07/2016"); 
echo strftime('%d %B %Y',strtotime($date)); // 08 julio 2016 

setlocale是关键因素在这里。

1

你可以创建一个关联数组。 将键设置为英文月份,将值设置为西班牙文对应的月份。

这将是这个样子......

$months = array(
    'january' => 'enero', 
    'february' => 'febrero', 
    'march' => 'marzo', 
    'april' => 'abril', 
    'may' => 'mayo', 
    'june' => 'junio', 
    'july' -> 'julio', 
    'august' => 'agosto', 
    'september' => 'septiembre', 
    'october' => 'octubre', 
    'november' => 'noviembre', 
    'december' => 'diciembre' 
); 

然后,你可以参考这样的几个月...

$enMonth = "july"; //This is the month in English that you will match to the corresponding month in Spanish. 

$esMonth = $months[$enMonth]; //You are returning the value (which is Spanish) of the key (which is English), giving you the month in Spanish. 

你也许还可以使用谷歌翻译的API,但似乎对于使用简单数组可以完成的事情来说太多了。

这里是Google Translate's API如果你有兴趣翻译其他单词或更大的单词。

1

另一个变种你可以使用:

安装intl扩展phplink)。 在php.ini文件中启用它,然后你就可以检查它正在与下面的示例:

$f = new IntlDateFormatter('es_ES', null, null, null, null, null, 'dd MMMM y'); 
print($f->format(new DateTime('2016-07-08')); 

的预期产出将是以下几点: 08 julio 2016

相关问题