简体   繁体   English

在PHP中使用时间轴Google Chart API-DataTable初始化

[英]Using timeline google chart api with php - DataTable initialization

I am trying to implement the timeline google chart https://developers.google.com/chart/interactive/docs/gallery/timeline 我正在尝试实施时间轴Google chart https://developers.google.com/chart/interactive/docs/gallery/timeline

but using date format is creating some errors in generating the chart . 但是使用date format会在生成chart产生一些错误。 All dates are set default to dec 31 1969 . 所有日期均默认设置为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')
    );

One way: 单程:

$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;

Other way: 另一种方式:

$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;

and then storing it into table and converting it into json . 然后将其存储到表中并将其转换为json

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

storing it as javascript variable using google visualization DataTable as follows: 使用Google可视化DataTable表将其存储为javascript变量,如下所示:

 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. 提前致谢。

This is your problem: 这是你的问题:

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

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). 日期必须是在一个非常特定的字符串格式输入: "Date(year, month, day, hours, minutes, seconds)" ,其中month为月从零开始的索引,以及一切后, month是可选的(默认值是1表示day ,0表示其他所有内容)。 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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM