php - Creating hour statistics using arrays and memcached -
i trying count how many hits have on site each hour, not sure how approach this.
here have now:
if($cacheavailable == true){ // got 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 $k => $v) { if($v['hour'] == $thishour){ $movestats[$k]['hits']=$movestats[$k]['hits']+1; } } $memcache->set('movestats', $movestats); echo '<pre>'; print_r($movestats); echo '</pre>'; }
this makes array this:
array ( [0] => array ( [hour] => 18 [hits] => 6 [executetime] => 0 ) )
//##### edit ######//
i able add current hour don't know how add new hour when clock turns new hour?
hoping , in advance.
you have check if index exists, if not create new one, , increase old value:
$todaystats = $movestats; if (!isset($todaystats [$thishour])) { $todaystats[$thishour] = 0; } $todaystats[$thishour]['hits']++; $todaystats[$thishour]['executetime'] = $total_time;
but have other problems in implementation: - don't use string without quotes. try call constant name , fallback return string itself. raises notice. - $thishour won't contain current hour. if want have hour try: date('h') only.
Comments
Post a Comment