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

  • SEARCH
  • Home
  • 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 7027609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:16:13+00:00 2026-05-28T00:16:13+00:00

I have 2 dates. Lets say they look like this. $start = 2010/12/24; $end

  • 0

I have 2 dates. Lets say they look like this.

$start = 2010/12/24;
$end = 2012/01/05;

I query the database to look for visits between these two dates. I find some. I then populate an array called stats.

$stats['2010/12/25'] = 50;
$stats['2010/12/31'] = 25;
...

As you can see, there are days missing. I need to fill the missing dates with a value of zero. I was thinking something like this. (I have pulled day / month / year from start and end dates.

for($y=$start_year; $y <= $end_year; $y++) {
    for($m=$start_month; $m <=$end_month; $m++) {
        for($d=$start_day; $d <= $end_day; $d++) {

This would work fine for the year however the months and days wouldn’t work. If the start day is the 15th. Days 1-14 of each subsequent month would be missed. I could have a solution like this then…

for($y=$start_year; $y <= $end_year; $y++) {
    for($m=1; $m <13; $m++) {
         $total_days = cal_days_in_month(CAL_GREGORIAN, $m, $y) + 1;
         for($d=1; $d <= $total_days; $d++) {

I would then need a bunch of if statements making sure starting and end months and days are valid.

Is there a better way of doing this? Or could this even be done in my mysql query?

  • 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-05-28T00:16:13+00:00Added an answer on May 28, 2026 at 12:16 am

    Just to demonstrate the power of some of PHP’s newer interval handling method (mentioned by pgl in his answer):

    $startDate = DateTime::createFromFormat("Y/m/d","2010/12/24",new DateTimeZone("Europe/London"));
    $endDate = DateTime::createFromFormat("Y/m/d","2012/01/05",new DateTimeZone("Europe/London"));
    
    $periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
    $period = new DatePeriod( $startDate, $periodInterval, $endDate );
    
    foreach($period as $date){
       echo $date->format("Y-m-d") , PHP_EOL;
    }
    

    Does require PHP >= 5.3.0

    EDIT

    If you need to include the actual end date, then you need to add a day to $endDate immediately before the foreach() loop:

    $endDate->add( $periodInterval );
    

    EDIT #2

    $startDate = new DateTime("2010/12/24",new DateTimeZone("Europe/London"));
    $endDate = new DateTime("2012/01/05",new DateTimeZone("Europe/London"));
    
    do {
       echo $startDate->format("Y-m-d") , PHP_EOL;
       $startDate->modify("+1 day");
    } while ($startDate <= $endDate);
    

    For PHP 5.2.0 (or earlier if dateTime objects are enabled)

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

Sidebar

Related Questions

Lets say I have a table with 20 entries. They are sorted by date
When a user is buying a product (lets say they have 3 products) and
I have an array of dates whose format is like this: 10/15/2005 or 2/10/2011.
Let's say I have a queryset of things Bill has worked on like this:
Lets say I have some simple classes for a twitter-like app: User Post A
Lets say I have this SQL Table id | name | date 1 |
I have an array of items that all have start and end dates associated
Lets say I have a table: -------------------------------------- | ID | DATE | GROUP |
Let's say I have the following database table: record_id | record_date | record_value -----------+-------------+--------------
Lets say I have a SQL Server with 100 databases on it. How can

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.