Аймен Ахмед
Аймен Ахмед

Reputation: 555

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

I have the following code:

$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 
  }
}

The data is stored like this:

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

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

I want to get the visitors from the last 24 hours.

I don't want to use MySQL db, I just want to use the txt file but I'm stuck.

Thanks in advance.

Upvotes: 0

Views: 241

Answers (1)

Rajat Singhal
Rajat Singhal

Reputation: 11264

I can see 2 problems: 1st you are comparing the stored time with current time and saying that it will filter array item if its date not within 24 hours..

I think you should use $today = strtotime("-1 day"); and name yesterday instead of today..

Secondly and reason of error is that you are exploding data in file with , which will give you "" ie null for last element in array..and thats why strtotime function is giving error for that value..

what you should do is:

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
      }
}

Upvotes: 1

Related Questions