Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most robust way to go about this is to use <code>DateTime</code> instead of <code>strtotime</code>:</p> <pre><code>$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 </code></pre> <p>Now, you can form your array of dates like so:</p> <pre><code>$sale_data = array(); foreach( $period as $day) { $key = $day-&gt;format( 'M d'); $sale_data[ $key ] = 0; } </code></pre> <p>This <a href="http://codepad.viper-7.com/MDpsY8">initializes your array</a> to something like:</p> <pre><code>array(8) { ["Jun 18"]=&gt; int(0) ["Jun 19"]=&gt; int(0) ["Jun 20"]=&gt; int(0) ["Jun 21"]=&gt; int(0) ["Jun 22"]=&gt; int(0) ["Jun 23"]=&gt; int(0) ["Jun 24"]=&gt; int(0) ["Jun 25"]=&gt; int(0) } </code></pre> <p>Now you have an array with all of the possible dates in the past 7 days, and you can do this in your loop:</p> <pre><code>$display_date = date('M d', $sale['created']); $sale_data[$display_date] += $sale['amt']; </code></pre> <p>You do not need to check if the array key exists, as it is guaranteed to exist.</p> <p>Finally, I would recommend looking into the <code>DATETIME</code> 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.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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