按周排序数组中的信息

时间:2012-05-15 19:06:16

标签: php arrays datetime sorting

我有一系列日期,比如

array
1 => string '2012-02-27' (length=10)
2 => string '2012-03-02' (length=10)
59 => string '2012-03-05' (length=10)

等等。这些日期代表人们申请某些东西的日子。例如,2月27日,3人申请并于3月1日申请。

我想要做的是创建一个表格,该表格在2012-02-262012-03-03周提交了一份申请表。左侧列的标题为Weeks,右侧列的标题为# of applications received

我已经在白天设置了表格,但周也很有帮助。我曾看过一篇关于列出周刊的帖子,并尝试使用它,但它没有用。

更具体地说,我的问题是我如何处理一系列日期并将其分解几周并将这些信息放在表格中?

2 个答案:

答案 0 :(得分:0)

您没有指定使用的数据库引擎,但大多数都有日期格式字段“一年中的一周”,因此SELECT COUNT, YEAR(date), WEEKOFYEAR(date) FROM stuff GROUP BY YEAR(date), WEEKOFYEAR(date)

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_weekofyear

答案 1 :(得分:0)

你可能想在PHP中使用它,而不是数据库。

1) convert dates to epoch (seconds) with strtotime()
2) sort array by (numeric now) date with asort()
3) loop through the sorted now array from start date to end date and add the values.

当然,使用数据库可以更轻松地完成。