【问题标题】:MySQL Datetime in Google Chart谷歌图表中的 MySQL 日期时间
【发布时间】:2014-07-24 09:19:30
【问题描述】:

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

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>

【问题讨论】:

    标签: javascript php mysql google-visualization


    【解决方案1】:

    “日期时间”数据类型需要非常特定的数据输入语法。使用 JSON 时,数据应构造为以下格式的字符串:'Date(year, month, day, hours, minutes, seconds, milliseconds)',其中month 之后的所有选项都是可选的(day0 其他所有选项默认为 1)和 @987654326 @ 是零索引(所以一月是 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'])
        )));
    }
    

    【讨论】:

    • 谢谢@asgallant,更接近了,但我仍然收到一个新错误Type mismatch. Value Date(0, -1, 0, 0, 0, 0) does not match type datetime 我所做的只是将while ($res = mysql_fetch_assoc($sth)) 添加到代码的顶部。
    【解决方案2】:

    感谢 Asgallant 和大量的摆弄,下面的代码解决了我所有的问题

        array('v' => 'Date(' . date('Y,n,d,H,i,s', strtotime($res['TIME'])).')'), 
        array('v' => floatval($res['TEMP'])), 
        array('v' => floatval($res['HUMID']))
    

    我找到了一种使用 PHP Date 函数将 MySQL Datetime 转换为 javascript 的简化方法,虽然 Temp 和 Humid 值在 MySQL 中存储为小数,但 javascript 不喜欢它,所以我也使用 floatval 来使这些工作。现在我有一个快乐、有效的注释图表!

    【讨论】:

    • 这些日期将相差 1 个月(因为 javascript 使用从零开始的数月索引),这就是为什么您需要像我在回答中所做的那样打破日期。
    • 他们现在好像还好?
    • 对照数据库检查图表中的日期,您会发现它们相差 1 个月。例如:'Date(' . date('Y,n,d,H,i,s', strtotime('January 1, 2014')).')' 变为 'Date(2014,1,1,0,0,0)'(在解析为 javascript Date 对象后)变为 February 1, 2014
    • 啊,对不起,我明白你的意思了,但你的代码不起作用?我刚收到Type mismatch. Value Date(0, -1, 0, 0, 0, 0) does not match type datetime 日期没有被转换。
    • 如果模式不匹配,那么您需要针对您的日期格式进行调整。根据代码 cmets,我假设格式为 'yyyy-MM-dd hh:mm:ss'
    猜你喜欢
    • 2012-03-23
    • 2014-04-22
    • 1970-01-01
    • 2016-03-22
    • 2014-04-23
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多