I have been trying to plot some data from a database using canvasJS. I currently have it so it takes my data, encodes it as json and passes it back where it is plotted on a line graph and this works fine but it simply uses numerical values of 0, 1 , 2 , 3 ... for the x values.
Now I would like to take the times I also have in the database and use these as x values. When I simply take them and stick them in the json encode they are added as strings in the format (hh:mm:ss)
When I use strtotime($row['time'])
they are converted to timestamps and plotted but these make no sense. How can I convert them back to javascript times that make more sense when plotted?
here is the code that does the ajax call:
$.post( "display_ajax.php", { hour: $("#slider-range").slider("values",0)+":"+$("#slider-range").slider("values",1)}, function( datax ) {
console.log(datax);
dataPoints = datax;
dataSeries.dataPoints = dataPoints;
data.push(dataSeries);
console.log(dataPoints);
draw_chart ();
},"json");
where the data being passed is two values taken from a jquery slider that correspond to a time range.
Javascript timestamps include milliseconds.
Integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC (Unix Epoch)
In your PHP, when you do strtotime($row['time'])
you need to multiply by 1000 as strtotime
returns the number of seconds since 1 January 1970 00:00:00 UTC up to the date specified.
$js_time = strtotime($row['time']) * 1000;