【问题标题】:Get visitors by month with php / mysql使用 php / mysql 按月获取访问者
【发布时间】:2016-11-20 11:45:39
【问题描述】:

我正在尝试计算访问者日期,以查看每月有多少访问量并将结果发布到 jquery 图表中。

好的,下面的代码“有效”,但它以某种方式计算所有内容 * 2。如果本月有 1 个访问者,它将输出 2,这是为什么呢?

我也想知道如何使这段代码更小? $jan $feb 等似乎不应该这样做,但我是一个初学者,所以我真的不知道如何使这段代码变得更小和更好。有人可以帮我解决这个问题吗?

数据库:

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"];}
    }

【问题讨论】:

  • 您是否从数据库中获取此数据?
  • 是的,来自mysql数据库。

标签: javascript php date filter


【解决方案1】:
$visitsPerMonth = [];
foreach ($dates as $date) {
    $month = date('m', strtotime($date->date));
    $visitsPerMonth[$month]++;
}

如果 $dates 源自 SQL 数据库,请改用 GROUP BY 查询。

如果您的 jQuery 图表需要 JSON 输入,请在 $visitsPerMonth 上使用 json_encode()。

【讨论】:

    【解决方案2】:

    试试这个:

    $arr = array()
    foreach($dates as $date){ 
       $month = date('m',strtotime($date->date));
       $arr[$month][] = $date;
    }
    

    这里的代码创建了一个数组,以月份为键并将该月份的数据作为值附加。

    【讨论】:

    • 你好 Parth,我该如何回应这个?我试图回显 $date,但它给了我错误,也许我不应该回显 $date。请帮忙。
    • 试试 print_r($date),你不能回显数组,要么使用 var_dump() 要么 print_r()
    【解决方案3】:

    根据您的要求,如果您将访问数据存储在数据库中,那么您可以像这样每月从数据库查询中获取计数

    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

    希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多