1

I'm trying to print out an array of dates pulled from an API, which come out formatted as YYYYmmdd, i.e. 20160701. When I convert them to a friendlier format and then go to print them in Highcharts, it actually will do a mathematical calculation based on the operator being used to separate the date elements. Obviously that's not what I want it to do. So the first date is actually performing 7 minus 1 minus 2016, resulting in the -2010. Does anyone know what is causing this?

PHP Snippet

foreach ($arr['rows'] as $item){
 $dates[] = DateTime::createFromFormat('Ymd', $item[0])->format('m-d- Y');
 }

Javascript Highchart Plugin

$('#myChart').highcharts({
          xAxis: {
              categories: [
                <?php
                echo implode(',', $dates);
                ?>
              ]
          },

What the dates end up looking like:

Output Image

3
  • 1
    could it be the extra space in your format call? format('m-d- Y'); should be format('m-d-Y');, right? Commented Aug 9, 2016 at 16:45
  • Why not just use a datetime x axis type, and skip this hassle anyway? Commented Aug 9, 2016 at 16:48
  • If you are going to do it this way, though, use php's json_encode() function rather than implode(), ie: xAxis: { categories: <?php echo json_encode($dates); ?> } Commented Aug 9, 2016 at 17:21

1 Answer 1

3

The problem is that you're not injecting any quotes in the Javascript source.

What you get is something like:

$('#myChart').highcharts({
      xAxis: {
          categories: [ 7-1-2016 ]   //  <--- should be [ "7-1-2016" ]
      }
});

which is evaluated as categories: [ -2010 ] on the Javascript side.

You should do:

$('#myChart').highcharts({
      xAxis: {
          categories: [
            <?php
              echo '"'.implode('","', $dates).'"';
            ?>
          ]
      }
});

Or if you prefer to have this fixed in the PHP code that is building this array:

foreach ($arr['rows'] as $item){
    $dates[] = DateTime::createFromFormat('Ymd', $item[0])->format('"m-d-Y"');
}

EDIT: as suggested by jlbriggs, a better approach would be to use json_encode().

At least on $dates ...

$('#myChart').highcharts({
      xAxis: {
          categories: <?php echo json_encode($dates); ?>
      }
});

... or on the whole object passed to highcharts() (assuming it's entirely built on the PHP side):

$('#myChart').highcharts(<?php echo json_encode($highChartsObject); ?>);
Sign up to request clarification or add additional context in comments.

2 Comments

While that works, it's basically the (open-to-error) long-way-around to accomplish what json_encode() does for you explicitly.
@jlbriggs - Yes. I focused on the 'why is it not working as expected' explanation, but you're of course perfectly right. Answer edited accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.