douliang4858 2014-10-08 10:16
浏览 15

PHP / PDO / sqlsrv - 如何在列表中将每两个日期之间的每个用户的所有数据都获取

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 支付宝批量检测实名工具
    • ¥15 基于单片机的靶位控制系统
    • ¥15 AT89C51控制8位八段数码管显示时钟。
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题