I have been searching for days for a solution to a code I am trying to create in PHP.
I have this game where the names of the players and guests are updated in a database, every 5 seconds along with the timeframe they have been active in the game, similar to the example below:
$players = "Jennifer,2016-02-14 10:41:02|Don,2016-02-14 11:41:02|Scoop,2016-02-14 10:41:02|";
I would like to loop through $players
and remove any name along with the timeframe where it shows that the player has been inactive for 3 minutes, based on the current time.
I really hope that my question is clear enough and that someone out there could point me in the right direction. I would greatly appreciate it.
$game_id=1; $name='Jennifer';
//First I select all of the players in the game
$sql = 'SELECT players FROM gamesvalues WHERE game_id=?'; /* Prepare statement */
$stmt = $conn->prepare($sql);if($stmt === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} /* Bind parameters. TYpes: s = string, i = integer, d = double, b = blob */
$stmt->bind_param('i',$game_id);
/* Execute statement */
$stmt->execute();
$stmt->bind_result($players);
while ($stmt->fetch())
//Next find out if the name "Jennifer" exists or positioned in players list
$pos = strpos($players, $name);
if ($pos === false) {
echo "The string '$name' was not found in the string '$guests'";
} else {
//Once the player's position is found,use substr and strpos to seperate the
//player's name and active time from the list
$activeplayer= substr($players,$pos, strpos($players, '|'));
//Then use explode to find the active time of the player
$playertime = explode(",", "$activeplayer");
$lastactive_time=strtotime($playertime[1]);
$current_time = strtotime(date("h:i:sa"));
//Since I have a time limit of 3 mins for inactive players to be in game,
//I subtract the last active time of the player from the current time using strotime
//If the time is greater than 3 minutes, I remove the player by using str_replace
$timedifference=round(abs($lastactive_time - $current_time) / 60);
if ($timedifference>3)
$newusr=str_replace($activeplayer.'|',"",$players); }