循环获取数组的总和

Get totals of an array in a loop

正在尝试查找投资物业的总空置天数...

我从 mysql 获取信息,将其放入循环数组中以查找不同行中日期之间的差异。

我能够 return 每个 "move out" 和 "move in" 日期之间的差异,但无法获得运行总数。 下面的代码 return 这个...

2014 年 1 月 25 日和 2014 年 1 月 27 日在物业 # 7 上的差异 = 2

2014 年 11 月 3 日和 2014 年 11 月 23 日物业# 7 之间的差异 = 20

  mysql_select_db($database_rent, $rent);
    $query_RS_Vac = "SELECT Properties.PropId, Tenants.TenantId, Tenants.PropertyID,      Tenants.TenantAdress, Tenants.MoveIn, Tenants.MoveOut, Properties.P_GpNo FROM Properties,   Tenants WHERE Tenants.PropertyID=Properties.PropId  AND Tenants.TenantAdress = "7"
    ORDER BY Tenants.TenantId ";
    $result = mysql_query($query_RS_Vac) or die ("no query");
    $result_array = array();      
    while ($row = mysql_fetch_array($result)) {
    $values = array($row['MoveIn'],$row['MoveOut'], $row['TenantAdress']);
    array_push($result_array,$values);
    }
    $it = 0;
    $PropId=$values[2];
    while($it<count($result_array)){
    // if neither begin and end are NULL 
    if($result_array[$it][1]!= null & $result_array[$it+1][0]!=null){
        $datetime1 = new DateTime($result_array[$it][1]);
        $datetime2 = new DateTime($result_array[$it+1][0]);
        $diff = $datetime1->diff($datetime2);
        $days2= $diff->format("%a"); 
      echo "Difference between ".$datetime1->format("Y-m-d")." and ".$datetime2->format("Y- m-d")." on Property# " . $PropId . " = " . $days2 . "<br/>";
   } 
     $it++;       
     }

2 种方式

$total_days[]=$days2;
array_sum($total_days);

$total_days+=$days2;