<?php
function convertDates($timestamp) {
return date('Y-d-m', $timestamp);
}
$days = array(); //store all the times in this array
$occurences = array(); //count all the occurences for each day
$complete = array(); //fill in missing days in this array
$zero = array(); //missing days mean zero
$query = mysql_query("SELECT `login` FROM `statistics` ORDER BY `login` ASC");
while($rows = mysql_fetch_array($query)) {
$days[] = $rows['login'];
}
$days[] = time(); //append todays time in the array
for($i = 0; $i < count($days); $i++) {
$complete[] = convertDates($days[$i]);
$difference = isset($days[$i+1]) ? $days[$i+1] - $days[$i] : $days[$i] - $days[$i];
if($difference > 86400) {
$difference /= 86400;
$fill = $days[$i];
for($k = 0; $k < $difference; $k++) {
$fill += 86400;
$complete[] = convertDates($fill); //fill in missing days
$zero[] = convertDates($fill); //count this day as a zero
}
echo ceil($difference).' days missing between '.convertDates($days[$i+1]).' and '.convertDates($days[$i]).'<br/>';
}
//echo convertDates($days[$i]).'<br/>';
}
$occurences = array_count_values($complete); //count all duplicates of days, here will be the count of days
$complete = array_unique($complete); //remove duplicate days from array
sort($complete); //sort it again
//print_r($zero);
//print_r($occurences);
/*Here checking the logins for each day, including days that did not exist, but filled in as zero. Note calling them duplicates just means occurrences.*/
for($i=0; $i < count($occurences); $i++) {
if(in_array($complete[$i], $zero)) {
echo "$i [".$complete[$i]."] has 0 duplicates (". $occurences[$complete[$i]] .")<br/>";
} else {
echo "$i [".$complete[$i]."] has ".$occurences[$complete[$i]] . " duplicates<br/>";
}
}
echo "Days with empty days ".count($days)."<br/>";
echo "Days after being filled ".count($complete);
?>
I’m making a graph with jqplot and I ran into a problem with my timestamps. I can convert the timestamps into a day. However the graph does not know when there is non consecutive days that on that day it means zero.
This graph is for login statistics. If there was no logins it would not be in the database. I have to define on the blank days there was zero logins. I made a loop to fill in the blanks that were not in the database.
However I ran into another problem. The days I filled in were counted as logins due to their existence. I tried to make another array called $zero to store these days and check if the day is supposed to be a zero or not. I just couldn’t get it to work.
The values in the database are stored in php time(); I use a function convertDates to turn them into days. From here there are several days, so to count how many logins on that day I used array_count_values() and stored that into another array. Then I removed the duplicate days and paired the day with its respective array_count_values() key. From here I cannot figure out how to make sure on days I filled in, it shows up as a zero.
The jqplot graph doesn’t know there was zero logins. I have to make php say there was zero on that day.
Does anyone have any advice from here to help me get this working?
Thank you for your time.
Don’t compute date/time differences using second math. PHP provides a
DateTimeclass with a convenientDateTime::modifymethod to do this. I would imagine you need to do something like this:After this,
$required_dayswould contain all the dates between earliest and latest date inclusive. Proceed from there as you see fit. It might be more convenient to have an array where keys would be dates and values would be all logins on that date.Note that I used
Y-m-dinstead ofY-d-myou had.Update: detect dates not in database:
As I suggested, you can create an array with each date in the period as a key and an array holding all the logins on that date as a value.
This should fill
$empty_dayswith all dates where there were 0 logins.