I need to get the count of my Logs
database for each user on each hour/day/month/year depending on how far the dates are separated by, and display them in a comma separated list.
I need to outcome to be something like this:
Username1, 30, 20, 4, 20, 30
Username2, 12, 11, 10, 9, 8, 5
Username3, 99, 1, 5, 8
And so on...
What I have right now displays this (shortened down):
Username1, 30,
Username2, 12,
Username3, 99,
20, 11, 1, 4, 10, 5, 20, 9, 8, 30, 5,
This is what I've tried:
<?php
function getAllDatesBetweenTwoDates($strDateFrom, $strDateTo, $intervalSeconds)
{
$aryRange = array();
$iDateFrom = mktime(1, 0, 0, substr($strDateFrom, 5, 2), substr($strDateFrom, 8, 2), substr($strDateFrom, 0, 4));
$iDateTo = mktime(1, 0, 0, substr($strDateTo, 5, 2), substr($strDateTo, 8, 2), substr($strDateTo, 0, 4));
if ($iDateTo >= $iDateFrom) {
array_push($aryRange, date('Y-m-d', $iDateFrom)); // first entry
while ($iDateFrom < $iDateTo) {
$iDateFrom += $intervalSeconds;
array_push($aryRange, date('Y-m-d', $iDateFrom));
}
}
return $aryRange;
}
$fromDate = '2014-08-28';
$toDate = '2014-09-01';
$diff = abs(strtotime($fromDate) - strtotime($toDate));
$years = floor($diff / (365 * 60 * 60 * 24));
$months = floor(($diff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
// if 1 year, show by months
if ($years == 1) {
$seconds = 2764800; // 32 days
}
// if over 1 year, show by years
elseif ($years > 1) {
$seconds = 31536000; // 1 year
}
// If 1 month, show by week
elseif ($months == 1) {
$seconds = 604800; // 1 week
}
// if more than 1 month, show by month
elseif ($months > 1) {
$seconds = 2764800; // 32 days
}
// if 1 week, show by days
elseif ($days == 7) {
$seconds = 86400; // 24 hours
}
// if more than a week, show by weeks
elseif ($day > 7) {
$seconds = 604800; // 1 week
}
// If less than or equal to a day show by the hour
elseif ($day < 2) {
$seconds = 3600; // 1 hour
} else {
$seconds = 86400; // 24 hours
}
$dateArray = getAllDatesBetweenTwoDates($fromDate, $toDate, $seconds);
$property_types = array();
for ($i = 0, $count = count($dateArray); $i < $count; $i++) {
$startDate = $dateArray[$i] . " 00:00:00.000";
if ($dateArray[$i + 1] == "") {
$endDate = $dateArray[$i] . " 23:59:59.000";
} else {
$endDate = $dateArray[$i + 1] . " 23:59:59.000";
}
$sql2 = "SELECT OD.Username, OD.ID,
(SELECT COUNT(*) FROM Logs where 1=1 and StartDateTime>='" . $startDate . "' AND EndDateTime <= '" . $endDate . "' and UserID=OD.ID) as c
FROM dbo.Users OD ORDER BY c DESC";
$query2 = $pdo->prepare($sql2);
$query2->execute();
if (!$query2) {
die($pdo->errorInfo());
}
foreach ($pdo->query($sql2) as $row) {
if ($row['c'] > 0) {
if (in_array($row['ID'], $property_types)) {
echo "";
} else {
array_push($property_types, $row['ID']);
echo "</br>" . $row['Username'] . " , ";
}
echo $row['c'] . ", ";
}
}
}
?>
How could I format this as I need to?