gpt4 book ai didi

php - "Sunday last week"周日 strtotime() 错误

转载 作者:行者123 更新时间:2023-11-29 13:15:14 27 4
gpt4 key购买 nike

为了让我的 child 快乐,我一直在实现 Yellow Car Game 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 号,上周是 2 号,正好 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')";

关于php - "Sunday last week"周日 strtotime() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21556751/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com