This is sort of a PHP question too, as that is where I’m adding the code, but it is to set up for javascript.
Basically I just want to know what format of date string JS will read.
I’m trying to make a simple graph with Highcharts JS, the data comes from an SQL DB Table, two pieces of data, Date and Balance, Date is of course stored in SQL Date format YYYY-MM-DD.
But the script is not reading it right.
I thought YYYY,MM,DD would work, but that confuses the script with the commas when I add to an array.
What’s the simple way to make a string containing a date that JS will read?
Hi Sam,
The following code will convert a MySQL format date into a JS Date object:
var inputDate = '2013-12-24',
t = inputDate.split('-'),
myDate = new Date(t[0], t[1]-1, t[2]);
Nevermind, I solved it. It turns out that the Highcharts script wants the date as a timestamp in miliseconds, then you must tell the script that the graph axis is ‘datetime’ and it works.
So for anyone wondering, the PHP is like this:
$finSQL = "SELECT * FROM tablename ORDER BY Date";
$result = mysql_query($finSQL);
while ($row = mysql_fetch_array($result)) {
$date = strtotime($row['Date']);
$date = $date *= 1000 ;
$balance = $row['Balance'] ;
$data[] = "[$date, $balance]";
}
And the JS like this:
<script type="text/javascript">
var chart = new Highcharts.Chart({
chart: {
renderTo: 'container'
},
xAxis: {
type: 'datetime',
},
yAxis: {
title: {
text: 'Balance'
}
},
series: [{
name: 'Balance in Pounds',
data: [<?php echo join($data, ',') ?>]
}]
});
</script>