如何将MySQL日期时间值转换为谷歌图表api日期时间

时间:2021-04-24 16:40:13

I am trying to convert a value from a datetime field in a MySQL database to a value I can pass on to a Google chart datetime field.

我试图将MySQL数据库中的日期时间字段中的值转换为我可以传递到Google图表日期时间字段的值。

For example a MySQL value: 2012-03-05 17:03:56.

例如MySQL值:2012-03-05 17:03:56。

Google Chart Api link: http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html

Google Chart Api链接:http://code.google.com/apis/chart/interactive/docs/gallery/linechart.html

I want to pass this into a column defined like this: data.addColumn('datetime', 't');

我想将其传递到如下定义的列:data.addColumn('datetime','t');

I want to send it into a row as such: data.addRow([date_value]);

我想将它发送到一行:data.addRow([date_value]);

I'm not sure how to convert between the two. So what I am asking is How do I convert MySQL datetime value to a google chart api datetime value?

我不确定如何在两者之间进行转换。所以我要问的是如何将MySQL日期时间值转换为谷歌图表API日期时间值?

1 个解决方案

#1


2  

Google's just using the standard javascript Date object. Easier method would be to get a unix_timestamp(yourdatefield) out of MySQL, which gives you seconds-since-the-epoch.

Google只使用标准的javascript Date对象。更简单的方法是从MySQL中获取unix_timestamp(yourdatefield),从而为你提供从历史开始的秒数。

Javascript's date object accepts milliseconds-since-the-epoch as one initializer value, so:

Javascript的日期对象接受毫秒 - 自该纪元作为一个初始化值,因此:

data.addRow(new Date(<?php echo ($seconds_from_db) ?>000));
                                                     ^^^--- 3 extra zeroes to make it a millisecond value

#1


2  

Google's just using the standard javascript Date object. Easier method would be to get a unix_timestamp(yourdatefield) out of MySQL, which gives you seconds-since-the-epoch.

Google只使用标准的javascript Date对象。更简单的方法是从MySQL中获取unix_timestamp(yourdatefield),从而为你提供从历史开始的秒数。

Javascript's date object accepts milliseconds-since-the-epoch as one initializer value, so:

Javascript的日期对象接受毫秒 - 自该纪元作为一个初始化值,因此:

data.addRow(new Date(<?php echo ($seconds_from_db) ?>000));
                                                     ^^^--- 3 extra zeroes to make it a millisecond value