如何使用时间戳在PHP/MySQL JSON谷歌图表表


How to use timestamp on PHP/MySQL to JSON Google Chart Table

我正在研究一个项目,我使用谷歌API图表,通过使用php, mysql &json。现在我卡住了'synctime'列,它使用mysql数据库上的时间戳数据类型,这给了我错误。我应该用什么来代替"date"?

任何帮助都是非常感激的。我的代码如下:
$sql="SELECT device, recordcount,  CONCAT(city, ', ', country) AS `Country`, synctime FROM devices";
$exec = mysqli_query($con,$sql);
mysqli_close($con);
$table = array();
$table['cols'] = array(
    //Labels for the chart, these represent the column titles
    array('id' => '', 'label' => 'device', 'type' => 'string'),
    array('id' => '', 'label' => 'recordcount', 'type' => 'number'),
    array('id' => '', 'label' => 'Country', 'type' => 'string'),
    array('id' => '', 'label' => 'synctime', 'type' => 'string')
    ); 
$rows = array();
foreach($exec as $row){
    $temp = array();
    //Values
    $temp[] = array('v' => (string) $row['device']);
    $temp[] = array('v' => (int) $row['recordcount']);
    $temp[] = array('v' => (string) $row['Country']);
    $temp[] = array('v' => (date) $row['synctime']);
    $rows[] = array('c' => $temp);
    }
$table['rows'] = $rows; 
$jsonTable = json_encode($table);

试试这个

首先转换日期格式,然后传递给没有(date)类型的数组,或者您可以定义字符串类型。

 $getdate = date("Y-m-d H:i:s" , strtotime($row['synctime'])); 
 $temp[] = array('v' => $getdate);