在每个循环内部调用时,类对象值不会更新。
我尽了最大的努力,但是我不太了解自己在做什么。 我认为我的类缩放和功能不对。
在每个循环内部调用时,类对象值不会更新。
我尽了最大的努力,但是我不太了解自己在做什么。 我认为我的类缩放和功能不对。
<?php
Class Compute{
public $total_lates = 0;
public $interval;
public $normal_in_am;
public $normal_in_pm;
public $normal_out_am;
public $normal_out_pm;
public $user_in_am;
public $user_out_am;
public $user_in_pm;
public $user_out_pm;
public function __construct($in_am, $out_am, $in_pm, $out_pm, $req_in_am, $req_out_am, $req_in_pm, $req_out_pm){
$this->user_in_am = $in_am;
$this->user_out_am = $out_am;
$this->user_in_pm = $in_pm;
$this->user_out_pm = $out_pm;
$this->normal_in_am = $req_in_am;
$this->normal_out_am = $req_out_am;
$this->normal_in_pm = $req_in_pm;
$this->normal_out_pm = $req_out_pm;
}
public function lates(){
if( trim($this->user_in_am) !== ""){
if( strtolower(trim(explode(' ', $this->user_in_am)[2])) == "am"){
$datetime1 = new DateTime($this->normal_in_am);
$datetime2 = new DateTime($this->user_in_am);
$this->interval = $datetime1->diff($datetime2);
$hour_in_am = explode(":", explode(" ", $this->normal_in_am)[1])[0];
$hour2_in_am = explode(":", explode(" ", $this->user_in_am)[1])[0];
if($hour2_in_am == $hour_in_am){
$this->total_lates += (int) $this->interval->format('%i');
}else if($hour2_in_am < $hour_in_am){
$this->total_lates += 0;
}else{
$this->total_lates += ((int) $this->interval->format('%h')*60 + (int) $this->interval->format('%i'));
}
}else{
if( strtolower(trim(explode(' ', $this->user_in_am)[2])) == "pm"){
$datetime1 = new DateTime($this->normal_in_pm);
$datetime2 = new DateTime($this->user_in_am);
$this->interval = $datetime1->diff($datetime2);
// echo $this->user_out_am;
$hour_in_pm = (int) explode(":", explode(" ", $this->normal_in_pm)[1])[0];
$hour2_in_pm = (int) explode(":", explode(" ", date('m-d-y H:i:s a', strtotime($this->user_in_am)))[1])[0]; // convert 24 hrs
if($hour2_in_pm == $hour_in_pm){
$this->total_lates += (int) $this->interval->format('%i');
}else if($hour2_in_pm < $hour_in_pm){
$this->total_lates += 0;
}else{
$this->total_lates += ((int) $this->interval->format('%h')*60 + (int) $this->interval->format('%i'));
}
}
}
}
// AFTERNOON COMPUTATION OF LATES
if( trim($this->user_in_pm) !== ""){
if( strtolower(trim(explode(' ', $this->user_in_pm)[2])) == "pm"){
$datetime1 = new DateTime($this->normal_in_pm);
$datetime2 = new DateTime($this->user_in_pm);
$this->interval = $datetime1->diff($datetime2);
$hour_in_pm = explode(":", explode(" ", $this->normal_in_pm)[1])[0];
$hour2_in_pm = explode(":", explode(" ", date('m-d-y H:i:s a', strtotime($this->user_in_pm)))[1])[0]; // convert 24 hrs
if($hour2_in_pm == $hour_in_pm){
$this->total_lates += (int) $this->interval->format('%i');
}else if($hour2_in_pm < $hour_in_pm){
$this->total_lates += 0;
}else{
$this->total_lates += ((int) $this->interval->format('%h')*60 + (int) $this->interval->format('%i'));
}
}
}
return $this->total_lates;
}
public function __destruct(){
}
}
?>
foreach(...){
$compute = new Compute($dtr_in1, $dtr_in2, $dtr_out1, $dtr_out2, $InAM, $OutAM, $InPM, $OutPM);
echo $compute->lates();
}
应该计算每行的结果,但所做的只是将当前值附加到下一行。