SELECT COUNT(*) AS count, state, DATE_FORMAT( '%m/%d', registered ) AS date_formatted
FROM users
WHERE CAST( registered AS DATE ) > DATE_SUB( CURDATE(), INTERVAL 7 DAY)
GROUP BY state, date_formatted
以上是我们用于从数据库返回信息的查询。
我们的日期保存为:2011-03-28 14:36:48
(日期时间字段)
返回的数据如下:
Array
(
[] => Array
(
[act] => 1
[nsw] => 1
[nt] => 1
[qld] => 3
[sa] => 1
[tas] => 1
[vic] => 1
[wa] => 4
)
)
并没有在数组中返回日期。
while( $row = mysql_fetch_array( $query )) { $my_array[$row['date_formatted']][$row['state']] = $row['count']; }
这是我们用来构成数组的php代码。
谢谢:)
答案 0 :(得分:3)
DATE_FORMAT
将日期作为第一个参数,格式作为第二个参数:
SELECT COUNT(*) AS count, state, DATE_FORMAT(registered ,'%m/%d')
...