"Sunday last week" 周日的 strtotime() 错误



为了让我的孩子开心,我一直在PHP中实施长途旅行的黄色汽车游戏。其中一个坐在我的手机上,在看到它们时添加黄色汽车。柜台应增加他们的每个分数,每个星期日晚上的得分重置并展示上周的获胜者。

上周这是正确的,本周又是又一次,但我在周日注意到所有分数均为0,而我上周的得分根本没有。

<?php
    $host = "localhost";
    $un = "username";
    $pw = "password";
    $db = "database";
    $con = mysqli_connect($host, $un, $pw, $db);
    $thisweek = date('Y-m-d', strtotime('Monday this week'));
    //echo $thisweek;
    $lastweek = date('Y-m-d', strtotime('Monday last week'));
    //echo $lastweek;
    $mquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Matthew') AND (nowdate >= '$thisweek')";
    $mresult = mysqli_query($con, $mquery);
    $mdata = mysqli_fetch_assoc($mresult);
    $mcount = $mdata['total'];
    $rquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Roisin') AND (nowdate >= '$thisweek')";
    $rresult = mysqli_query($con, $rquery);
    $rdata = mysqli_fetch_assoc($rresult);
    $rcount = $rdata['total'];
    $mlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Matthew') AND (nowdate >= '$lastweek') AND (nowdate < $thisweek)";
    $mlresult = mysqli_query($con, $mlquery);
    $mldata = mysqli_fetch_assoc($mlresult);
    $mlcount = $mldata['total'];
    $rlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Roisin') AND (nowdate >= '$lastweek') AND (nowdate < $thisweek)";
    $rlresult = mysqli_query($con, $rlquery);
    $rldata = mysqli_fetch_assoc($rlresult);
    $rlcount = $rldata['total'];
    if ($mlcount > $rlcount) {
        $lcount = "Matthew";
        $first = $mlcount;
        $second = $rlcount;
    }
    else {
        $lcount = "Roisin";
        $first = $rlcount;
        $second = $mlcount;
    }
?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
    <title>Yellow Car</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">
    <link rel="stylesheet" href="styles.css">
    <link rel="icon" href="favicon.ico">
</head>
<body>
    <div class="container">
        <div class="pure-g-r">
            <div class="pure-u-1 text-center">
                <h1>Yellow Car!</h1>
            </div>
        </div>
        <div class="pure-g">
            <div class="pure-u-1-2 padded text-center">
                <form class="pure-form" method="post" action="matt-add.php">
                    <button type="submit" class="pure-button button-car pure-input-1">Matthew</button>
                </form>
                <p>
                    This Week: <strong class="blue-text"><?php echo $mcount; ?></strong>
                </p>
            </div>
            <div class="pure-u-1-2 padded text-center">
                <form class="pure-form" method="post" action="rosh-add.php">
                    <button type="submit" class="pure-button button-car pure-input-1">Roisin</button>
                </form>
                <p>
                    This Week: <strong class="blue-text"><?php echo $rcount; ?></strong>
                </p>
            </div>
        </div>
        <div class="pure-g-r">
            <div class="pure-u-1 text-center">
                <h2>
                    <?php
                        if ($first == $second) {
                    ?>
                        Last week was a <span class="blue-text">draw</span>! (<?php echo $first . "-" . $second; ?>)
                    <?php
                        }
                        else {
                    ?>
                        Last week's winner was: <span class="blue-text"><?php echo $lcount; ?></span>! (<?php echo $first . "-" . $second; ?>)
                    <?
                        }
                    ?>
                </h2>
            </div>
        </div>
    </div>
</body>
</html>

数据库很简单;一个记录日期及其名称的表。添加查询:

INSERT INTO records (who, nowdate) VALUES ('$who', NOW())

我尝试在1月2日星期日回应日期,但它告诉我本周是第9位,上周是第二名,恰好是7天。

如果有人可以建议我的代码中的任何错误,这将不胜感激。

您需要在两个查询中单一引用您的 $thisweek,如下面...

 $mlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Matthew') AND (nowdate >= '$lastweek') AND (nowdate < '$thisweek')";
    $mlresult = mysqli_query($con, $mlquery);
    $mldata = mysqli_fetch_assoc($mlresult);
    $mlcount = $mldata['total'];
    $rlquery = "SELECT COUNT(who) AS total FROM records WHERE (who = 'Roisin') AND (nowdate >= '$lastweek') AND (nowdate < '$thisweek')";

最新更新