這來自我上一個關於獲取指定數據集[位於此處] [1]的平均時間間隔的問題。我會後再次全部功能:DateTime :: add即使間隔時間應該僅爲秒,也會增加小時數
function getATBData($siteID, $fromDate, $toDate)
{
global $pdo;
$ATBarray = array();
$maxATB;
$minATB;
$avgATB;
$totalATB=new DateTime("@0");
$totalEvents=0;
$timetable;
$query = "SELECT id, siteID, start_time, end_time FROM atb_log WHERE siteID=:siteID AND (start_time BETWEEN :fromDate AND :toDate) AND (end_time BETWEEN :fromDate AND :toDate)";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":siteID", $siteID);
$stmt->bindParam(":fromDate", $fromDate);
$stmt->bindParam(":toDate", $toDate);
$stmt->execute();
foreach ($stmt as $row)
{
$timeDiff = date_diff(new DateTime($row['start_time']),new DateTime($row['end_time']), true); //force absolute
if(!isset($maxATB) OR dateIntervalInSeconds($timeDiff) > dateIntervalInSeconds($maxATB))
$maxATB = $timeDiff;
if(!isset($minATB) OR dateIntervalInSeconds($timeDiff) < dateIntervalInSeconds($minATB))
$minATB = $timeDiff;
$totalATB->add($timeDiff);
echo "added " . $timeDiff->format("%H:%I:%S") . " total is now: " . $totalATB->format("H:i:s") . "<br />";
$totalEvents++;
}
if($totalEvents!=0)
{
$avgATB = average_time($totalATB->format("H:i:s"),$totalEvents,0);
}
else
{
$avgATB=0;
$maxATB=new DateInterval('PT0S');
$minATB=new DateInterval('PT0S');
}
//$avgSeconds = new DateInterval("PT" . $avgATB . "S");
$ATBarray['max'] = $maxATB->format("%H:%I:%S");
$ATBarray['min'] = $minATB->format("%H:%I:%S");
$ATBarray['avg'] = $avgATB;
$ATBarray['total'] = $totalATB->format("H:i:s");
$ATBarray['events'] = $totalEvents;
return $ATBarray;
}
鑑於此功能,我增加了一個輸出語句來嘗試調試(爲什麼我得到這麼大的時間間隔爲我的總時間,當大部分值是幾秒鐘),這就是它的輸出:
added 00:00:02 total is now: 01:00:02
added 00:00:00 total is now: 02:00:02
added 00:00:01 total is now: 03:00:03
added 00:00:01 total is now: 04:00:04
added 00:00:00 total is now: 05:00:04
added 00:00:02 total is now: 06:00:06
added 00:00:00 total is now: 07:00:06
等等。所以看起來,儘管時間只有幾秒鐘,但每次都會增加一個小時。上面$timeDiff
上add()
的呼叫是我如何添加的。
所以問題是 - 有沒有不同的方式來調用add()
函數,使它只會添加秒?我說錯了嗎?
稱它比PHP更熟悉PHP? :)這很好,我可能會重寫這些函數來做到這一點。 這些結果可以從PHP格式化,或者你也可以從SQL中完成? – muttley91
'SEC_TO_TIME'應該這樣做,我會加入它。 – Wrikken
這節省了我大量的工作。非常感謝你。今天學到的教訓是儘可能讓數據庫爲你工作,無論如何主要是爲'DateTime'工作。 – muttley91