Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi there I am building a system that collect data. I store the data in mysql database and display it using amCharts. I making archive chart that displays change in the data over time. I have problems with conversion between mysql timestamp array and javascript date array using php and josn.

The mysql query goes like this:

SELECT 
    UNIX_TIMESTAMP( data ) AS data,ROUND(AVG(CurrentA),3),ROUND(AVG(CurrentB),3),ROUND(AVG(CurrentC),3)
FROM
    Danni
WHERE 
    data 
    BETWEEN 
        '2013-07-13 00:00:00' 
    AND 
        '2013-07-16 00:00:00' 
GROUP BY
    FLOOR(UNIX_TIMESTAMP(data)/72)
LIMIT 40

The returned data is formated by this php code:

$data = array();
while ($row = mysql_fetch_array($ustroistvo)){
    $data['date'][] = $row['0'];
    $data['CurrentA'][] = $row['1'];
    $data['CurrentB'][] = $row['2'];
    $data['CurrentC'][] = $row['3'];
}
echo json_encode($data);
exit();

So the end result is

{"date":["1373748170","1373748192","1373748264","1373748336","1373748408","1373748480","1373748553","1373748624","1373748696","1373796506","1373796577","1373796648","1373796720","1373796792","1373796864","1373796936","1373797008"],"CurrentA":["45.667","37.794","37.508","35.815","31.238","33.061","32.937","35.385","36.020","31.852","34.047","34.508","30.672","37.292","32.554","39.262","32.314"],"CurrentB":["39.000","36.921","33.769","35.123","36.492","41.576","42.492","34.862","36.041","34.967","37.062","33.108","35.531","34.262","33.385","35.877","35.941"],"CurrentC":["35.000","32.429","35.862","36.785","34.873","36.894","31.921","36.938","33.714","40.541","28.688","34.308","33.266","39.846","35.708","34.908","32.118"]}

So to convert the date object to actual javascript date I use:

success: function (data) {
  console.log(data.date);
  data.date=new Date(data.date*1000);
  console.log(data.date);
  chart.dataProvider=data;
  chart.validateData();
}

The problem lies here data.date=new Date(data.date*1000) gives me invalid date and I think it is because data.date is array. I do not know how to format it properly so that data.date=new Date(data.date*1000) returns date array.

share|improve this question
    
Since its a array, which has set of data, may be you need to make that calculation(data.date*1000) for each data in array.this can be achieved by looping around array until there are no more items in it. –  dreamweiver Jul 19 '13 at 7:17
    
Well that is the thing I am thinking of but I do not know how to do. :D –  melanholly Jul 19 '13 at 7:20
    
That shouldnt be any different from the regular js looping clause.var len=data.date.length;for(var i=0;i<len;i++){ var dt=data.date[0]; //now do your processing with every single data here } –  dreamweiver Jul 19 '13 at 7:25
add comment

1 Answer

up vote 1 down vote accepted

Simply

 data.date = data.date.map(function(d) { return new Date(d * 1000) })

Also, it's common to write array names in plural, to improve readability and avoid extra confusion. Rename the date field to dates.

share|improve this answer
    
Thank you this code does The thing i've been searching for. The was written for other part of the website and now I am modifying it for the archives part that is the reason 'date' is not 'dates'. –  melanholly Jul 19 '13 at 7:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.