外语中的 PHP date() - 例如 Mar 25 Ao?09

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

PHP date() in foreign languages - e.g. Mar 25 Ao? 09

phpdatelocalization

提问by David Miller

I have a script that needs to display date data to an international audience - e.g.

我有一个需要向国际观众显示日期数据的脚本 - 例如

"submitted Tue 25 Aug 09"

“09 年 8 月 25 日星期二提交”

Is there an easier/cleaner way to get this converted to the French(etc) equivalent "Mar 25 Ao? 09" than:

有没有比以下更简单/更清洁的方法将其转换为法语(等)等效的“Mar 25 Ao?09”:

Setting a constant LANGand a $LANGUAGESarray of include files & :

设置一个常量LANG和一$LANGUAGES组包含文件 & :

if(LANG != 'EN')
{
include $LANGUAGES['LANG'];
}

& then the included file maps the days & months & replaces for the appropriate locale?

&然后包含的文件映射日期和月份并替换为适当的语言环境?

Thanks

谢谢

David

大卫

回答by Jakub

I think you can't get away from doing so without setting LOCALE:

我认为如果不设置 LOCALE,您就无法避免这样做:

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

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

Some details on strftime: http://us2.php.net/manual/en/function.strftime.php

关于 strftime 的一些细节:http: //us2.php.net/manual/en/function.strftime.php

回答by Samir Talwar

According to the datefunction's manual page, you should use setlocale. Methods such as strftimewill then use the locale specified. date, however, will not for some reason.

根据该date函数的手册页,您应该使用setlocale. 诸如此类的方法strftime将使用指定的语言环境。date但是,由于某种原因,不会。

回答by Artur Babyuk

I think that the best way to do it with strftimeand setlocalefunctions. But it will not work if your server has no needed locale installed (in current questions it is fr_FR).

我认为这是最好的方法strftimesetlocale功能。但是,如果您的服务器没有安装所需的语言环境,它将无法工作(在当前问题中是fr_FR)。

Code bellow throw an exception if locale change will be unsuccessful

如果区域设置更改不成功,下面的代码会抛出异常

<?php

$result = setlocale(LC_ALL, 'fr_FR');

if($result === false){
    throw new \RuntimeException(
        'Got error changing locale, check if locale is installed on the system'
    );
}

$dayOfMonth = '%e';
//if it is Windows we will use %#d as %e is not supported
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
   $dayOfMonth = '%#d';
}

//Mar 25 Ao? 09 - month shortname, day of month, day shortname, year last two digits
echo strftime("%b $dayOfMonth %a %y"); 

回答by André Hoffmann

You might also want to have a look at Zend_Date.

您可能还想看看Zend_Date