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