2

I'm working on a module that will generate a graph using Google's chart api. The data used to generate the chart uses date objects because I'm graphing race times, like this

[
    ['Number', 'Time'],
    ['1', new Date(2014, 5, 5, 0, 1, 47, 470)],
    ['2', new Date(2014, 5, 5, 0, 1, 45, 880)],
    ['3', new Date(2014, 5, 5, 0, 1, 43, 520)],
    ['4', new Date(2014, 5, 5, 0, 1, 40, 670)]
]

I want to generate this using PHP and am having trouble passing this to the script. The code I have to generate the data looks like this

$jsData = array();
$jsData[] = array("Number","Time");

foreach($records_nodes as $node) {
  $number++;
  $uTime = strtotime("2014-01-01 0:$time");

  $hundreths = isset($node->field_racetime[$node->language]) ? substr($node->field_racetime[$node->language][0]['time'],-2) : 0;

  $jstime = sprintf("new Date(%d,%d,%d,%d,%d,%d,%d)",
      2014,
      1,
      1,
      12,
      date("m",$uTime),
      date("s",$uTime),
      $hundreths  * 10
  );
  $jsData[] = array("$number",$jstime);
}
drupal_add_js(array('records_view' => array('data' => $jsData)), 'setting');

This works well. I can see the data being passed to Drupal.settings.records_view.data. However, the second element (the new Date part) is being passed as a string:

['1', 'new Date(2014, 5, 5, 0, 1, 47, 470)'],

Is there a way to pass this as the JavaScript object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.