如何确保页面仅在一段时间内可用

问题描述 投票:0回答:1

我正在做一个项目,使用html、php和js记录学生的出勤情况。一段时间后,学生应该无法记录他们的出勤情况。

$start_time = "08:00";
$end_time = "04:06";

$current_time = date("H:i");

if ($current_time < $start_time || $current_time > $end_time) {
// Outside of the allowed time range, redirect or display a 
message
header("Location: error_page.php"); // Redirect to an error page
exit();
}

这是我使用的代码,甚至在结束时间之前它就立即将我重定向到错误页面。

php
1个回答
0
投票

看来问题可能与结束时间的格式有关。时间“04:06”似乎暗示清晨(4 AM),这比您的开始时间“08:00”(上午 8 点)早。这将导致条件 $current_time > $end_time 在下午始终为 true,因此重定向。

如果您打算将结束时间设置为下午 4:06,则应使用 24 小时格式“16:06”。这是代码的更正版本:

$start_time = "08:00";
$end_time = "16:06";

$current_time = date("H:i");

if ($current_time < $start_time || $current_time > $end_time) {
    // Outside of the allowed time range, redirect or display a message
    header("Location: error_page.php"); // Redirect to an error page
    exit();
}

通过此调整,您的脚本应正确允许在上午 8:00 到下午 4:06 之间进行出勤记录,并在这些时间之外重定向到错误页面。

© www.soinside.com 2019 - 2024. All rights reserved.