I have a database full of php time(); stored when a user logs in.
I know I should have created a value for their username and updated the login count but instead I have a database full of php time(); with their username
Here is how I find the amount of logins for individual users:
<?php
function to_date($timestamp) {
return date('Y-m-d', $timestamp);
}
$days = array();
$occurences = array();
$zero = array();
$query = mysql_query("SELECT `login_time` FROM `stats` WHERE `userid`='$someuserid' ORDER BY `login_time` ASC");
while($rows = mysql_fetch_array($query)) {
$days[] = to_date($rows['login_time']);
}
$days[] = to_date(time());
$occurences = array_count_values($days);
$days = array_unique($days);
sort($days);
for($i = 0; $i < count($days); $i++) {
$difference = isset($days[$i+1]) ? strtotime($days[$i+1]) - strtotime($days[$i]) : 0;
if($difference > 86400) {
$difference = ceil(abs($difference/86400));
$fill = $days[$i];
for($k = 0; $k < $difference-1; $k++) {
$fill += strtotime('+1 day', strtotime($fill));
$zero[] = to_date($fill);
}
}
}
echo "[";
for($i = 0; $i < count($zero); $i++) {
echo "[\"".$zero[$i]."\",0], ";
}
for($i = 0; $i < count($occurences); $i++) {
if($i == count($occurences)-1)
echo "[\"".$days[$i]."\",".($occurences[$days[$i]]-1)."]";
else {
echo "[\"".$days[$i]."\",".$occurences[$days[$i]]."], ";
}
}
echo "]";
?>
How could I apply this to all the users and find out who logged in the most for each day in the database? The output is encoded for jqplot.
Use the above query to get all the required data directly from the database and this will help you avoid doing the post processing using PHP
Additionally, if you would like to get only one record, which have maximum logins in a (any) day, the use the below query with an added SQL wrapper for the above query:
Let me know if this helped you.