PHP MySQL Compare $row's dates from today
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:
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>
Answer
Solution:
Hi please replace these query
With these one
Answer
Solution:
This is how I ended up solving this issue:
I hope this logic can help others as well.