多语言网站中的PHP5类“DateTime”

时间:2013-07-31 17:43:40

标签: php datetime

我正在使用PHP5类" DateTime"在一个网站上,现在我需要将网站从英语翻译成西班牙语,但我不知道这个课程是否可以显示西班牙语的月份和日期,如二月 - > Febrero和周一 - > Lunes。感谢您的帮助和评论!

1 个答案:

答案 0 :(得分:0)

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

你有没看过stftime

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

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;
 }

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