2012-07-09 75 views
0

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

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

我的网站是PHP的MySQL后端。

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

这将不胜感激,如果有人有一些示例代码,我试着按时阅读PHP的时间,但它炸我的头。

+1

你有什么至今?你的数据库的结构是什么? – 2012-07-09 00:47:00

回答

0

您可以使用time()数据类型在startTime和endTime字段中弹出数据库。进入这些领域,输入他们可用的时间以及他们必须回来的时间。

然后,查询获取可用项目是一件简单的事情。

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

我基本上已经做到了这一点,似乎工作,你知道什么最好的办法是转换为gmt?使用我有$ now = date('H:i:s')的时间格式;谢谢 – Guernica 2012-07-09 02:08:30

+0

如果您的用户将沿着不同的时区分布,请让他们在当地时区输入并进行修改。您还可以使用http://php.net/manual/en/function.date-default-timezone-set.php来设置PHP计算的时区,或者在php.ini文件中对其进行配置。 – Fluffeh 2012-07-09 03:22:34

+0

我在PHP中设置时区,只是从它的工作,代码将只从一个内部位置执行,所以这是足够的谢谢 – Guernica 2012-07-09 23:15:42

1

这里假定数据库在项目表中有两列,分别叫做available_startavailable_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),但这应该让你开始。

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

1

time()date(),也许mktime()是你所需要的。只需在与时间范围内的时刻,你允许/禁止观看内容

PHP date() manual比较时 - 这是一个真正的强大功能

还有一个时区的问题,你可能会考虑

相关问题