2

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;
               } 
Pawan
  • 3,864
  • 17
  • 50
  • 83

2 Answers2

3

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.

vp_arth
  • 14,461
  • 4
  • 37
  • 66
0

It's funny that you're getting anything because "echo $var" might be a non-object.

    <?php
       $a = 6;
       echo $a -> b;
    ?>

PHP Notice:  Trying to get property of non-object.

IN PHP the right pointing arrow "->" is used to access the component parts of an object, in php it is similar to "::" or the humble "." in languages like java and the C family.

Without more context it is impossible to tell what exactly is happening in you're case but perhaps this page on the "->" will be helpful for you.

If that dosn't give you what you need here is a general PHP note card

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50
  • No for the first time I am getting the correct result. Only problem is when I am using it again, I am facing the problem. – Pawan Dec 23 '14 at 19:15