I have a counter file that records page hits - each hit is a UNIX timestamp
in the file. But I'm only interested in page hits in the last 365 days.
The below code creates an array from the file and counts hits based on
interval - day, month or year.
$avs = file($viscounte r);
foreach ($avs as $val)
{
if($val>(time()-(86400)))
{
++$v24h;
}
if($val>(time()-(2592000)))
{
++$v30d;
}
if($val>(time()-(31536000)))
{
++$v365d;
}
}
echo $v24h;
echo $v30d;
echo $v365;
The problem is speed - the foreach loop is too slow. This is exacerbated
when the file contains timestamps that are more than one year old - the code
has to loop through additional array elements that I'm not interested in -
sometimes in the tens of thousands. I can't modify the file because it
needs to be kept for historical analysis.
How can I only inspect array elements that are less than or equal to one
year old? Other ways to make this more efficient?
Thanks!
in the file. But I'm only interested in page hits in the last 365 days.
The below code creates an array from the file and counts hits based on
interval - day, month or year.
$avs = file($viscounte r);
foreach ($avs as $val)
{
if($val>(time()-(86400)))
{
++$v24h;
}
if($val>(time()-(2592000)))
{
++$v30d;
}
if($val>(time()-(31536000)))
{
++$v365d;
}
}
echo $v24h;
echo $v30d;
echo $v365;
The problem is speed - the foreach loop is too slow. This is exacerbated
when the file contains timestamps that are more than one year old - the code
has to loop through additional array elements that I'm not interested in -
sometimes in the tens of thousands. I can't modify the file because it
needs to be kept for historical analysis.
How can I only inspect array elements that are less than or equal to one
year old? Other ways to make this more efficient?
Thanks!
Comment