Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“PHP5类”;日期时间“;在多语言网站中_Php_Datetime - Fatal编程技术网

“PHP5类”;日期时间“;在多语言网站中

“PHP5类”;日期时间“;在多语言网站中,php,datetime,Php,Datetime,我在一个网站上使用PHP5类“DateTime”,现在我需要将网站从英语翻译成西班牙语,但我不知道这个类是否可以用西班牙语显示月份和天数,如二月->费布雷罗和星期一->伦斯。谢谢你的帮助和评论 这篇文章是一个答案,因为我无法发表评论 你调查过了吗 一、 就个人而言,没有很好的结果,所以我不得不恢复手动翻译月和日。下面的解决方案:(yii::t()部分是完成翻译的地方) 函数tDate($format,$date='now'){ //将日期设置为DateTime对象 如果(is_string($d

我在一个网站上使用PHP5类“DateTime”,现在我需要将网站从英语翻译成西班牙语,但我不知道这个类是否可以用西班牙语显示月份和天数,如二月->费布雷罗和星期一->伦斯。谢谢你的帮助和评论

这篇文章是一个答案,因为我无法发表评论

你调查过了吗

一、 就个人而言,没有很好的结果,所以我不得不恢复手动翻译月和日。下面的解决方案:(yii::t()部分是完成翻译的地方)

函数tDate($format,$date='now'){
//将日期设置为DateTime对象
如果(is_string($date))$date=new DateTime($date);
如果(is_int($date))$date=new DateTime(date('r',$date));
如果(!is_object($date))返回“无效输入日期”;
$monthIndex=$date->format(“n”);//用作数组索引
$dayIndex=$date->format(“w”);//用作数组索引
//我们将翻译后的文本格式存储在变量中
eval(“$M=”.yii::t('app','array(1=>“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”));”;
eval(“$F=”.yii::t('app','array(1=>“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”)));”;
eval(“$D=”.yii::t('app','array(0=>“Mon”,“Tue”,“Wed”,“Thu”,“Fri”,“Sat”,“Sun”));”);
eval(“$l=”.yii::t('app','array(0=>“Sunday”、“周一”、“周二”、“周三”、“周四”、“周五”、“周六”));”;
$format_len=strlen($format);
$buffer='';
//我们在date()中执行格式化,但使用特殊情况。
对于($i=0;$i<$format_len;$i++){
开关($format[$i]){
案例'M':$buffer.=$M[$monthIndex];中断;
案例'F':$buffer.=$F[$monthIndex];中断;
案例'D':$buffer.=$D[$dayIndex];中断;
案例'l':$buffer.=$l[$dayIndex];中断;
大小写“\\”:$buffer.=$format[++$i];break;
默认值:$buffer.=$date->format($format[$i]);
}
}
返回$buffer;
}

基本上,它是php
date()
函数,但重载了M、F、D和l格式(我没有费心去涵盖所有格式)。

谢谢charlespwd,我在PHP5类“DateTime”中也使用了setlocale(),但它总是以英文格式显示日期。也许我需要试着用一下stftime
function tDate($format,$date='now'){
    // make the date a DateTime Object 
    if(is_string($date)) $date = new DateTime($date);
    if(is_int($date)) $date = new DateTime(date('r',$date));
    if(!is_object($date)) return 'invalid input date';

    $monthIndex = $date->format("n"); // used as array index
    $dayIndex = $date->format("w"); // used as array index

    // we store in variables the translated format text 
    eval('$M = '.yii::t('app','array ( 1 => "Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")').';');
    eval('$F = '.yii::t('app','array ( 1 => "January", "February", "March", "April","May", "June", "July", "August", "September", "October", "November",    "December")').';');
    eval('$D = '.yii::t('app','array (0 => "Mon", "Tue", "Wed", "Thu","Fri", "Sat", "Sun")').';');
    eval('$l = '.yii::t('app','array (0 => "Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday")').';');

    $format_len = strlen($format);
    $buffer = '';

    // we perform the formatting as in date(), but we use our special cases. 
    for ($i = 0; $i < $format_len; $i++){
        switch($format[$i]){
            case 'M': $buffer .= $M[$monthIndex]; break; 
            case 'F': $buffer .= $F[$monthIndex]; break;
            case 'D': $buffer .= $D[$dayIndex]; break;
            case 'l': $buffer .= $l[$dayIndex]; break;
            case '\\': $buffer .= $format[++$i]; break;
            default: $buffer.= $date->format($format[$i]);
        }
    }
    return $buffer;
 }