Well assuming I'm right about those time fields (and I'm not sure), I would say change these two lines to:
Code:
$rep = $query['radded'];
$tick = $res['tadded'];
(or I guess you don't really need the intermediate variables, but whatever). When you say those fields are "unix time", what's the actual type of the column (or is that a type I don't know about?). For instance, vBulletin saves unix times, but it uses a database type of INT (unsigned, that is).
Also, change this line to
Code:
$hour = floor($avg/3600);
That one I'm pretty sure about, but I don't see how that alone would explain what you're seeing.