由于JS月份间隔0-11,图表日期错误

时间:2015-06-01 16:11:46

标签: javascript php mysql date

该脚本每天从mysql汇总下载数据并将其显示为图表,但据我所知,JS(highcharts)月份间隔为0到11,因为PHP输出1-12,使得当前月份五月 - > 6月31日(UTC 2015年,05年),6月31日不存在,导致5月31日和6月1日两种数据的视觉错误,数据存在,但没有显示。

[Date.UTC(2015, 05, 31), 4],
[Date.UTC(2015, 06, 01), 8],

enter image description here

我正在尝试修复日期偏移。

将日期偏移一个月将导致php从4月(因为UTC月04 = 5月)查询数据,并且还导致5月31日(UTC格式2015,04,31)为6月1日(UTC格式2015,05 ,01)

[Date.UTC(2015, 04, 25), 3],
[Date.UTC(2015, 04, 26), 4],
[Date.UTC(2015, 04, 27), 8],
[Date.UTC(2015, 04, 28), 9],
[Date.UTC(2015, 04, 29), 5],
[Date.UTC(2015, 04, 30), 8],
[Date.UTC(2015, 05, 01), 4],
[Date.UTC(2015, 05, 01), 8],

$date_download =  date("Y, m, d", strtotime($row["date"]." -1 month"));

完整代码:

<?php
$sql_download = "SELECT date, SUM(quantity) FROM downloads GROUP BY DATE(`Date`)";

?>
<script type="text/javascript">

        $('#container_downloads').highcharts({
            chart: {
                zoomType: 'x'
            },
            title: {
                text: 'Unity Downloads'
            },
            xAxis: {
                type: 'datetime',
                dateTimeLabelFormats: {
                    day: '%e. %b. %Y'
                },
                minRange: 14 * 24 * 3600000 // fourteen days

            },


            yAxis: {
                title: {
                    text: 'Downloads'
                },
                allowDecimals: false,

            },
            legend: {
                enabled: false
            },

            plotOptions: {
                area: {
                    fillColor: {
                        linearGradient: { x1: 0, y1: 0, x2: 0, y2: 1},
                        stops: [
                            [0, Highcharts.getOptions().colors[0]],
                            [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
                        ]
                    },
                    marker: {
                        radius: 2
                    },
                    lineWidth: 1,
                    states: {
                        hover: {
                            lineWidth: 1
                        }
                    },
                    threshold: null
                }
            },

            series: [{
                type: 'area',
                name: 'Downloads',
                data: [
    <?php 


    $result_download = $mysqli->query($sql_download);

    while($row = $result_download->fetch_assoc()){
       $quantity_download =  $row["SUM(quantity)"];     
       $date_download =  date("Y, m, d", strtotime($row["date"]));
       $ans_download = "[Date.UTC(" . $date_download . "), " .  $quantity_download . "]";
       echo $ans_download . ",\r\n";
    }

    ?>
                ]
            }]
        });

1 个答案:

答案 0 :(得分:1)

while($row = $result_download->fetch_assoc()){
       $quantity_download =  $row["SUM(quantity)"];     
       $date_str = strtotime($row["date"]);
       $year = date('Y', $date_str);
       $month = date('n', $date_str) - 1;
       $day = date('j', $date_str);
       $ans_download = "[Date.UTC(" . sprintf('%s, %s, %s',$year,$month,$day) . "), " .  $quantity_download . "]";
       echo $ans_download . ",\r\n";
}