PHP - Count elements for the past 15 days with time interval

43 views Asked by At

I'm trying to count rows based on when they were created.

Each day should be different but it just returns me 2 days.

For instance 1 day ago it should count 2, 6, 8 (because total is number 2 plus number 6) and so on. There are date on the database but it looks like after the second look it get stuck.

Here the code

$dayLosses = array();
$daywins = array();
$dayTotal = array();
$addCounter = 1;
$counter2 = 2;
while($addCounter < 15){

    //losses
    $query = "SELECT * FROM `signals` WHERE userID = ? AND outcome = 1 AND created_at BETWEEN 
                DATE_SUB(DATE(NOW()), INTERVAL $counter2  DAY)
                    AND DATE_SUB(DATE(NOW()), INTERVAL 1 DAY) ";
    //counting losses
    $result = $pdo->prepare($query);
    $result->bindParam(1, $userID);
    if ($result->execute()) {
        $dayLosses[] = $result->rowCount();
    }

    $query = "SELECT * FROM `signals` WHERE userID = ? AND outcome = 2 AND created_at BETWEEN 
                DATE_SUB(DATE(NOW()), INTERVAL $counter2 DAY)
                  AND DATE_SUB(DATE(NOW()), INTERVAL $addCounter DAY) ";
    //counting winners
    $result = $pdo->prepare($query);
    $result->bindParam(1, $userID);
    if ($result->execute()) {
        $daywins[] = $result->rowCount();
    }

    $dayTotal[] = $day2Loss + $day2Wins;

    $counter2 += 1;
    $addCounter += 1;
}

echo '<pre>'; print_r($dayLosses); echo '</pre>';
echo '<pre>'; print_r($daywins); echo '</pre>';
echo '<pre>'; print_r($dayTotal); echo '</pre>';
?>

Here's the output

Array
(
    [0] => 2
    [1] => 1
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    [12] => 0
    [13] => 0
)
Array
(
    [0] => 6
    [1] => 1
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
    [10] => 0
    [11] => 0
    [12] => 0
    [13] => 0
)
Array
(
    [0] => 8
    [1] => 8
    [2] => 8
    [3] => 8
    [4] => 8
    [5] => 8
    [6] => 8
    [7] => 8
    [8] => 8
    [9] => 8
    [10] => 8
    [11] => 8
    [12] => 8
    [13] => 8
)

What am I doing wrong?

1

There are 1 answers

1
Daniel On

The output above, is that the arrays $dayLosses = array(); and $daywins = array();?

What is the purpose of this line: $dayTotal[] = $day2Loss + $day2Wins;? I can't find those variables anywhere else in your script.