javascript - Google Chart 中的 MySQL 日期时间

标签 javascript php mysql google-visualization

我正在处理来自 MySQL 的图表,它作为折线图工作得很好,但是当我更改为注释图时,由于它需要日期/时间,它给了我以下错误,我将类型更改为日期时间(是字符串)并且仍然有错误。

Type mismatch. Value 2014-07-23 19:03:16 does not match type datetime

原始代码

 <?php
        $con=mysql_connect("ip","user","pass") or die("Failed to connect with database!!!!");
        mysql_select_db("db", $con); 

        $sth = mysql_query("SELECT * FROM db.table");

        $data = array (
      'cols' => array( 
        array('id' => 'date', 'label' => 'date', 'type' => 'datetime'), 
        array('id' => 'Temp', 'label' => 'Temp', 'type' => 'number'), 
        array('id' => 'Humid', 'label' => 'Humid', 'type' => 'number')
    ),
    'rows' => array()
);

while ($res = mysql_fetch_assoc($sth))
    // array nesting is complex owing to to google charts api
    array_push($data['rows'], array('c' => array(
        array('v' => $res['TIME']), 
        array('v' => $res['TEMP']), 
        array('v' => $res['HUMID'])
    )));

?>

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1.1', {'packages':['annotationchart']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
            var bar_chart_data = new google.visualization.DataTable(<?php echo json_encode($data); ?>);
        var options = {
          title: 'Weather Station'
        };
        var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
        chart.draw(bar_chart_data, options);
      }
    </script>
</head>
            <body>
                <div id="chart_div" style="width: 900px; height: 500px;"></div>
            </body>
        </html>

最佳答案

“日期时间”数据类型需要非常特定的数据输入语法。使用 JSON 时,数据应构造为以下格式的字符串:'Date(year, month, day, hours, minutes, seconds, milliseconds)',其中 month 之后的所有选项 是可选的(默认为 1 for day0 for all others)并且 month 为零-索引(所以一月是 0 而不是 1)。

您可以像这样转换日期时间:

while ($res = mysql_fetch_assoc($sth)) {
    // assumes dates are patterned 'yyyy-MM-dd hh:mm:ss'
    preg_match('/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})/', $res['TIME'], $match);
    $year = (int) $match[1];
    $month = (int) $match[2] - 1; // convert to zero-index to match javascript's dates
    $day = (int) $match[3];
    $hours = (int) $match[4];
    $minutes = (int) $match[5];
    $seconds = (int) $match[6];
    array_push($data['rows'], array('c' => array(
        array('v' => "Date($year, $month, $day, $hours, $minutes, $seconds)"), 
        array('v' => $res['TEMP']), 
        array('v' => $res['HUMID'])
    )));
}

关于javascript - Google Chart 中的 MySQL 日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24929767/

相关文章:

javascript - NodeJs后端代码结构

javascript - jquery 函数仅在控制台中工作

javascript - 在 react native ListView 中搜索大数据

php - 将 JSON 字符串或数组转换为 Excel 文件?

php - 创建复杂的 IF、OR、NOT & AND SQL 查询 (MySQL)

javascript - 我需要根据小计计算总计,

php - 用我自己的数字和字母自动递增

php - 获取具有重复项计数的自定义分类术语

php MySQL 检索数据时出错

java - 尝试检索用户在 Controller 中输入的值并将其添加到 SpringBoot 应用程序中的 mysql 数据库中