PHP MySQL Compare $row's dates from today

160

I am wondering if it is possible to compare dates in all $row's from a while loop to today's date. Let me explain.

I have created this table shown in this image: Betting History

I have created this table through joining several tables, and actually have a while loop inside a while loop (which is how I got multiple games to show on one row, but only one wager amount per row.

What I want to do is compare the dates of each of the individual games to today's date, to list if the parlay is active or not (in this image, they would all not be active, but that's not the point).

I want to take today's date and if it is less than all the dates of the games, then Active == 'Future', or if today's date is in between a set of dates, then Active == 'In Progress', and if today's date is past all the dates, then Active == 'Past'. One part that makes this challenging is that I do not know how many individual games there will be, so I'm thinking something like this may work:

/* fetch row */
$firstrow = $result2->fetch_row();

$date1=date_create("$firstrow[0]");
$date2=date_create(date("Y-m-d"));
$diff=date_diff($date1,$date2);

But I don't know how many rows to make to test my last date, perhaps something with a

$row_count = $result->num_rows;
$datelast=date_create("$lastrow[$row-count-1]");

Not sure how to then implement and use this if it is the correct logic!

So does anyone know how I can compare each of these date rows to today's current date?

This is my initial select query: I've omitted some info from my image, to try to make the core logic of the problem easier, that's why there are some extra fields here

//Create parlay select query    
$query = "SELECT
        u.first_name AS 'User First Name',
        u.last_name AS 'User Last Name',
        b.betting_site_name AS 'Betting Site',
        p.id AS 'Parlay ID',
        p.wager AS 'Wager',
        p.odds AS 'Odds',
        p.success AS 'Success',
        DATE_FORMAT(p.creationdate, '%d-%m-%Y') AS 'Date',
        pg.parlayid AS 'PG Parlay ID',
        SUM(p.wager * p.odds) AS Winnings
    FROM parlays p
    JOIN parlaygames pg ON pg.parlayid = p.id
    JOIN bonuses b ON p.bettingsiteid = b.id
    JOIN users u ON p.userid = u.id
    WHERE userid=$id
    GROUP BY p.id
    ORDER BY p.id DESC
    LIMIT 5";

$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

This is my table creation code (and second while loop to get the individual games):

  <h2>Betting History</h2>
      <table class="table table-striped userwinnings">
        <tr>
          <th>Parlay Information</th>
          <th>Wager</th>
          <th>Active</th>
        </tr>
<?php

while($row = $result->fetch_assoc()) {
          $output = '<tr>';
                  $output .= '<td><table><tr><th>Date</th><th>Game</th><th>Bet Info</th></tr>';

                  $parlayid = $row['Parlay ID'];

                  $query3 = "SELECT 
                pg.parlayid AS ParlayID,
                g.date AS GameDate,
                ht.name AS HomeTeam,
                away.name AS AwayTeam,
                pg.betinfo AS BetInfo
            FROM parlaygames pg
            JOIN parlays p ON pg.parlayid = p.id
            JOIN games g ON pg.gameid = g.id
            JOIN teams ht ON g.home_team = ht.id
            JOIN teams away ON g.away_team = away.id
            JOIN users u ON u.id = p.userid
            WHERE p.id = $parlayid";

        $result3 = $mysqli->query($query3) or die($mysqli->error.__LINE__);
        while($row3 = $result3->fetch_assoc()) {
           $gamescount = $result3->num_rows;
            $output .= '<tr>';
            $output .= '<td>'.$row3['GameDate'].'</td>';
            $output .= '<td>'.$row3['HomeTeam'].' vs '.$row3['AwayTeam'].'</td>';
            $output .= '<td>'.$row3['BetInfo'].'</td>';
            $output .= '</tr>';
        }                      

          $output .= '</table>';
                  $output .= '</td>';
                  $output .= '<td>'.$row['Wager'].'</td>';
                  $output .= '<td>'.$row['Active'].'</td>';
                  $output .= '</tr>';

                  echo $output;

}

?>
</table>
907

Answer

Solution:

Hi please replace these query

$query3 = "SELECT 
                pg.parlayid AS ParlayID,
                g.date AS GameDate,
                ht.name AS HomeTeam,
                away.name AS AwayTeam,
                pg.betinfo AS BetInfo
            FROM parlaygames pg
            JOIN parlays p ON pg.parlayid = p.id
            JOIN games g ON pg.gameid = g.id
            JOIN teams ht ON g.home_team = ht.id
            JOIN teams away ON g.away_team = away.id
            JOIN users u ON u.id = p.userid
            WHERE p.id = $parlayid";

With these one

$query3 = "SELECT 
                pg.parlayid AS ParlayID,
                g.date AS GameDate,
                IF((NOW() between MIN(g.date) AND MAX(g.date)),'In Progress',IF((MAX(g.date) > NOW()),'Future','Past')) as Acctive,
                ht.name AS HomeTeam,
                away.name AS AwayTeam,
                pg.betinfo AS BetInfo
            FROM parlaygames pg
            JOIN parlays p ON pg.parlayid = p.id
            JOIN games g ON pg.gameid = g.id
            JOIN teams ht ON g.home_team = ht.id
            JOIN teams away ON g.away_team = away.id
            JOIN users u ON u.id = p.userid
            WHERE p.id = $parlayid group by pg.parlayid";
870

Answer

Solution:

This is how I ended up solving this issue:

while($row3 = $result3->fetch_assoc()) {
           $gamescount = $result3->num_rows;

            /* seek to row no. 1 */
                $result4->data_seek(0);

                /* fetch row */
                $firstrow = $result4->fetch_row();

                $result4->data_seek($gamescount-1);
                $lastrow = $result4->fetch_row();

            $date1=date_create("$firstrow[1]");
            $date3=date_create("$lastrow[1]");
            $date2=date_create(date("Y-m-d"));
            $diff=date_diff($date3,$date2);

            $firstdate = $date3->format('Y-m-d');
            $lastdate = $date1->format('Y-m-d');
            $today = $date2->format('Y-m-d');

            if ($firstdate < $today && $today < $lastdate) {
                $active = "In Progress";
            } else if ($today > $lastdate) {
                $active = "Past";
            } else if ($today < $firstdate) {
                $active = "Future";
            }

I hope this logic can help others as well.

People are also looking for solutions to the problem: php - How can I send a variable from a controller to a behaviour? cakePHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.