簡體   English   中英

從給定的mysql結果獲取月數

[英]Get count of months from given mysql result

我試圖獲取按月注冊的用戶數量,以將其插入到chart.js中。我遇到的問題是每月顯示計數結果。

我從mysql查詢中得到以下結果

php print_r($this->results);

結果

Array (
  [0] => Array
    (
        [d] => 2018-05-15 21:54:08
    )

  [1] => Array
    (
        [d] => 2018-05-16 15:50:58
    )

  [2] => Array
    (
        [d] => 2018-06-18 18:27:11
    )

)

答案應該是:05 = 2和06 = 1

謝謝您的幫助!

$data = [];
$data[0]['d'] = '2018-05-15 21:54:08';
$data[1]['d'] = '2018-05-16 15:50:58';
$data[2]['d'] = '2018-06-18 18:27:11';

$result = [];
foreach( $data as $element ) {
    // convert date to timestramp by strToTime
    // get month via date( 'm' )
    $month = date( 'm', strToTime( $element['d'] ) );

    // if a count for month is present increment it
    if( isset( $result[$month] )) {
        $result[$month]++;
    } else { // if NO count is present, create and set to 1
        $result[$month] = 1;
    }
}

echo '<pre>';
var_dump($result);

結果

array(2) {
  ["05"]=>
  int(2)
  ["06"]=>
  int(1)
}

編輯

補充說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM