为什么这段代码导致空格?

时间:2016-07-23 18:00:47

标签: php

我的目标是减去两次,然后从那里生成一个登录的检查器。

我需要知道的是,如果我在几分钟内正确地减去了两次。

PHP版本:7.0

使用NOW()输入时间并显示为(例如:2016-07-23 15:01:34)

出于某种原因,此代码包含在HTML中,只是空白。

代码(一切都在代码中定义得更高):

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

?>

1 个答案:

答案 0 :(得分:1)

据我了解,now()没有按照您的预期获得当前时间。请参阅this问题,我认为这是您误解的根源。

假设$results['LastVisited']的格式为2016-07-23 14:11:02 - 您可以使用strtotime将其转换为UNIX时间戳。

因此,如果15分钟前的时间小于或等于您上次访问的时间:

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

使用time()的替代格式 - 您将使用而不是MySQL等效UNIX_TIMESTAMP()

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