dongya2578 2014-12-23 18:32
浏览 42
已采纳

Yii2:两次echo变量给出了错误的结果

Well I have a function getDaysTotal in my model say estimate.php.

If in my view.php if I use

echo $model->DaysTotal; 

I get the value 3. But if I do it again

echo $model->DaysTotal;

Now I get 1. Any idea, why I am getting it like this. This is happening for any function in estimate.php. If I am using it for second time the result is weird.

Am I doing anything wrong here? How can I correct this? Thanks.

Here is the code for getTotalDays function:

public function getDaysTotal() {
               $this->discharge_date = strtotime($this->discharge_date);
               $this->admission_date = strtotime($this->admission_date);

               $datediff = ($this->discharge_date - $this->admission_date);

               $fraction_days = ($datediff/(60*60*24));

               if ($fraction_days < 1){
                          return 1;

               }elseif(($datediff)%(60*60*24) < 10800){
                 $option2 = floor($datediff/(60*60*24));
                 return $option2;
               }elseif(($datediff%86400) > 10800 && ($datediff%86400)<21600) {
                  $option3 = ceil($datediff/(60*60*24)*2)/2;
                  return $option3;
               }elseif (($datediff%86400) >21600){
                   $option4= ceil($datediff/86400);
                   return $option4;
               } 
  • 写回答

2条回答 默认 最新

  • duanji1899 2014-12-23 19:35
    关注

    Your getter changes your object:

    public function getDaysTotal() {
               $this->discharge_date = strtotime($this->discharge_date);
               $this->admission_date = strtotime($this->admission_date);
    

    You should not to do it. On next call strtotime(int) returns false for both lines.

    Try followed:

    public function getDaysTotal() {
               $discharge_date = strtotime($this->discharge_date);
               $admission_date = strtotime($this->admission_date);
               $datediff = ($discharge_date - $admission_date);
    

    Used aux vars here, without any object state modifying.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料