0

我正在尝试计算每小时在我的网站上的点击次数,但不知道如何处理这个问题。

这是我现在拥有的:

if($cacheAvailable == true){ // WE GOT A CACHE

    date_default_timezone_set("UTC");
    $thisHour = date("H", time());

    $moveStats = $memcache->get('moveStats');

    if(!$moveStats){
        $todayStats = array(array(hour => $thisHour, hits => 1, executetime => $total_time));
        $memcache->set('moveStats', $todayStats);
    } 


    foreach ($moveStats as $k => $v) {
        if($v['hour'] == $thisHour){
            $moveStats[$k]['hits']=$moveStats[$k]['hits']+1;
        }
    }

    $memcache->set('moveStats', $moveStats);

    echo '<pre>';
    print_r($moveStats);
    echo '</pre>';

}

这使得一个像这样的数组:

Array
(
    [0] => Array
        (
            [hour] => 18
            [hits] => 6
            [executetime] => 0
        )

)

//##### 编辑 ######//

我可以添加到当前小时,但我不知道如何在时钟变成新小时时添加新小时?

希望得到帮助并提前感谢。

4

1 回答 1

0

You just have to check if that index already exists, if not create a new one, and always increase the old value:

$todayStats = $moveStats;
if (!isset($todayStats [$thisHour])) {
    $todayStats[$thisHour] = 0;
}
$todayStats[$thisHour]['hits']++;
$todayStats[$thisHour]['executetime'] = $total_time;

But you have some other problems in your implementation: - Don't use string without quotes. That will try to call a constant with that name and only as fallback return the string itself. It also raises a notice. - $thisHour won't contain the current hour. If you really want to have the hour try: date('H') only.

于 2013-04-05T18:13:47.963 回答