PHP MySQL比较今天的$ row' s

时间:2016-06-24 08:38:51

标签: php mysql date

我想知道是否可以将所有$ row中的日期从while循环到今天的日期进行比较。让我解释一下。

我已创建此图所示的此表格: Betting History

我通过连接几个表创建了这个表,并且实际上在while循环中有一个while循环(这就是我在一行中显示多个游戏的方式,但每行只有一个下注量。

我想要做的是将每个游戏的日期与今天的日期进行比较,以列出parlay是否处于活动状态(在此图片中,它们都不会处于活动状态,但是&# 39;不是重点)。

我想采取今天的日期,如果它少于游戏的所有日期,那么Active ==' Future'或者如果今天的日期介于两者之间一组日期,然后有效= ='进行中'如果今天的日期超过所有日期,则有效==过去'。造成这种挑战的一个部分是我不知道会有多少个人游戏,所以我认为这样的事情可能有用:

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

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

但我不知道要测试我的最后约会行数,也许是

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

如果它是正确的逻辑,不知道如何实现和使用它!

那么有谁知道如何将这些日期行中的每一行与今天的当前日期进行比较?

这是我的初始选择查询: 我已经从我的图片中省略了一些信息,试图让问题的核心逻辑变得更容易,这就是为什么这里有一些额外的字段

//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__);

这是我的表创建代码(第二个循环来获取单个游戏):

  <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>

2 个答案:

答案 0 :(得分:0)

嗨,请替换这些查询

$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";

使用这些

$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";

答案 1 :(得分:0)

这就是我最终解决这个问题的方法:

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";
            }

我希望这种逻辑能够帮助其他人。