2011-12-31 78 views
0

我有以下代码:如何计算过去24小时的访客?

$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文件,但我卡住了。

在此先感谢。

+0

重复http://stackoverflow.com/questions/8670262/wrong-in-date-comparison – liquorvicar 2011-12-31 10:24:18

回答

1

我可以看到2个问题:1 你比较当前时间的存储时间,他说,如果它的日期不是24小时内,将滤波器阵列项目..

我认为你应该使用今天$ = strtotime(“ - 1 day”); 和名称昨天而不是今天..

其次和错误的原因是您在为文件数据爆炸,它会给你“”,即空在array..and这就是最后一个元素,为什么的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 
     } 
} 
+0

谢谢,我解决了这个问题 – 2011-12-31 10:08:24

相关问题