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 8140357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:08:12+00:00 2026-06-06T12:08:12+00:00

Possible Duplicate: Create an Array of the Last 30 Days Using PHP I am

  • 0

Possible Duplicate:
Create an Array of the Last 30 Days Using PHP

I am trying to create an array with “last 7 days sales”, being today plus 6 days previous. I am using this so far:

$rightnow = time(); 
$time_window = $rightnow - (60*60*24*6); // 6 days ago + today = 7 days

$tw_time = date('M d', $time_window);
$tw_time = strtotime($tw_time); // 6 days ago starting at 00:00:00

$valid_sales = mysql_query("SELECT amt, created FROM sales WHERE created > $tw_time");

$sale_data = array();

foreach ($valid_sales as $sale) {

    $display_date = date('M d', $sale['created']);

    if (array_key_exists($display_date,$sale_data)) { // If date is in array

        $sale_data[$display_date] = $sale_data[$display_date] + $sale['amt']; // Add amount to date's sales

    } else { // If date is not in array

        $sale_data[$display_date] = $sale['amt']; // Create key with this amount

    }

} // End foreach valid_sales

This will give me an array with the key being the date and the value being the amount of sales for that date. ie:

Array ( [Jun 19] => 19.00 [Jun 20] => 52.50 [Jun 22] => 2.00 ) 

The problem I am having is that I need to add each day onto the array even if no sales existed for that day (no results were found with the MySQL query). So, I am tring to get an array like this:

Array ( [Jun 19] => 19.00 [Jun 20] => 52.50 [Jun 21] => 0.00 [Jun 22] => 2.00 [Jun 23] => 0.00 [Jun 24] => 0.00 [Jun 25] => 0.00 ) 

This way, every day for the last 7 days is in the array, even if the date did not appear in the MySQL query.

Any suggestions as to how to do this?

  • 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-06T12:08:13+00:00Added an answer on June 6, 2026 at 12:08 pm

    The most robust way to go about this is to use DateTime instead of strtotime:

    $now = new DateTime( "7 days ago", new DateTimeZone('America/New_York'));
    $interval = new DateInterval( 'P1D'); // 1 Day interval
    $period = new DatePeriod( $now, $interval, 7); // 7 Days
    

    Now, you can form your array of dates like so:

    $sale_data = array();
    foreach( $period as $day) {
        $key = $day->format( 'M d');
        $sale_data[ $key ] = 0;
    }
    

    This initializes your array to something like:

    array(8) {
     ["Jun 18"]=>      int(0)
      ["Jun 19"]=>      int(0)
      ["Jun 20"]=>      int(0)
      ["Jun 21"]=>      int(0)
      ["Jun 22"]=>      int(0)
      ["Jun 23"]=>      int(0)
      ["Jun 24"]=>      int(0)
      ["Jun 25"]=>      int(0)
    }
    

    Now you have an array with all of the possible dates in the past 7 days, and you can do this in your loop:

    $display_date = date('M d', $sale['created']);
    $sale_data[$display_date] += $sale['amt'];
    

    You do not need to check if the array key exists, as it is guaranteed to exist.

    Finally, I would recommend looking into the DATETIME or other associated date/time column types, as they would be of more use here than storing UNIX timestamps. You could be using MySQL date/time functions to properly select the rows you’re looking for instead of having to create a UNIX timestamp every time you want to query for data based on time.

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

Sidebar

Related Questions

Possible Duplicate: PHP : Create array for JSON I have an array like this
Possible Duplicate: How to create comma separated list from array in PHP? Given this
Possible Duplicate: How to create comma separated list from array in PHP? My array
Possible Duplicate: How to create comma separated list from array in PHP? I have
Possible Duplicate: Multiple index variables in PHP foreach loop I am trying to create
Possible Duplicate: How to create an empty array in PHP with predefined size? In
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: What’s the reason I can’t create generic array types in Java? HashSet<Integer>[]
Possible Duplicate: Grouping WHERE clauses with Zend_Db_Table_Abstract I need to create something like this:
Possible Duplicate: Dynamically Growing an Array in C++ I want to create a loop

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.