Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get last 7 days using PHP [duplicate]

Tags:

arrays

php

mysql

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?

like image 477
MultiDev Avatar asked Jun 25 '12 16:06

MultiDev


1 Answers

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.

like image 122
nickb Avatar answered Nov 04 '22 06:11

nickb