Using timeline google chart api with php - DataTable initialization

早过忘川 提交于 2021-02-08 11:30:26

问题


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

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')
    );

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.

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

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.


回答1:


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). 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");


来源:https://stackoverflow.com/questions/25706810/using-timeline-google-chart-api-with-php-datatable-initialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!