在循环中获取数组的总数

时间:2015-01-08 17:33:57

标签: php

试图找出投资物业的总空置天数......

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

我可以返回每个“移出”和“移入”日期之间的差异,但我无法获得总计。 下面的代码返回此...

2014-01-25与2014-01-27之间关于物业#7 = 2的差异

2014-11-03和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++;       
     }

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

1 个答案:

答案 0 :(得分:0)

2种方式

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

$total_days+=$days2;