0

我写了一门课来翻译不同语言/格式的日期。对不起,我重新发布了这个问题,因为上次我发布了它,我失去了我的互联网连接,人们离开了这个话题......

这是完整的课程...

class GetDateTime {

    private $_text_en_US = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", "January", "February", "March", "April", "May","June", "July", "August", "September","October", "November", "December");

    private $_text_fr_FR = array("Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche", "Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");

    public function getDateTime($format='d/m/Y', $timestamp, $locale='fr_FR') {
        switch ($format) {
            case 'd/m/Y':
            case 'm/d/Y':
                return date($format, $timestamp);
            break;  
            case 'l d F Y':
                return str_replace($_text_en_US, ${'_text_'.$locale}, date($format, $timestamp));
            break;  
        }
    }

}

...以及我如何称呼它:

include_once (BASE_DIR.'/lib/dateTime.class.php');
$dateTime = new GetDateTime();

事实是当我打电话时它根本没有翻译:

echo $dateTime->getDateTime('l d F Y', time());
4

2 回答 2

2

怎么样strftime?您可以设置您的区域设置并获取翻译日期。

<?php
    setlocale(LC_TIME, "fr_FR");
    echo strftime("Today is %A (in French) and the date is %B %d, %Y");

    // Outputs something like that: Today is mercredi (in French) and the date is novembre 13, 2013
?>
于 2013-11-13T15:32:41.943 回答
1

作为该getDateTime方法的第二个参数,您期望 atimestamp但您传递的是空字符串date()的函数结果。用函数替换它。time()

echo $dateTime->getDateTime('l d F Y', time());

编辑
我怎么会错过!您指的是局部变量,而不是对象的属性。在最后一个条件中使用$this关键字:case

return str_replace($this->_text_en_US, $this->{'_text_'.$locale}, date($format, $timestamp));
于 2013-11-13T15:32:07.643 回答