gpt4 book ai didi

javascript - 对象时间(2014,2,17 11 :02:08, 2014-03-17 11,02,08)没有方法'getTimezoneOffset

转载 作者:行者123 更新时间:2023-11-29 13:07:49 24 4
gpt4 key购买 nike

正在处理从 phpmyadmin 获取数据的谷歌图表。我在时间/日期格式方面遇到问题:Object Time(2014, 2, 17 11:02:08, 2014-03-17 11, 02, 08) 没有方法 'getTimezoneOffset.

我的数据库:

enter image description here

代码:

    <?php

$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("chart", $con);

$sth = mysql_query("SELECT * FROM googlechart");

$rows = array();
//flag is not needed
$flag = true;
$table = array();

$table['cols'] = array(

array('label' => 'Time', 'type' => 'datetime'),
array('label' => 'PH', 'type' => 'number'),
array('label' => 'temperature','type' => 'number'),
array('label' => 'Chlorine','type' => 'number'),
);

$rows = array();

while($r = mysql_fetch_assoc($sth)) {

// assumes dates are in the format "yyyy-MM-dd"
$dateString = $r['Time'];
$dateArray = explode('-', $dateString);
$year = $dateArray[0];
$month = $dateArray[1] - 1; // subtract 1 to convert to javascript's 0-indexed months
$day = $dateArray[2];

// assumes time is in the format "hh:mm:ss"
$timeString = $r['Time'];
$timeArray = explode(':', $timeString);
$hours = $timeArray[0];
$minutes = $timeArray[1];
$seconds = $timeArray[2];

$temp = array();
$temp[] = array('v' => "Time($year, $month, $day, $hours, $minutes, $seconds)");
$temp[] = array('v' => (string) $r['PH']);
$temp[] = array('v' => (string) $r['temperature']);
$temp[] = array('v' => (string) $r['Chlorine']);

$rows[] = array('c' => $temp);

}

$table['rows'] = $rows;
$jsonTable = json_encode($table);
/* echo $jsonTable; */

?>

HTML/JavaScript 代码:

   google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);

function drawChart() {
var data = new google.visualization.DataTable(<?=$jsonTable?>);



var options = {
/*width: 900, height: 900, */
title: 'Visualization',
curveType: 'function',
legend: { position: 'bottom' },
pointSize: 12,
vAxis: {title: "Values", titleTextStyle: {italic: false}},
hAxis: {title: "Time", titleTextStyle: {italic: false}},
explorer: {
actions: ['dragToZoom', 'rightClickToReset'],
axis: 'vertical'
}


};

var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);


}

最佳答案

这是在做什么...

$dateString = $r['Time'];
$dateArray = explode('-', $dateString);

...正在占用您的时间:

2014-03-17 11:02:08

...进入:

'2014'
'03'
'17 11:02:08'

..其中最后一项不是您想要的 $day

同样,

$timeArray = explode(':', $timeString);

...正在分块为:

'2014-03-17 11'
'02'
'08'

...第一个项目在几个小时内都不是您想要的。

但是,更严重的是,我不确定是否:

$temp[] = array('v' => "Time($year, $month, $day, $hours, $minutes, $seconds)");

...只会构建一个字符串,将被 Google's DataTable constructor 接受,但看起来并非如此。

虽然您有正确的想法来构建 JSON 对象以获得对 JavaScript 有用的格式,但遗憾的是 JSON 不支持日期,因此我相信一旦您在 PHP 端获取了日期和小时,您将需要迭代通过 JavaScript 端的表格将这些字符串转换为 Date() 对象,而不是您构建的 Time(...) 字符串。

关于javascript - 对象时间(2014,2,17 11 :02:08, 2014-03-17 11,02,08)没有方法'getTimezoneOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22474506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com