简体   繁体   English

如何从PHP中的数据库中获取上个月的数据?

[英]How to fetch previous month's data from database in php?

I am developing a php website and need to fetch some data from previous month. 我正在开发一个php网站,需要从上个月获取一些数据。 Not only first previous month but last five months data. 不仅是前一个月的数据,还包括最近五个月的数据。 And I am confused that how can I do this? 我很困惑我该怎么做? In my database I've created two columns. 在数据库中,我创建了两列。 One is "Month" and another is "Year". 一个是“月”,另一个是“年”。 So row will look like 所以行看起来像


id ---- text ---- month ---- year id ----文字----月----年

1 ---- hello ---- May ----- 2014 1 ----你好----五月----- 2014

2 ---- hi ---- May ----- 2014 2 ----嗨----五月----- 2014


Now I can fetch data by using date() function like this. 现在,我可以通过使用date()函数来获取数据。

$first_month = date('F', strtotime('-1 month'));
$second_month = date('F', strtotime('-2 month'));
$third_month = date('F', strtotime('-3 month'));

for five months, I can create five variables. 在五个月内,我可以创建五个变量。 But for year? 但是一年? I mean if current month is january and year 2014, then I will need to fetch data from December 2013, so how it will work? 我的意思是,如果当前月份是2014年1月,则我需要从2013年12月开始获取数据,那么它将如何工作? Can anyone suggest me? 有人可以建议我吗?

I am so confused. 我感到很困惑。 Please tell me if anyone has done something like this before. 请告诉我以前是否有人做过这样的事情。 I need idea or concept. 我需要想法或概念。

You could either restructure your tables to use a Unix timestamp or MySQL date , and select using comparator's. 您可以重组表以使用Unix时间戳或MySQL date ,然后使用比较器进行选择。 Or you could create temporary tables and do the same. 或者,您可以创建临时表并执行相同的操作。

The direction you are heading will be nontrivial to maintain and debug. 您要前往的方向对于维护和调试而言并非难事。 Also, the correct queries will be more complex than I believe you expect them to be. 另外,正确的查询将比我认为的要复杂。 For instance, spanning years will be one line with the above. 例如,跨年将是上述的一行。 While many many lines going the way you are. 尽管许多线路都按您的方式运行。

Edit: To maintain your current logic, the SQL Select statement would have to be a series of WHERE s. 编辑:要维持您当前的逻辑,SQL Select语句必须是一系列WHERE For instance: 例如:

SELECT * FROM table
WHERE
    (month IN ('April', 'May') AND year = 2014)

Would select the months of April and May in the year 2014. 将选择2014年的4月和5月。

The following would span a year: 以下内容将持续一年:

SELECT * FROM table
WHERE
    (month IN ('December') AND year = 2013)
OR
    (month IN ('January') AND year = 2014)

From a programmatic standpoint, you would need to generate an array of years and months, including all months of the year you would like to select from. 从程序设计的角度来看,您需要生成一系列的年月,包括一年中要选择的所有月份。

The structure for such an array would look like such: 这种数组的结构如下所示:

// For the first example
$where1 = array(
    2014 => array(
        'April',
        'May'
    )
);

// For the second example
$where2 = array(
    2013 => array(
        'December'
    ),
    2014 => array(
        'January'
    )
);

And you would have to create a WHERE clause from those arrays. 而且您将必须从这些数组创建WHERE子句。 Such as: 如:

/**
 * Outputs
 * [where1] (month IN ('April', 'May') AND year = 2014)
 *
 * [where2] (month IN ('December') AND year = 2013) OR (month IN ('January') AND year = 2014)
 */
foreach(array('where1','where1') as $where_name) {

    $clause = '';

    $where = $$where_name;

    $wheres = array();

    foreach($where as $year => $months) {
        $wheres[] = "month IN ('" . implode("', '", $months) . "') AND year = $year";
    }

    $clause = '('.implode (') OR (', $wheres).')';

    echo "[$where_name] $clause\n\n";
}

That should do the trick. 这应该够了吧。 There is a working example here . 有一个工作示例这里 Gathering the list of months is almost the same as you have been doing, but moreso like so: 收集月份的清单几乎与您一直在做的一样,但是更像这样:

    list($month, $year) = explode(" ", date('F Y', strtotime('-3 month')));

var_dump($month); // February
var_dump($year); // 2014

$array[$year][] = $month; // Will create the array structure as above

There are more efficient ways, but this is the general idea. 有更有效的方法,但这是总的想法。 I believe I have left you with some interesting tools to solve your problem. 我相信我已经为您提供了一些有趣的工具来解决您的问题。

To make this hit a range you would need to do something along the lines of: 要使其达到一定范围,您需要执行以下操作:

$where3 = array();

for ($prev_months = 1; $prev_months <= 5; $prev_months++) {
    list($month, $year) = explode(" ", date('F Y', strtotime("-$prev_months month")));
    $where3[$year][] = $month;
}

You can view the working example here: http://ideone.com/UWI5wI 您可以在此处查看工作示例: http : //ideone.com/UWI5wI

For reference, here are the examples of using your methodology vs the accepted norm: 供参考,以下是使用您的方法与公认准则的示例:

// your last and 5
(month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December') AND year = 2013)

// native 1 and 5
date <= 2014-05-01 01:00:00 AND date > 2013-12-01 01:00:00

I used the first (which is not convention) because every month has a first. 我使用了第一个(这不是约定),因为每个月都有一个。 You would want to use the last day of the target month, instead of the first day of the following month, but you get that. 您可能希望使用目标月份的最后一天,而不是下个月的第一天,但​​是您得到了。

The difference is much more apparent when spanning LONG periods of time: 跨越很长一段时间后,差异更加明显:

// 2 years, yours
(month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April', 'March', 'February', 'January') AND year = 2013) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May') AND year = 2012)

// The conventional way is still just two comparators...

the MySQL date format is YYYY-MM-DD HH:MM:SS MySQL日期格式为YYYY-MM-DD HH:MM:SS

or (to get the date format for one month ago): 或(获取一个月前的日期格式):

date("Y-m-d H:i:s",strtotime("-1 month"));

You should be issuing a created (timestamp/datetime) column on new data, and simply searching through this data. 您应该在新数据上发布一列“创建的(时间戳/日期时间)”,并简单地搜索这些数据。

By creating month, day, year columns and trying to separate it you have only made this harder on yourself. 通过创建月,日,年列并尝试将其分开,只会使您自己更难。

If you want to keep your table structure you can use what you have as this: 如果要保留表结构,可以使用以下内容:

$month = date('F', strtotime('-6 month'));
$year = date('Y', strtotime('-6 month'));

Will get you November and 2013 会让你十一月和2013年

Not sure what type of data you are storing but another option may be to name your tables based on the year and then have columns for each month 不确定要存储的数据类型,但另一种选择可能是根据年份命名表,然后每个月都有列

table name: year_2014 表名称:year_2014

 id----m1---m2---m3 etc...
 1----hello
 2----hi
 3----Hey

Then your query would be: 那么您的查询将是:

$month = date("n", strtotime('-6 month'));
$year = date('Y', strtotime('-6 month'));
$monthval = "m" + $month;
$SQL = "SELECT " . $monthval . " FROM year_".$year;

SELECT m11 FROM year_2013

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM