How to convert MySQL datetime value to google char

2019-05-27 03:03发布

问题:

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.

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

Google Chart Api link: 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');

I want to send it into a row as such: 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?

回答1:

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.

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

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