如何使用PHP获得最近7天 [英] How to get last 7 days using PHP

查看:963
本文介绍了如何使用PHP获得最近7天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用PHP创建最近30天的数组

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

我正在尝试创建一个具有过去7天销售量"的阵列,即今天和6天之前的销售量.到目前为止,我一直在使用它:

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 ) 

我遇到的问题是,即使那天没有销售,我也需要每天将其添加到阵列中(MySQL查询未找到结果).所以,我很想得到一个像这样的数组:

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 ) 

这样,即使日期没有出现在MySQL查询中,过去7天的每一天都在数组中.

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?

推荐答案

解决方案最可靠的方法是使用DateTime而不是strtotime:

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;
}

将您的数组初始化为类似:

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)
}

现在您有了一个包含过去7天所有可能日期的数组,您可以在循环中执行此操作:

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.

最后,我建议您查看DATETIME或其他关联的日期/时间列类型,因为它们在这里比存储UNIX时间戳更多用.您可能正在使用MySQL日期/时间函数来正确选择要查找的行,而不是每次要基于时间查询数据时都必须创建UNIX时间戳.

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.

这篇关于如何使用PHP获得最近7天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆