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

图片转代码服务由CSDN问答提供 功能建议

我的模型中有一个函数 getDaysTotal ,说是estimate.php。

如果我在view.php中使用

  echo $ model-&gt; DaysTotal;  
   
 
 

我得到的值是3.但如果我再次这样做

  echo $ model-&gt; DaysTotal  ; 
   
 
 

现在我得到1.任何想法,为什么我这样得到它。 这发生在estimate.php中的任何函数。 如果我第二次使用它,结果很奇怪。

我在这里做错了吗? 我该如何解决这个问题? 谢谢。

以下是 getTotalDays 函数的代码:

  public  function getDaysTotal(){
 $ this-&gt; discharge_date = strtotime($ this-&gt; discharge_date); 
 $ this-&gt; admission_date = strtotime($ this-&gt; admission_date); 
 
 $ datediff =  ($ this-&gt; discharge_date  -  $ this-&gt; admission_date); 
 
 $ fraction_days =($ datediff /(60 * 60 * 24)); 
 
 if($ fraction_days&lt; 1){\  n返回1; 
 
} elseif(($ datediff)%(60 * 60 * 24)&lt; 10800){
 $ option2 = floor($ datediff /(60 * 60 * 24)); 
返回 $ option2; 
} elseif(($ datediff%86400)&gt; 10800&amp;&amp;($ datediff%86400)&lt; 21600){
 $ option3 = ceil($ datediff /(60 * 60 * 24)*  2)/ 2; 
返回$ option3; 
} elseif(($ datediff%86400)&gt; 21600){
 $ option4 = ceil($ datediff / 86400); 
 返回$ option4; 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题