尝试从非对象中减去时出错

时间:2014-06-13 19:57:21

标签: php arrays

我正在尝试从数据库在给定时间段内打开和关闭的几分钟内获得总差异。当我的数据库包含的开放时间多于关闭时间时,反之亦然,我收到错误,因为它试图从不存在的关闭时间中减去开放时间。我究竟做错了什么?有没有办法忽略没有匹配的剩余时间?

PHP

$closing = "SELECT log_time FROM log_table WHERE database_name = '$d' AND server_protocol = '98' AND log_date >= '$from_date' AND log_date < '$to_date' ORDER BY log_date DESC, log_time DESC";
 $query = $this->db->prepare($closing);
 $query->execute();
 $closing_time = $query->fetchAll();

 $opening = "SELECT log_time FROM log_table WHERE database_name = '$d' AND server_protocol = '94' AND log_date >= '$from_date' AND log_date < '$to_date' ORDER BY log_date DESC, log_time DESC";
 $query = $this->db->prepare($opening);
 $query->execute();
 $opening_time = $query->fetchAll();

foreach ($opening_time as $o) {
     print "<pre>";
     print "------OPENING TIME -----";
     print_r($o);
     print "</pre>";
     }
foreach ($closing_time as $c) {
     print "<pre>";
     print "------CLOSING TIME -----";
     print_r($c);
     print "</pre>";
     }

输出

FMServer_Sample
------OPENING TIME -----stdClass Object
(
    [log_time] => 14:42:51
)
 ------OPENING TIME -----stdClass Object
(
    [log_time] => 13:24:36
)
------OPENING TIME -----stdClass Object
(
     [log_time] => 10:19:58
)
------CLOSING TIME -----stdClass Object
(
     [log_time] => 15:02:21
)
------CLOSING TIME -----stdClass Object
(
     [log_time] => 14:54:21
)
------CLOSING TIME -----stdClass Object
(
    [log_time] => 14:20:25
)
------CLOSING TIME -----stdClass Object
(
    [log_time] => 12:08:04
)

iCDS
------OPENING TIME -----stdClass Object
(
    [log_time] => 14:40:00
)
 ------OPENING TIME -----stdClass Object
(
    [log_time] => 13:00:00
)
------CLOSING TIME -----stdClass Object
(
     [log_time] => 14:50:00
)
------CLOSING TIME -----stdClass Object
(
     [log_time] => 13:10:00
)

当我尝试用开始时间减去开放时间的收盘时间时......

$result = 0;
for ($x = 0; $x < count($closing_time); $x++) {
     if ($closing_time != "" && $opening_time != "") {
           $result += (strtotime($closing_time[$x]->log_time) -strtotime($opening_time[$x]->log_time));
      }
}
$result = round($result / 60);

我得到第二组错误和一个解决方案的数字,结果应为20。

错误

Notice: Undefined offset 19
Notice: Trying to get property of non-object

iCDS 23377537 //should be 20

0 个答案:

没有答案