Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在一天中的特定时间禁用项目_Php_Mysql_Time_Restrictions - Fatal编程技术网

Php 在一天中的特定时间禁用项目

Php 在一天中的特定时间禁用项目,php,mysql,time,restrictions,Php,Mysql,Time,Restrictions,我有一个基于请求的网站,用户可以请求项目,我想在一天中的特定时间限制一些请求 例如,用户只能在下午5点到8点之间请求item5。剩下的时间,它是不可用的 我的网站是在PHP与MySQL后端 理想情况下,我喜欢一个可以用项目id调用的函数,该函数将检查数据库中的时间值,并返回真值或假值 如果有人有一些示例代码,我将不胜感激。我曾尝试用PHP按时阅读,但这让我头疼。这假设数据库在item表中有两列,分别称为available\u start和available\u end。这两个都假定为DATETI

我有一个基于请求的网站,用户可以请求项目,我想在一天中的特定时间限制一些请求

例如,用户只能在下午5点到8点之间请求item5。剩下的时间,它是不可用的

我的网站是在PHP与MySQL后端

理想情况下,我喜欢一个可以用项目id调用的函数,该函数将检查数据库中的时间值,并返回真值或假值


如果有人有一些示例代码,我将不胜感激。我曾尝试用PHP按时阅读,但这让我头疼。

这假设数据库在item表中有两列,分别称为available\u start和available\u end。这两个都假定为DATETIME

function isItemAvailableNow(item_id){
    # Globals are bad but this gets the point across
    global $db;

    # Get the item from the database
    $result = mysql_query("SELECT * FROM items WHERE id = " . mysql_real_escape_string(item_id, $db) . ";");
    $row = mysql_fetch_assoc($result);

    # Pull the beginning and ending availablity time out of the database result
    $available_start = $row['available_start']
    $available_end   = $row['available_end']

    # Get the current time in the same format as the MySQL DATETIME type
    $now = date('%Y-%m-%d %H:%i:%s');

    return ($now >= $available_start) && ($now <= $available_end);
}
这段代码还没有经过测试,我做了一些假设,比如您正在使用MySQL,但这应该可以让您开始


您还需要考虑时区,但这是另一种讨论。

您可以使用时间数据类型弹出StimeTimes和EntEndot字段到ToT数据库中。在这些字段中,输入它们的可用时间以及必须返回的时间

然后,查询返回可用项是一件简单的事情

tableAvailability
|--------------------------------|
| id     | availStart | availEnd |
|--------------------------------|
| 1      | 15:00:00   | 18:00:00 |
| 2      | 12:00:00   | 12:30:00 |
|--------------------------------|


$startTime='15:30:00';
$requiredTime=2; // Showing Hours for example

$sql="
select
    id
from
    tableAvailability
where
    ".$startTime." >= availStart
    and addTime('".$startTime."', '".$requiredTime.":00:00')<availEnd";
时间,日期,也许时间就是你需要的。只需将当前时间与允许/不允许查看内容的时间范围进行比较

-这真是一个强大的功能


还有一个时区问题,您可以考虑

到目前为止您有什么?你的数据库的结构是什么?我基本上已经这样做了,而且似乎有效,你知道最好的方法是转换成gmt吗?使用我的时间格式$now=date'H:i:s';感谢如果您的用户将分布在不同的时区,请让他们输入本地时区并进行修改。您还可以使用设置由PHP计算的时区,或者在PHP.ini文件中进行配置。我在PHP中设置了时区,并从中开始工作,代码将仅从一个内部位置执行,因此这就足够了,谢谢