Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get last 7 days using PHP
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/337760/create-an-array-of-the-last-30-days-using-php">Create an Array of the Last 30 Days Using PHP</a> </p> </blockquote> <p>I am trying to create an array with "last 7 days sales", being today plus 6 days previous. I am using this so far:</p> <pre><code>$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 &gt; $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 </code></pre> <p>This will give me an array with the key being the date and the value being the amount of sales for that date. ie:</p> <pre><code>Array ( [Jun 19] =&gt; 19.00 [Jun 20] =&gt; 52.50 [Jun 22] =&gt; 2.00 ) </code></pre> <p>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:</p> <pre><code>Array ( [Jun 19] =&gt; 19.00 [Jun 20] =&gt; 52.50 [Jun 21] =&gt; 0.00 [Jun 22] =&gt; 2.00 [Jun 23] =&gt; 0.00 [Jun 24] =&gt; 0.00 [Jun 25] =&gt; 0.00 ) </code></pre> <p>This way, every day for the last 7 days is in the array, even if the date did not appear in the MySQL query. </p> <p>Any suggestions as to how to do this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload