时间显示功能不起作用

时间:2017-06-19 10:05:51

标签: php function object time

此代码应输出类似:

  

此时此刻,您生活在国际日历开始以来的2017年第5个月的第9天第3小时的第11分钟。

而是输出:https://prnt.sc/fli92p

不知道问题是什么。

       date_default_timezone_set(//location...); 
       say_time();

        function say_time() 
        {
            $o = ' of the';


            class time_value
            {
                public $t, $name, $display;
                protected $n, $suf;

                function __construct() 
                {
                    $this->display = " $this->n"."$this->suf"." $this->name";
                    $this->n = date($this->t,time());
                    switch ($this->n)
                    {
                            case 1:
                                $suf = 'st';
                                break;
                            case 2:
                                $suf = 'nd';
                                break;
                            case 3:
                                $suf = 'rd';
                                break;
                            default:
                                $suf = 'nth';
                                break; 
                    }
                }

            }

                $sec = new time_value;
                $sec->t = 's';
                $sec->name = 'seconds';

                $min = new time_value;
                $min->t = 'i';
                $min->name = 'minutes';

                $hr = new time_value;
                $hr->t = 'G';
                $hr->name = 'hours';

                $day = new time_value;
                $day->t = 'j';
                $day->name = 'days';

                $mon = new time_value;
                $mon->t = 'n';
                $mon->name = 'months';

                $yr = new time_value;
                $yr->t = 'Y';
                $yr->name = 'years';

                echo "You are, at this moment, living in the "
                               .$sec->display .$o
                               .$min->display .$o
                               .$hr ->display .$o 
                               .$day->display .$o
                               .$mon->display .$o
                               .$yr ->display .
                     " since the begining of the International calender.";
                echo $sec->display;
        }

1 个答案:

答案 0 :(得分:1)

该行:

$this->display = " $this->n"."$this->suf"." $this->name";

是类'构造函数的第一行。它在对象的$display属性中存储一个只包含空格的字符串,因为它包含的值尚未设置。

阅读双引号字符串中的double-quotes stringsvariables parsing

为了工作,班级time_value应该是这样的:

class time_value
{
    private $t, $name, $display;

    public function __construct($t, $name)
    {
        $this->t = $t;
        $this->name = $name;

        $n = date($this->t, time());
        switch ($n)
        {
            case 1:
                $suf = 'st';
                break;
            case 2:
                $suf = 'nd';
                break;
            case 3:
                $suf = 'rd';
                break;
            default:
                $suf = 'th';
                break; 
        }

        $this->display = " {$n}{$suf} {$this->name}";
    }

    public function display() { return $this->display; }
}

$sec = new time_value('s', 'seconds');
$min = new time_value('i', 'minutes');
// all the other time components here...

echo $sec->display().$o.$min->display(); // ...

面向对象编程的下一步是将所有时间组件的生成封装到time_value类(或者使用time_value实例的另一个类中,如果你更喜欢它)并且函数say_time()的代码如下所示:

function say_time()
{
    $time = new time_value();
    echo "You are, at this moment, living in the ".$time->display("of the")." since the begining of the International calender.";
}