Reputation: 124
I'm trying to get the average time a user spends on my site using my own built analytics.
I'm using the DateTime class right now but the math seems very sketchy. Say I have an array of login times and logout times.
$array = array(
array("login" => '2012-01-31 10:35:58', "logout" => '2012-02-01 10:35:58'),
array("login" => '2012-02-04 10:35:58', "logout" => '2012-02-05 10:35:58')
);
I want to get the of amount of time between each login and logout, then find the average of all those times.
Upvotes: 1
Views: 3942
Reputation: 739
$array = array( array ("login" => "2012-01-31 10:35:58", "logout" => "2012-02-01 10:35:58"),
array ("login" => "2012-02-04 10:35:58", "logout" => "2012-02-05 10:35:58") );
$amount = 0 ;
foreach( $array as $at )
{
$amount += strtotime( $at[ "logout" ] ) - strtotime( $at[ "login" ] ) ;
}
$average = $amount / count( $array ) ;
echo "amount: $amount seconds\naverage $average seconds\n" ;
Upvotes: 1
Reputation: 23061
Example using DateTime
(tested).
define('DATETIME_FORMAT', 'Y-m-d H:i:s');
$array = array(
array('login' => '2012-01-31 10:35:58', 'logout' => '2012-02-01 10:35:58'),
array('login' => '2012-02-04 10:35:58', 'logout' => '2012-02-06 10:22:58')
);
$total = 0;
$count = 0;
foreach($array as $timeInfo)
{
$loginDatetime = DateTime::createFromFormat(DATETIME_FORMAT, $timeInfo['login']);
$logoutDatetime = DateTime::createFromFormat(DATETIME_FORMAT, $timeInfo['logout']);
$total += ($logoutDatetime->getTimestamp() - $loginDatetime->getTimestamp());
$count++;
}
$average = $total / $count;
echo "Average session duration: ".$average." seconds";
EDIT: initally I've used an array to store diffs, ending with array_sum() / count()
to compute the average, but I found Marc B's solution with $total
and $count
simpler and certainly faster (it may matter here since potentially a lot of login/logout datetimes will be processed). => applied it here.
Upvotes: 2
Reputation: 488454
You could do something like:
$sessions = array();
foreach($array as $s) {
$sessions[] = strtotime($s['logout']) - strtotime($s['login']);
}
Now $sessions
is an array of all the session lengths in seconds, so if you then do:
$average = array_sum($sessions) / count($sessions);
That is the average session length, in seconds. You could then print that in human-readable format, but I think that's beyond the scope of this question.
The reason I am putting the session lengths in an array first instead of simply summing them up in the loop is that you can then also get other statistics out of it, like longest/shortest sessions, median, etc.
Upvotes: 3
Reputation: 360732
$total = 0;
$count = 0;
foreach ($array as $timestamp) {
$diff = strtotime($timestamp['logout']) - strtotime($timestamp['login']);
$total += $diff;
$count++;
}
echo "Average session time is ", $total / $count;
For safety's sake, you'd be better off using DateTime::createFromFormat()
to do the date->time parsing. Your timestamps are a nice normal format, but strtotime is unreliable when you've got some wonky formats.
As well, this code assumes that all login/logout pairs are fully defined. If you have any where either time is off, you'll end up with some huge outliers as those'll most likely come out as 0
, rather than a normal "modern" timestamp.
Upvotes: 7