0

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

?>
Austin
  • 41
  • 6

1 Answers1

1

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'])) {}
Community
  • 1
  • 1
Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • I am still having an issue. In this code http://pastebin.com/CT7MBLp0 (when it's included in the HTML), is simply returning blank. I have made sure everyone's status is set to 0, and it still will not return "nobody is online" like it should be. Also for further reference, here is the field. https://gyazo.com/15c5818e82aaa46f6a265cf289df2a2a – Austin Jul 23 '16 at 19:23
  • Can you `echo 'test';` in your `if` block successfully? And, under a successful subsequent if check, echo another string. Find out where your script is getting to, or not getting to. You should really use a `switch` instead of the massive if-else chain as well e.g. `switch ($results['AdminLevel'])` see [this](http://pastebin.com/raw/ntTDmrHA) example – Jonathan Jul 23 '16 at 21:07
  • This statement is not ran echo "There are no staff members online."; even when the status' are set to 0 – Austin Jul 23 '16 at 21:16
  • Edited the post to show my new code. Nothing was echoed. – Austin Jul 23 '16 at 21:19
  • can you please debug your script. echo everywhere. See what is going on. – Jonathan Jul 23 '16 at 22:08
  • I think this answer has at least deserved your helpful upvote. I answered your question but you've managed to get issues elsewhere in the script. You can confirm that it is an answer by testing in isolation with dummy variables and dummy data. The only remaining advice I have is to ask you if you have instantiated `$rank` prior to the assignments made. – Jonathan Jul 23 '16 at 23:02
  • I've figured out the issue... it's got something to do with http://pastebin.com/PgBJLqfB – Austin Jul 23 '16 at 23:10