Austin Austin - 1 year ago 41
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) :


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);

echo "
<table class='table-fill'>
<th class='text-left' style='padding-left: 12px; padding-right: 12px;';></th>
<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';
case 6:
$rank = 'Community Moderator';
case 9:
$rank = 'Senior Moderator';
case 12:
$rank = 'Supervisor';
case 15:
$rank = 'Administrator';
case 18:
$rank = 'Senior Administrator';
case 21:
$rank = 'Staff Advisor';
case 24:
$rank = 'Engineer';
case 27:
$rank = 'Vice Chairman';
case 30:
$rank = 'Chairman';
case 33:
$rank = 'Website Engineer';
$rank = 'Unknown';
echo "<tr>";
echo "<td>" . $results['Username'] . " - " . $rank . "</td>";
} else {
echo "<tr><td>{$results['Username']} &ndash; $rank</td>";
echo "<tr><td>There are no staff members online.</td>";
echo " </tbody>


Answer Source

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'])) {}