【问题标题】:Time display function not working时间显示功能不工作
【发布时间】:2017-11-21 11:55:41
【问题描述】:

这段代码应该输出如下内容:

这一刻,你生活在国际日历开始后的2017年5月9日3时2分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;
        }

【问题讨论】:

  • 为什么要在函数中使用类?

标签: php function object time


【解决方案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.";
}

【讨论】:

    猜你喜欢
    • 2016-07-31
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多