Reputation: 3042
function convertDates($timestamp) {
return date('Y-m-d', $timestamp);
}
$days = array(); //storing databases php time();
$complete = array(); //storing the generated missing days
$occurrences = array(); //finding how many php time() are on same day
$zero = array(); //instead of storing occurrence as 1, store it as zero
$query = mysql_query("SELECT `login` FROM `statistics` ORDER BY `login` ASC");
while($rows = mysql_fetch_array($query)) {
$days[] = $rows['login']; //filling array with times
}
$days[] = time(); //append current time
for($i = 0; $i < count($days); $i++) {
$complete[] = convertDates($days[$i]);
$difference = isset($days[$i+1]) ? $days[$i+1] - $days[$i] : 0;
if($difference >= 86400) {
$difference = ceil(abs($difference/86400));
$fill = $days[$i];
for($k = 0; $k < $difference; $k++) {
$fill += 86400;
$complete[] = convertDates($fill);
$zero[] = convertDates($fill);
}
}
}
$occurrences = array_count_values($complete);
$complete = array_unique($complete);
$zero = array_unique($zero);
sort($complete);
echo "[";
for($i = 0; $i < count($zero); $i++) {
echo "[\"".$zero[$i]."\",0], ";
}
for($i = 0; $i < count($occurrences); $i++) {
if($i == count($occurrences)-1)
echo "[\"".$complete[$i]."\",".$occurrences[$complete[$i]]."]";
else {
echo "[\"".$complete[$i]."\",".$occurrences[$complete[$i]]."], ";
}
}
echo "]";
I'm plotting points into a jqplot graph and for the statistics I need to define days the user did not login. Lets call these "gaps"
The user logs in on March 25, 2012 and does not log back in for 5 days. It is now March 30, 2012. I have to generate those missing days from the two dates.
My current algorithm doesn't seem to be working. I can't figure out what is going wrong.
The dates in the database are from php time();
I stored all the values into an array $days
Here is the output from the logins table generated by the script. Note it is encoded for the jqplot:
[["2012-04-01",0], ["2012-04-02",0], ["2012-04-03",0], ["2012-04-04",0], ["2012-04-05",0], ["2012-04-07",0], ["2012-04-08",0], ["2012-04-09",0], ["2012-04-10",0], ["2012-04-11",0], ["2012-04-12",0], ["2012-04-13",0], ["2012-04-14",0], ["2012-04-15",0], ["2012-04-16",0], ["2012-04-17",0], ["2012-04-18",0], ["2012-04-19",0], ["2012-04-20",0], ["2012-04-21",0], ["2012-04-22",0], ["2012-04-23",0], ["2012-04-24",0], ["2012-04-25",0], ["2012-04-26",0], ["2012-04-27",0], ["2012-04-28",0], ["2012-04-29",0], ["2012-04-30",0], ["2012-03-31",1], ["2012-04-01",2], ["2012-04-02",2], ["2012-04-03",1], ["2012-04-04",19], ["2012-04-05",13], ["2012-04-06",8], ["2012-04-07",1], ["2012-04-08",1], ["2012-04-09",4], ["2012-04-10",1], ["2012-04-11",1], ["2012-04-12",2], ["2012-04-13",1], ["2012-04-14",1], ["2012-04-15",1], ["2012-04-16",2], ["2012-04-17",10], ["2012-04-18",1], ["2012-04-19",1], ["2012-04-20",1], ["2012-04-21",1], ["2012-04-22",1], ["2012-04-23",1], ["2012-04-24",1], ["2012-04-25",1], ["2012-04-26",1], ["2012-04-27",1], ["2012-04-28",1], ["2012-04-29",1], ["2012-04-30",2]]
My script is not working. How can I properly generate the days where the user did not login?
Thank you for your time.
Upvotes: 0
Views: 868
Reputation: 7019
Use a loop to go through successive dates, using a function like:
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
For each cycle, apply your query result. Then you'll have all the dates. You'll still be using a loop; but the basis will be day progression, not your query result.
Pseudo-code:
for ($day_counter = 0; $day_counter < 365; $day_counter++) {
$day_this = strtotime(date("Y-m-d", strtotime($day_this)) . " +1 day");
$login_count = mysql_query("SELECT COUNT(DATE(login)) AS date_login
FROM statistics
WHERE date_login=".$day_this);
array_push($days, array($day_this=>$login_count);
}
Not sure how this part should look:
COUNT(DATE(login))
... but the idea is that you convert a date-time value to a simple date, then run a count.
CORRECTION:
COUNT(DATE(login)) is wrong. You must apply the COUNT() function to a distinct record field (usually a primary key), and use the function that collapses date/time into a simple date in the WHERE clause. I often start with what I wrote above and then figure this out. :-)
Upvotes: 1