2016-03-26 66 views
1

我想保留日期的月數,從日期以dd/mm/yyyy格式輸入的CSV中讀取。 $all是這些日期的數組。我正在繪製一個學年的進度,這個學年從九月開始到七月結束。日期代表學生的成就。我需要的數量是累積的 - 9月份日期也應在隨後各月統計等於是我用它來創建一個谷歌圖表像這樣:保持月份的累計數量

An example graph

我有以下工作代碼這感覺非常麻煩。我覺得我錯過了一些可以更簡潔地解決我的問題的方法。

是否有以這種方式處理日期的常見解決方案?你能建議更廣泛的閱讀嗎?

foreach ($all as $y) { 
    $month = substr($y,3, 2); 

    switch ($month) { 
     case '09': 
      $sep++;$oct++;$nov++;$dec++;$jan++;$feb++;$mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '10': 
      $oct++;$nov++;$dec++;$jan++;$feb++;$mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '11': 
      $nov++;$dec++;$jan++;$feb++;$mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '12': 
      $dec++;$jan++;$feb++;$mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '01': 
      $jan++;$feb++;$mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '02': 
      $feb++;$mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '03': 
      $mar++;$apr++;$may++;$jun++;$jul++; 
      break; 
     case '04': 
      $apr++;$may++;$jun++;$jul++; 
      break; 
     case '05': 
      $may++;$jun++;$jul++; 
      break; 
     case '06': 
      $jun++;$jul++; 
      break; 
     case '07': 
      $jul++; 
      break; 
     default: 
      $jul++; 
    } 

} 

//All months initially set to 1 to help with scaling issues. 
//Work out percentage of all achievements completed 
$NOW = date("n"); 
switch($NOW) { 
    case '9': 
     if ($sep != 1) { 
      $sep = round(($sep-1)/$total*100); 
     } 
     $oct="null";$nov="null";$dec="null";$jan="null";$feb="null";$mar="null";$apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '10': 
     if ($oct != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
     } 
     $nov="null";$dec="null";$jan="null";$feb="null";$mar="null";$apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '11': 
     if ($nov != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
     } 
     $dec="null";$jan="null";$feb="null";$mar="null";$apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '12': 
     if ($dec != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
     } 
      $jan="null";$feb="null";$mar="null";$apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '1': 
     if ($jan != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
     } 
     $feb="null";$mar="null";$apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '2': 
     if ($feb != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
     } 
     $mar="null";$apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '3': 
     if ($mar != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
      $mar = round(($mar-1)/$total*100); 
     } 
     $apr="null";$may="null";$jun="null";$jul="null"; 
     break; 

    case '4': 
     if ($apr != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
      $mar = round(($mar-1)/$total*100); 
      $apr = round(($apr-1)/$total*100); 
     } 
     $may="null";$jun="null";$jul="null"; 
     break; 

    case '5': 
     if ($may != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
      $mar = round(($mar-1)/$total*100); 
      $apr = round(($apr-1)/$total*100); 
      $may = round(($may-1)/$total*100); 
     } 
     $jun="null";$jul="null"; 
     break; 

    case '6': 
     if ($jun != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
      $mar = round(($mar-1)/$total*100); 
      $apr = round(($apr-1)/$total*100); 
      $may = round(($may-1)/$total*100); 
      $jun = round(($jun-1)/$total*100); 
     } 
     $jul="null"; 
     break; 

    case '7': 
     if ($jul != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
      $mar = round(($mar-1)/$total*100); 
      $apr = round(($apr-1)/$total*100); 
      $may = round(($may-1)/$total*100); 
      $jun = round(($jun-1)/$total*100); 
      $jul = round(($jul-1)/$total*100); 
     } 
     break; 

    case '8': 
     if ($jul != 1) { 
      $sep = round(($sep-1)/$total*100); 
      $oct = round(($oct-1)/$total*100); 
      $nov = round(($nov-1)/$total*100); 
      $dec = round(($dec-1)/$total*100); 
      $jan = round(($jan-1)/$total*100); 
      $feb = round(($feb-1)/$total*100); 
      $mar = round(($mar-1)/$total*100); 
      $apr = round(($apr-1)/$total*100); 
      $may = round(($may-1)/$total*100); 
      $jun = round(($jun-1)/$total*100); 
      $jul = round(($jul-1)/$total*100); 
     } 
     break; 

    default: 
     echo " "; 

} 

回答

0

我的建議是不要存儲累積數據,更容易存儲每月的實際成果數。你可以這樣做填充array鍵均爲個月(1〜12):

$counts = array(); 
foreach ($all as $y) { 
    $month = (int)substr($y,3,2); 

    if (isset($counts[$month])) 
    $counts[$month] = 0; 

    $counts[$month]++; 
} 

編輯

現在,你可以簡單地$counts循環正常化每個條目:

$total = array_sum($counts); 
$sum = 0; 

// Avoiding division by zero 
if ($total != 0) { 

    foreach (array_merge(range(9,12),range(1,7)) as $key) { 

    $sum += $counts[$key]; 
    $counts[$key] = round($sum/$total*100); 

    } 

} 
0

下面是一個解決方案,它應該給出相同的結果:

// Work with month numbers relative to school year: 
$cur_month = (3 + date("n")) % 12; // 0 = september, 1 = october, ... 

// Collect monthly statistics (August excluded) 
$stats = array_fill(0, 11, 0); 
foreach ($all as $date) { 
    // Extract month and transpose to school month: 0 = september, ... 
    $month = (3+substr($date,3, 2)) % 12; 
    // August is mapped to July 
    $month = min(11, $month); 
    // Only data in the past or current month is taken into account: 
    if ($month <= $cur_month) $stats[$month]++; 
} 

// Cumulate statistics: 
$total = 0; 
foreach ($stats as $month => &$stat) { 
    $total += $stat; 
    // Avoid zeroes for scaling problems 
    $stat = max(1, $total); 
} 

// Don't allow total == 0 
$total = max(1, $total); 

// Convert to percentages, and clear future months 
foreach ($stats as $month => &$stat) { 
    $stat = $month <= $cur_month 
     ? round($stat/$total * 100) 
     : 'null'; 
} 

// Assign to individual variables, if needed 
list($sep, $oct, $nov, $dec, $jan, $feb, 
    $mar, $apr, $may, $jun, $jul) = $stats;