我正在开发一个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年,那我就需要从2013年12月获取数据,所以它是如何工作的? 任何人都可以给我建议?
我感到很困惑。 请告诉我,如果一个人做过这样的事情。 我需要的想法或概念。
您既可以调整你的表是使用Unix时间戳或MySQL的date
,并选择使用比较的。 或者你可以创建临时表,做同样的。
你前进的方向将是平凡的维护和调试。 此外,正确的查询会更复杂,比我相信你期望的那样。 例如,跨越年将是与上面一行。 虽然很多很多行去你的方式。
编辑:要维持目前的逻辑,SQL SELECT语句必须是一系列的WHERE
秒。 例如:
SELECT * FROM table
WHERE
(month IN ('April', 'May') AND year = 2014)
将在2014年选择四月和五月的几个月。
下面将跨越一年:
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";
}
这应该够了吧。 有一个工作示例这里 。 收集几个月的名单几乎是一样的,你一直在做,但moreso像这样:
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
作为参考,在这里是用你的方法VS公认的准则的例子:
// 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