I'm trying to count visitor dates to see how many visits there are per month & post the result into a jquery chart.
Ok so this code below "works", but it somehow counts everything * 2. If there is 1 visitor this month it will output 2, why is that?
I would also like to know how can I make this code smaller? $jan $feb etc. seems not the way this should be done, but I'm a beginner so I don't really know how this code can be made smaller & better. Can somebody help me with this?
Database:
date
2016-11-17 16:36:12
Php:
$jan = ''; $feb = ''; $maa = ''; $apr = ''; $mei = ''; $jun = ''; $jul = ''; $aug = ''; $sep = ''; $okt = ''; $nov = ''; $dec = '';
foreach($dates as $date){
$month = date_parse_from_format("Y-m-d H:i:s", $date->date);
if($month ["month"] == '01'){$jan .= $month ["month"];}
if($month ["month"] == '02'){$feb .= $month ["month"];}
if($month ["month"] == '03'){$maa .= $month ["month"];}
if($month ["month"] == '04'){$apr .= $month ["month"];}
if($maand["month"] == '05'){$mei .= $month ["month"];}
if($maand["month"] == '06'){$jun .= $month ["month"];}
if($maand["month"] == '07'){$jul .= $month ["month"];}
if($maand["month"] == '08'){$aug .= $month ["month"];}
if($maand["month"] == '08'){$sep .= $month ["month"];}
if($maand["month "] == '10'){$okt .= $month ["month"];}
if($maand["month "] == '11'){$nov .= $month ["month"];}
if($maand["month "] == '12'){$dec .= $month ["month"];}
}
try this:
$arr = array()
foreach($dates as $date){
$month = date('m',strtotime($date->date));
$arr[$month][] = $date;
}
Here this code creates an array an takes month as key and append data for that month as values.
$visitsPerMonth = [];
foreach ($dates as $date) {
$month = date('m', strtotime($date->date));
$visitsPerMonth[$month]++;
}
If $dates is originating in a SQL database, instead use a GROUP BY query.
Use json_encode() on $visitsPerMonth if your jQuery chart expects JSON input.
As per you requirements if you are storing visits data in database then you can get the count from database query as per month like this
SELECT COUNT(*) FROM visitors YEAR(visited_date) = '2016' GROUP BY MONTH(visited_date)
If there is 1 visitor this month it will output 2, why is that?
This is because you are concatenating the string and counting the length of string. The month is of 2 characters so every time it iterates it concatenated 2 characters per iteration. See the example bellow
Case 1: $data has 1 date 2016-11-17 16:36:12
$nov = ''; // at start
$nov .= $month ["month"]; // date 2016-11-17 16:36:12
=> $nov .= '11'; //shorthand concatenation php
=> $nov = $nov . '11';
=> $nov = '' . '11'; // as $nov = '' at start
count($nov); // $nov have value '11' which has 2 character so output will be 2
Case 2: $data has 2 dates 2016-11-17 16:36:12 2016-11-18 16:36:12
$nov = ''; // at start
$nov .= $month ["month"]; // date 2016-11-17 16:36:12
=> $nov .= '11'; //shorthand concatenation php
=> $nov = $nov . '11';
=> $nov = '' . '11'; // as $nov = '' at start
now $nov have value 11 in it.
$nov .= $month ["month"]; // date 2016-11-18 16:36:12
=> $nov .= '11'; //shorthand concatenation php
=> $nov = $nov . '11';
=> $nov = '11' . '11'; // as $nov has vale 11 in it
count($nov); // $nov have value '1111' which has 4 character so output will be 4
If you want your code to work do like this
$jan = 0; $feb = 0; $maa = 0; $apr = 0; $mei = 0; $jun = 0; $jul = 0; $aug = 0; $sep = 0; $okt = 0; $nov = 0; $dec = 0;
foreach($dates as $date){
$month = date_parse_from_format("Y-m-d H:i:s", $date->date);
if($month ["month"] == '01'){$jan++}
if($month ["month"] == '02'){$feb++;}
if($month ["month"] == '03'){$maa++;}
if($month ["month"] == '04'){$apr++;}
if($maand["month"] == '05'){$mei++;}
if($maand["month"] == '06'){$jun++;}
if($maand["month"] == '07'){$jul++;}
if($maand["month"] == '08'){$aug++;}
if($maand["month"] == '08'){$sep++;}
if($maand["month "] == '10'){$okt++;}
if($maand["month "] == '11'){$nov++;}
if($maand["month "] == '12'){$dec++;}
}
But i will suggest you to do in array like this
$visitorData = ['01'=>0,'02'=>0,'03'=>0,'04'=>0,'05'=>0,'06'=>0'07'=>0,'08'=>0,'09'=>0,'10'=>0,'11'=>0,'12'=>0];
foreach ($dates as $date) {
$month = date('m', strtotime($date->date));
$visitorData[$month]++;
}
Then JSON encode the array to feed it to the jquery
Hope this will help