Austin Austin - 4 months ago 8
PHP Question

Why does this block of code result in a blank space?

My goal is to subtract the two times and then generate a logged in checker from there.

What I need to know, is if I have correctly subtracted the two times in minutes.

PHP Version: 7.0

Time is entered into the DB using NOW() and shows as (Example: 2016-07-23 15:01:34)

For some reason, where this code is included in HTML, is just blank.

Code (everything is defined higher up in the code) :

<?php

include ('../includes/connection.php');
include ('../scripts/functions.php');

$getOnlineAdmins = $handler->prepare('SELECT * FROM Admins WHERE AdminLevel >= :al AND Status= :stat');
$statn = 1;
$aln = 1;
$getOnlineAdmins->bindParam(':al', $aln);
$getOnlineAdmins->bindParam(':stat', $statn);
$getOnlineAdmins->execute();

echo "
<table class='table-fill'>
<thead>
<tr>
<th class='text-left' style='padding-left: 12px; padding-right: 12px;';></th>
</tr>
</thead>
<tbody class='table-hover'>";

if ($getOnlineAdmins->rowCount() >=1){
echo ("These is one or more rows.");
while ($results = $getOnlineAdmins->fetch()){
if((strtotime($results['LastVisited']) + 900) >= time()){
echo ("Time requirement met.");
switch ($results['AdminLevel']) {
case 3:
$rank = 'In-Training/Intern';
break;
case 6:
$rank = 'Community Moderator';
break;
case 9:
$rank = 'Senior Moderator';
break;
case 12:
$rank = 'Supervisor';
break;
case 15:
$rank = 'Administrator';
break;
case 18:
$rank = 'Senior Administrator';
break;
case 21:
$rank = 'Staff Advisor';
break;
case 24:
$rank = 'Engineer';
break;
case 27:
$rank = 'Vice Chairman';
break;
case 30:
$rank = 'Chairman';
break;
case 33:
$rank = 'Website Engineer';
break;
default:
$rank = 'Unknown';
break;
}
echo "<tr>";
echo "<td>" . $results['Username'] . " - " . $rank . "</td>";
} else {
echo "<tr><td>{$results['Username']} &ndash; $rank</td>";
}
}
}else{
echo "<tr><td>There are no staff members online.</td>";
}
echo " </tbody>
</table>";

?>

Answer

As far as I understand it, now() is not getting the current time as you expect it to. See this question for what I believe to be the root of your misunderstanding.

Assuming $results['LastVisited'] is in the format 2016-07-23 14:11:02 – you could convert it to a UNIX timestamp with strtotime.

So if the time 15 minutes ago is less than or equal to your last visited time:

if (strtotime('-15 minutes') <= strtotime($results['LastVisited'])) {}

Alternative format with time() – that you would use instead of the MySQL equivalent UNIX_TIMESTAMP():

if ((time() - 900) <= strtotime($results['LastVisited'])) {}
Comments