我试图计算访客日期,以了解每月有多少次访问&将结果发布到jquery图表中。
好吧所以下面的代码"工作",但它以某种方式计算所有内容* 2.如果本月有1位访问者,它将输出2,为什么会这样?
我还想知道如何让这段代码更小? $ jan $ feb等似乎不是应该这样做的方式,但我是初学者,所以我不知道如何将这些代码变得更小和更小。更好。有人可以帮我吗?
数据库:
date
2016-11-17 16:36:12
腓:
$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"];}
}
答案 0 :(得分:0)
试试这个:
$arr = array()
foreach($dates as $date){
$month = date('m',strtotime($date->date));
$arr[$month][] = $date;
}
此处此代码创建一个数组,将月份作为键,并将该月份的数据附加为值。
答案 1 :(得分:0)
$visitsPerMonth = [];
foreach ($dates as $date) {
$month = date('m', strtotime($date->date));
$visitsPerMonth[$month]++;
}
如果$ date源自SQL数据库,则使用GROUP BY查询。
如果您的jQuery图表需要JSON输入,请在$ visitsPerMonth上使用json_encode()。
答案 2 :(得分:0)
根据您的要求,如果您将访问数据存储在数据库中,那么您可以像每月一样从数据库查询中获取计数
SELECT COUNT(*) FROM visitors YEAR(visited_date) = '2016' GROUP BY MONTH(visited_date)
如果本月有1位访客,则会输出2,为什么会这样?
这是因为你连接字符串并计算字符串的长度。月份为2个字符,因此每次迭代时,每次迭代连接2个字符。请参阅下面的示例
案例1:$ data有1个日期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
案例2:$ data有2个日期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
现在$ nov的值为11。
$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
如果您希望代码能够正常运行
$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++;}
}
但我会建议你像这样做数组
$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]++;
}
然后JSON对数组进行编码以将其提供给jquery
希望这会有所帮助