我正在尝试使用以下数据将Multidimentional数组排序为15分钟增量,这是来自呼叫中心数据库的电话记录数据
CallID CallDateTime IsLost IsAnswered
82650 May 10 2016 08:55:01:000AM 0 1
82666 May 10 2016 09:03:13:000AM 0 1
82677 May 10 2016 09:07:45:000AM 0 1
82688 May 10 2016 09:12:19:000AM 0 1
82689 May 10 2016 09:14:42:000AM 0 1
82702 May 10 2016 09:22:08:000AM 0 1
82708 May 10 2016 09:26:26:000AM 0 1
82737 May 10 2016 09:39:42:000AM 0 1
82739 May 10 2016 09:43:17:000AM 0 1
82748 May 10 2016 09:45:22:000AM 0 1
82786 May 10 2016 09:59:26:000AM 0 1
82789 May 10 2016 10:00:21:000AM 0 1
82820 May 10 2016 10:26:52:000AM 0 1
82842 May 10 2016 10:37:14:000AM 0 1
82846 May 10 2016 10:39:04:000AM 0 1
82859 May 10 2016 10:48:30:000AM 0 1
82865 May 10 2016 10:50:18:000AM 0 1
82880 May 10 2016 10:56:47:000AM 0 1
82911 May 10 2016 11:07:30:000AM 0 0
82925 May 10 2016 11:15:34:000AM 0 1
82926 May 10 2016 11:16:08:000AM 0 1
82927 May 10 2016 11:16:17:000AM 0 1
82943 May 10 2016 11:23:16:000AM 0 1
82944 May 10 2016 11:25:54:000AM 0 1
82947 May 10 2016 11:26:48:000AM 0 1
82966 May 10 2016 11:33:27:000AM 0 1
83048 May 10 2016 12:14:39:000PM 0 1
我从MSSQL Server数据库获取,通过以下PHP代码放入数据
$sql = "SELECT CallID, CallDateTime, IsLost, IsAnswered FROM vwCustomReportingCallsMain WHERE DDI = '2399' AND datediff(day, CallDateTime, '2016-05-10') = 0 ORDER BY CallDateTime ASC";
foreach ($dbh->query($sql) as $row)
{
if($row['CallID'] != $last) {
$callDataArray[] = array("CallID" => $row['CallID'], "CallDateTime" => $row['CallDateTime'], "IsLost" => $row['IsLost'], "IsAnswered" => $row['IsAnswered']);
}
$last = $row['CallID'];
}
所需的输出将类似于以下格式(数据不正确)
Date/Time IsAnswered IsLost
May 10 2016 08:30 7 0
May 10 2016 09:00 8 0
May 10 2016 09:30 14 0
May 10 2016 10:00 11 0
May 10 2016 10:30 11 0
May 10 2016 11:00 13 0
May 10 2016 11:30 12 0
May 10 2016 12:00 11 0
May 10 2016 12:30 8 0
May 10 2016 13:00 20 0
May 10 2016 13:30 9 0
May 10 2016 14:00 10 0
May 10 2016 14:30 12 0
May 10 2016 15:00 8 0
May 10 2016 15:30 14 0
May 10 2016 16:00 12 0
May 10 2016 16:30 11 0
关于如何在PHP甚至MSSQL PDO中的任何想法? 任何帮助都会很棒 - 干杯
答案 0 :(得分:1)
这是我将使用的SQL:
SELECT dte = DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CallDateTime) / 15 * 15, 0) AS CallPeriod, SUM(IsAnswered) AS IsAnswered, SUM(IsLost) AS IsLost
FROM vwCustomReportingCallsMain
WHERE DDI = '2399' AND datediff(day, CallDateTime, '2016-05-10') = 0
GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, CallDateTime) / 15 * 15, 0)
ORDER BY dte ASC;
请注意,我没有可以访问的MS SQL服务器,但它应该非常接近。
答案 1 :(得分:0)
这是一个PHP解决方案:
您可以使用以下代码将当前时间四舍五入到最接近的15分钟:
$time = strtotime('May 10 2016 08:55:01:000AM');
$rounded = round($time / (15 * 60)) * (15 * 60);
所以对你来说就像是:
$sql = "SELECT CallID, CallDateTime, IsLost, IsAnswered FROM vwCustomReportingCallsMain WHERE DDI = '2399' AND datediff(day, CallDateTime, '2016-05-10') = 0 ORDER BY CallDateTime ASC";
$callDataArray = array();
foreach ($dbh->query($sql) as $row)
{
if($row['CallID'] != $last) {
$time = strtotime($row['CallDateTime']);
$date = round($time / (15 * 60)) * (15 * 60);
if (!isset($callDataArray[$date])) {
$callDataArray[$date] = array(
'isLost' => $row['IsLost'],
'isAnswered' => $row['IsAnswered'],
);
} else {
$callDataArray[$date]['isLost'] = $callDataArray[$date]['isLost'] + row['IsLost'];
$callDataArray[$date]['isAnswered'] = $callDataArray[$date]['isAnswered'] + row['IsAnswered'];
}
}
$last = $row['CallID'];
}
然后你可以像$callDataArray
一样遍历:
<table><tr><th>Date</th><th>Answered</th><th>Lost</th></tr>
<?php
foreach($callDataArray as $date => $data) {
echo '<tr>';
echo '<td>' . date('jS \of F Y h:i:s A', $date) . '</td>';
echo '<td>' . $data['isAnswered'] . '</td>';
echo '<td>' . $data['isLost'] . '</td>';
echo '</tr>';
}
?>
</table>
答案 2 :(得分:-1)
您可以使用以下代码。它可能不完美,但它会让你知道如何进一步。
function roundToNearest30($timestring) {
$minutes = date('i', $timestring);
return sprintf("%02d", $minutes - ($minutes % 30));
}
$totalArray = array();
$lastTime = NULL;
foreach($callDataArray as $callData){
$timeString = strtotime($callData['CallDateTime']);
$currentHour = date('H', $timeString);
$nearest30Minutes = roundToNearest30($timeString);
$nearestTime = "{$currentHour}:{$nearest30Minutes}";
//create array key based on nearest 30 minutes
if (!isset($totalArray[$nearestTime])){
//add the array element as it does not exist
$totalArray[$nearestTime] = array("TotalLost" => $callData['IsLost'], "TotalAnswered" => $callData['IsAnswered']);
}else{
//update they array element with previous data
$totalArray[$nearestTime]["TotalLost"] += $callData['IsLost'];
$totalArray[$nearestTime]["TotalAnswered"] += $callData['IsAnswered'];
}
}
foreach($totalArray as $nearest30 => $data){
echo "{$nearest30}, Total Lost: {$data['TotalLost']}, Total Answered: {$data['TotalAnswered']}";
echo "<br>";
}
Output:
08:30, Total Lost: 0, Total Answered: 1
09:00, Total Lost: 0, Total Answered: 6
09:30, Total Lost: 0, Total Answered: 4
10:00, Total Lost: 0, Total Answered: 2
10:30, Total Lost: 0, Total Answered: 5
11:00, Total Lost: 0, Total Answered: 6
11:30, Total Lost: 0, Total Answered: 1
12:00, Total Lost: 0, Total Answered: 1