我正在尝试在开始时间和结束时间使用date_diff
。当开始时间是12:59:58而结束时间是01:00:00时,我得到的差异是11:59:58(而不是2秒)。虽然看起来很清楚这里发生了什么,但我究竟能修复它吗?这对我来说似乎是个错误,但对我来说可能是一个问题。
以下是我正在使用的代码:
function getGPSData($siteID, $fromDate, $toDate)
{
global $pdo;
$GPSarray = array();
$maxGPS;
$minGPS;
$avgGPS;
$totalGPS=new DateTime("@0",new DateTimeZone("UTC"));//new DateTime("@0");
$totalEvents=0;
$query = "SELECT id, siteID, call_type, start_time, end_time FROM call_log WHERE siteID=:siteID AND (start_time BETWEEN :fromDate AND :toDate) AND (end_time BETWEEN :fromDate AND :toDate) AND call_type IN ('LrrpOneTimeReq','LrrpStoreReq','LrrpUseStoredReq')";
$stmt = $pdo->prepare($query);
$stmt->bindParam(":siteID", $siteID);
$stmt->bindParam(":fromDate", $fromDate);
$stmt->bindParam(":toDate", $toDate);
$stmt->execute();
foreach ($stmt as $row)
{
//HERE IS THE DATE_DIFF
$timeDiff = date_diff(new DateTime($row['start_time'],new DateTimeZone("UTC")),new DateTime($row['end_time'],new DateTimeZone("UTC")), true); //force absolute
//echo $row['id'] . " --- difference between " . $row['start_time'] . " AND " . $row['end_time'] . " is " . $timeDiff->format("%H:%i:%s") . "<br />";
if(!isset($maxGPS) OR dateIntervalInSeconds($timeDiff) > dateIntervalInSeconds($maxGPS))
{ $maxGPS = $timeDiff; echo "from " . $row['id'] . " where start=" . $row['start_time'] . " AND end=" . $row['end_time'] . ", maxGPS is now " . $maxGPS->format("%H:%I:%S") . "<br /><br />"; }
if(!isset($minGPS) OR dateIntervalInSeconds($timeDiff) < dateIntervalInSeconds($minGPS))
$minGPS = $timeDiff;
$totalGPS->add($timeDiff);
$totalEvents++;
}
if($totalEvents!=0)
{
//$avgGPS=round($totalGPS->getTimestamp() / $totalEvents);
$avgGPS = average_time($totalGPS->format("H:i:s"),$totalEvents,0);
}
else
{
$avgGPS=0;
$maxGPS=new DateInterval('PT0S');
$minGPS=new DateInterval('PT0S');
}
//$avgSeconds = new DateInterval("PT" . $avgGPS . "S");
$GPSarray['max'] = $maxGPS->format("%H:%I:%S");
$GPSarray['min'] = $minGPS->format("%H:%I:%S");
$GPSarray['avg'] = $avgGPS;//gmdate("H:i:s",$avgGPS);//$avgSeconds->format("%H:%i:%s");
$GPSarray['total'] = $totalGPS->format("H:i:s");
$GPSarray['events'] = $totalEvents;
return $GPSarray;
}
答案 0 :(得分:1)
问题在于插入的数据忽略了AM / PM,因此所有数据都使用12小时格式,但代码将其视为24小时格式。