I'm trying to add a feature in my application that totals how much time a user spends in the system by the week, month, etc.
$select = "SELECT TOTAL_HRS FROM timeclock WHERE USERNAME = '$sessuser' AND CLOCK_OUT BETWEEN '$dbpast' AND '$dbnow'";
I have a MySql result of two sample time entries: 00:00hr:04min:08s
and 00:00hr:12min:52s
. Users can have more.
TOTAL_HRS is a varchar column, so when I put a sum() on it, it returns a 0.
Here's what I have so far:
while($row = $query->fetch_assoc()){
print_r($row);
$sanitized = preg_replace("/[^0-9:]+/", "", $row);
print_r($sanitized);
$joinarr = implode(':', $sanitized);
$parts = explode(':', $joinarr);
print_r($parts);
$zerodate = new DateTime('0000-01-01 00:00:00');
$addhrs += $parts[1];
$addmin += $parts[2];
print_r($addhrs);
$interval = $zerodate->add(new DateInterval('P' .$parts[0].'DT'.$parts[1].'H'.$parts[2].'M'.$parts[3].'S'));
$totalhrs = $interval->format('%D:%Hhr:%Imin:%Ss');
print_r($totalhrs);
}
I get funky junk in return: 0%Sat:%0012Sat, 01 Jan 0000 00:12:52 +0100:%001121:%st52
What I need to return back is: something like: 00:00hr:17min:00s
. I don't plan on storing this, just want it to display on a page.
I need some help figuring this out. I'm sure there is a better way. I'm not that great at functions. Or should I send the results through jquery and handle them there? Which side is more efficient at handling DateTime? The help would be much appreciated. Thanks!