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.
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.