/    Sign up×
Community /Pin to ProfileBookmark

Hey,

So I’m attempting to find average times, im going to be pulling for a table information like so:

[CODE]ID timeIn timeOut
1 16:13:12 18:13:12
2 17:13:11 19:13:12
3 18:11:12 20:12:11
4 19:15:35 21:22:45[/CODE]

And im attempting to subtract the times for each value, then finding the total average of all those. So i’ve been searching google and the web trying to find a simple way to subtract 2 times like so but have been unable to do so. I’ve attempted to explode the hours and minutes and make it into a giant number of seconds, subtract the two and recondense it into hours:minutes:seconds again.

Any advise on how to do this?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiAug 15.2009 — Assuming the times are hh:mm:ss, you could do something like this:

[code=php]
function hms_to_seconds($hms) {
list($h, $m, $s) = explode (":", $hms);
return ($h * 3600) + ($m * 60) + $s;
}

$sql = "SELECT timeIn, timeOut FROM fooTable";
$result = mysql_query($sql);
$times = array();
while ($row = mysql_fetch_assoc($result)) {
$times[] = hms_to_seconds($timeOut) - hms_to_seconds($timeIn);
}
$averageSeconds = array_sum($times) / sizeof($times);
[/code]
Copy linkTweet thisAlerts:
@SodbusterAug 15.2009 — Something like this should also work:[code=php]$total_time = 0;
while ($row = mysql_fetch_assoc($res)) {
$total_time += strtotime($row['timeOut']) - strtotime($row['timeIn']);
}
$average = $total_time / mysql_num_rows($res);
echo date('H:i:s', strtotime('today') + $average);[/code]
Edit: Changed [I]gmdate()[/I] back to [I]date()[/I].
Copy linkTweet thisAlerts:
@SodbusterAug 15.2009 — The code I posted above will produce erroneous results two days out of the year, of course. This should correct that:[code=php]$total_time = 0;
date_default_timezone_set('UTC');
while ($row = mysql_fetch_assoc($res)) {
$total_time += strtotime($row['timeOut']) - strtotime($row['timeIn']);
}
$average_time = date('H:i:s', $total_time / mysql_num_rows($res));
date_default_timezone_set(ini_get('date.timezone'));
echo $average_time;[/code]
Copy linkTweet thisAlerts:
@welshauthorAug 16.2009 — yea, mainly thats how i got my time figured out also, i was just curious if there was some basic built in php function that i had missed, thanks for the advise tho!
Copy linkTweet thisAlerts:
@SodbusterAug 16.2009 — The next time you're wondering if there's a "basic built in php function" to accomplish something, you might want to go to the documentation at php.net, rather than "searching google and the web".
Copy linkTweet thisAlerts:
@welshauthorAug 16.2009 — yea i searched there, generally if i search google for a function 9/10 the first link is the php.net documentation(which i do use often.) however sometimes i dont find it the clearest so i tend to ask here (i always look first before i ask)
×

Success!

Help @welsh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...