在PHP中使用时间轴Google Chart API-DataTable初始化 [英] Using timeline google chart api with php - DataTable initialization

查看:138
本文介绍了在PHP中使用时间轴Google Chart API-DataTable初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施时间表google chart https://developers.google.com/chart/interactive/docs/gallery/timeline

I am trying to implement the timeline google chart https://developers.google.com/chart/interactive/docs/gallery/timeline

但是使用日期格式会在生成图表时产生一些错误.所有日期均默认设置为 1969年12月31日.我知道我传递的日期格式错误.我该如何解决.

but using date format is creating some errors in generating the chart. All dates are set default to dec 31 1969. I know that I am passing a wrong format of date. How can I fix this.

尝试通过两种方式实施:

tried implementing in two ways :

$table['cols'] = array(
        array('id' => 'Role', 'type' => 'string'),
        array('id' => 'Name', 'type' => 'string'),
        array('id' => 'Start', 'type' => 'date'),
        array('id' => 'End', 'type' => 'date')
    );

一种方法:

$temp[] = array('v' => (string) $row['FirstName']); /*mysql and mysqli security please ignore */
$temp[] = array('v' => (string) $row['FirstName']);
$temp[] = array('v' =>  date('D M d Y H:i:s O',$row['HireDate'])); /*tried without date function also*/
$temp[] = array('v' =>  date('D M d Y H:i:s O',$row['ExpectedEndDate']));
$rows[] = $temp;

其他方式:

$temp[] = $row['FirstName'];
$temp[] = $row['FirstName'];
$temp[] =date('D M d Y H:i:s O',$row['HireDate']); 
$temp[] =date('D M d Y H:i:s O',$row['ExpectedEndDate']);
$rows[] = $temp;

,然后将其存储到表中并将其转换为 json .

and then storing it into table and converting it into json.

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

使用Google可视化 DataTable 将其存储为javascript变量,如下所示:

storing it as javascript variable using google visualization DataTable as follows:

 var dataTable = new google.visualization.DataTable(<?php echo $jsonTable;?>);

但是由于日期格式冲突(我认为),该图表未输出

But because of date format conflict (I think) the chart is not being outputted

在检查最终生成的输出时,变量会将值存储为:

On inspecting the final generated output the variable is storing the value as :

var dataTable = new google.visualization.DataTable({"cols":[{"id":"Role","type":"string"},{"id":"Name","type":"string"},{"id":"Start","type":"date"},{"id":"End","type":"date"}],"rows":[[{"v":"bob"},{"v":"bob"},{"v":"Wed Dec 31 1969 16:33:28 -0800"},{"v":"Wed Dec 31 1969 16:33:33 -0800"}],[{"v":"alan"},{"v":"alan"},{"v":"Wed Dec 31 1969 16:33:28 -0800"},{"v":"Wed Dec 31 1969 16:33:33 -0800"}]]});

任何人都可以建议如何使其工作.预先感谢.

Can anyone suggest how to make it work. Thanks in advance.

推荐答案

这是您的问题:

$temp[] = array('v' =>  date('D M d Y H:i:s O',$row['HireDate']));

日期必须以非常特定的字符串格式输入:日期(年,月,日,小时,分钟,秒)" ,其中 month 是该月的索引从零开始, month 之后的所有内容都是可选的( day 的默认值为1,其他所有参数的默认值为0).要以这种格式输入日期,您应该执行以下操作:

Dates must be input in a very specific string-format: "Date(year, month, day, hours, minutes, seconds)", where month is the zero-based index for the month, and everything after month is optional (defaults are 1 for day and 0 for everything else). To input your date in this format, you should do something like this:

$date = date('D M d Y H:i:s O',$row['HireDate']));
$year = (int) date_format($date, 'Y');
$month = ((int) date_format($date, 'm')) - 1; // adjust to javascript's 0-indexed months
$day  = (int) date_format($date, 'd');
$hours = (int) date_format($date, 'H');
$minutes = (int) date_format($date, 'i');
$seconds = (int) date_format($date, 's');

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

这篇关于在PHP中使用时间轴Google Chart API-DataTable初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆