可能重複:
Create an Array of the Last 30 Days Using PHP如何獲得最近7天內使用PHP
我試圖創建「最近7天銷售」的陣列,是當今加上6天以前。我使用到目前爲止:
$rightnow = time();
$time_window = $rightnow - (60*60*24*6); // 6 days ago + today = 7 days
$tw_time = date('M d', $time_window);
$tw_time = strtotime($tw_time); // 6 days ago starting at 00:00:00
$valid_sales = mysql_query("SELECT amt, created FROM sales WHERE created > $tw_time");
$sale_data = array();
foreach ($valid_sales as $sale) {
$display_date = date('M d', $sale['created']);
if (array_key_exists($display_date,$sale_data)) { // If date is in array
$sale_data[$display_date] = $sale_data[$display_date] + $sale['amt']; // Add amount to date's sales
} else { // If date is not in array
$sale_data[$display_date] = $sale['amt']; // Create key with this amount
}
} // End foreach valid_sales
這將給我一個數組,其中的日期和價值是該日期的銷售量。即:
Array ([Jun 19] => 19.00 [Jun 20] => 52.50 [Jun 22] => 2.00)
我遇到的問題是,我需要每天補充到,即使存在沒有銷售的那一天(無結果發現與MySQL查詢)的陣列。所以,我特林得到一個數組是這樣的:
Array ([Jun 19] => 19.00 [Jun 20] => 52.50 [Jun 21] => 0.00 [Jun 22] => 2.00 [Jun 23] => 0.00 [Jun 24] => 0.00 [Jun 25] => 0.00)
這樣一來,每天都過去7天的陣中,即使日期沒有出現在MySQL查詢。
有關如何做到這一點的任何建議?
您是否正在使用mysql數據庫中的unixtimetamps? O_o –