Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:03:20+00:00 2026-06-02T22:03:20+00:00

<?php function convertDates($timestamp) { return date(‘Y-d-m’, $timestamp); } $days = array(); //store all the

  • 0
<?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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T22:03:21+00:00Added an answer on June 2, 2026 at 10:03 pm

    Don’t compute date/time differences using second math. PHP provides a DateTime class with a convenient DateTime::modify method to do this. I would imagine you need to do something like this:

    for($dt = $earliest_date; $dt <= $latest_date; $dt->modify('+1 day')) {
      $required_days []= $dt->format('Y-m-d');
    }
    

    After this, $required_days would 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-d instead of Y-d-m you 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.

    $logins_by_date = array();
    foreach($required_days as $date) {
      $logins_by_date[$date] = array();
    }
    
    foreach($days as $login_datetime) {
      $dt = new DateTime($login_datetime);
      $key = $dt->format('Y-m-d');
      if(array_key_exists($key, $logins_by_date)) {
        $logins_by_date[$key] []= $login_datetime;
      } else {
        throw new Exception("Logical error - date {$key} does not exist in the login date map!");
      }
    }
    
    $empty_days = array();
    
    foreach($logins_by_date as $date => $logins) {
      if(count($logins) == 0) {
        $empty_days []= $date;
      }
    }
    

    This should fill $empty_days with all dates where there were 0 logins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function convertDates($timestamp) { return date('Y-m-d', $timestamp); } $days = array(); //storing databases php time();
I have a php function to add number of days to input date function
I have a PHP function that returns an array. This is the output of
Which php array function should be used to match a string to the first
i've made this php function to resize picture , and i want to save
We use very simple php function to display time and date. <div class=datedata><?=date(l, F
I have created a php function to return time in digital clock format. My
I am using php function usort to sort an array. The custom php function
This php function retrieves a list of common words used in a string and
I have a PHP function that returns something: function myfunction() { $array = array('one',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.