我该如何计算过去24小时的访客人数


How do I calculate the visitors for the last 24 hours?

我有以下代码:

$ips = file_get_contents($_SERVER['DOCUMENT_ROOT']."/visitors.txt");
$arr = explode(",",$ips);
$today =  strtotime(date('Y-m-d H:i:s'));
for ($n = 0, $max = count($arr); $n <= $max; $n++) {
$visArr = explode("#",$arr[$n]);
$visDate = strtotime($visArr[1]); //$visArr[1] = 2011-12-27 14:10:45
  if($visDate < $today){
     unset ($arr[$n]);  //remove array item if its date not within 24 hours 
  }
}

数据的存储方式如下:

xxx.xxx.xxx.xxx#2011-12-27 11:56:24,
xxx.xxx.xxx.xxx#2011-12-28 11:56:24,

我想要过去24小时内的访客。

我不想使用MySQL数据库,我只想使用txt文件,但我卡住了。

我可以看到两个问题:首先,您将存储的时间与当前时间进行比较,并说如果日期不在24小时内,它将过滤数组项。

我认为你应该使用$today = strtotime("-1 day");用昨天的名字代替今天的名字。

其次,错误的原因是你在文件中爆炸的数据,这将给你"即null为最后一个元素在数组..这就是为什么strtotime函数给出错误的值..

你应该做的是:

if($visArr[1])
{
    $visDate = strtotime($visArr[1]); //$visArr[1] = 2011-12-27 14:10:45
      if($visDate < $today){
         unset ($arr[$n]);  //remove array item if its date not within 24 hours
      }
}