[英]How to fetch previous month's data from database in php?
我正在开发一个php网站,需要从上个月获取一些数据。 不仅是前一个月的数据,还包括最近五个月的数据。 我很困惑我该怎么做? 在数据库中,我创建了两列。 一个是“月”,另一个是“年”。 所以行看起来像
id ----文字----月----年
1 ----你好----五月----- 2014
2 ----嗨----五月----- 2014
现在,我可以通过使用date()函数来获取数据。
$first_month = date('F', strtotime('-1 month'));
$second_month = date('F', strtotime('-2 month'));
$third_month = date('F', strtotime('-3 month'));
在五个月内,我可以创建五个变量。 但是一年? 我的意思是,如果当前月份是2014年1月,则我需要从2013年12月开始获取数据,那么它将如何工作? 有人可以建议我吗?
我感到很困惑。 请告诉我以前是否有人做过这样的事情。 我需要想法或概念。
您可以重组表以使用Unix时间戳或MySQL date
,然后使用比较器进行选择。 或者,您可以创建临时表并执行相同的操作。
您要前往的方向对于维护和调试而言并非难事。 另外,正确的查询将比我认为的要复杂。 例如,跨年将是上述的一行。 尽管许多线路都按您的方式运行。
编辑:要维持您当前的逻辑,SQL Select语句必须是一系列WHERE
。 例如:
SELECT * FROM table
WHERE
(month IN ('April', 'May') AND year = 2014)
将选择2014年的4月和5月。
以下内容将持续一年:
SELECT * FROM table
WHERE
(month IN ('December') AND year = 2013)
OR
(month IN ('January') AND year = 2014)
从程序设计的角度来看,您需要生成一系列的年月,包括一年中要选择的所有月份。
这种数组的结构如下所示:
// For the first example
$where1 = array(
2014 => array(
'April',
'May'
)
);
// For the second example
$where2 = array(
2013 => array(
'December'
),
2014 => array(
'January'
)
);
而且您将必须从这些数组创建WHERE
子句。 如:
/**
* 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";
}
这应该够了吧。 有一个工作示例这里 。 收集月份的清单几乎与您一直在做的一样,但是更像这样:
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
有更有效的方法,但这是总的想法。 我相信我已经为您提供了一些有趣的工具来解决您的问题。
要使其达到一定范围,您需要执行以下操作:
$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;
}
您可以在此处查看工作示例: http : //ideone.com/UWI5wI
供参考,以下是使用您的方法与公认准则的示例:
// 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
我使用了第一个(这不是约定),因为每个月都有一个。 您可能希望使用目标月份的最后一天,而不是下个月的第一天,但是您得到了。
跨越很长一段时间后,差异更加明显:
// 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...
MySQL日期格式为YYYY-MM-DD HH:MM:SS
或(获取一个月前的日期格式):
date("Y-m-d H:i:s",strtotime("-1 month"));
您应该在新数据上发布一列“创建的(时间戳/日期时间)”,并简单地搜索这些数据。
通过创建月,日,年列并尝试将其分开,只会使您自己更难。
如果要保留表结构,可以使用以下内容:
$month = date('F', strtotime('-6 month'));
$year = date('Y', strtotime('-6 month'));
会让你十一月和2013年
不确定要存储的数据类型,但另一种选择可能是根据年份命名表,然后每个月都有列
表名称:year_2014
id----m1---m2---m3 etc...
1----hello
2----hi
3----Hey
那么您的查询将是:
$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.