我正在尝试创建一个系统,允许我为自己的游戏创建一种日常使用礼物。如果玩家几乎每天都登录游戏(很显然是在当天的重置时间之后),那么他每天的首次访问都会获得奖金。 尽管是PHP和MySql的新手,但我还是尝试编写一些代码。第一部分似乎正常工作,但是编写第二部分代码时遇到问题。
<?php
// dati accesso database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dailybonus";
// Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($mysqli->connect_error) {
die("Connessione fallita");
}
// Check if the player is already present in the database, or if it is his first ever login
$check = $mysqli->query('SELECT * FROM bonus WHERE idgiocatore = "'.$_GET["idgiocatore"].'"');
// If it's your first login
if($check->num_rows == 0) {
echo "Primo login del giocatore!<br>";
// I insert his id and the current date / time in the database
$now = new DateTime();
$update = $mysqli->query('INSERT INTO bonus (idgiocatore,ultimoaccesso) VALUES ("'.$_GET["idgiocatore"].'","'.date_format($now, 'd-m-Y H:i:s').'")');
// If there are no errors I return "true" for the bonus assignment
if ($update) {
echo "true";
// Otherwise I return the error
} else { die("ERRORE");}
} else {
// Otherwise if the player is already present in the database
$now = new DateTime();
$resetTime = new DateTime();
date_time_set($resetTime, 12, 00, 00);
$a = $mysqli->query('SELECT ultimoaccesso FROM bonus WHERE idgiocatore = "'.$_GET["idgiocatore"].'"');
$lastLogin = new DateTime((string)$a);
echo "LastLogin: " . date_format($lastLogin, 'd-m-Y H:i:s') . "<br>";
echo "Reset time: " . date_format($resetTime, 'd-m-Y H:i:s') . "<br>";
echo "time: " . date_format($now, 'H') . "<br>";
if ($resetTime < $now) {
echo "ok1<br>";
if ($resetTime < $lastLogin) {
echo "ok2";
}
}
$update = $mysqli->query('UPDATE bonus SET ultimoaccesso=NOW() WHERE idgiocatore="'.$_GET["idgiocatore"].'"');
if ($update) {
echo "Aggiornato";
} else { die("ERRORE");}
}
$mysqli->close();
?>
当前我无法比较日期,因为两个日期之一不是字符串,而且我不知道如何转换。显然,我不确定我使用的是获得所需结果的最简单/最快的方法。
你能帮我一下吗?