0

I have the following query to select the last five days price values:

$five_days_history = self::$db->select("SELECT CLOSE, TRADE_DATE
  FROM FF_HISTORICAL_STOCK_PRICE
  WHERE TRADE_DATE >= DATE_SUB(CURDATE(), INTERVAL 5 DAY) AND date <= CURDATE() and SYMBOL='$stock_symbol'
  ORDER BY TRADE_DATE DESC");

I need to get the TRADE_DATE output into the following type of JS array:

labels: ["January", "February", "March", "April", "May", "June", "July"],

I want to replace the months with my date output. I try and get it into a comma separated list which works fine with the below code:

$five_day_ouput = array();
foreach ($five_days_history as $five_history) {
    $display_date = date('m/d/Y', strtotime($five_history['TRADE_DATE']));
    array_push($five_day_ouput, $display_date);
}
$five_day_ouput = implode(', ', $five_day_ouput);

When I do a print_r I get the following results:

06/08/2016, 06/07/2016, 06/06/2016, 06/05/2016, 06/04/2016, 06/03/2016

I then tried to replace my label with the following code:

labels: [<?php echo $five_day_output; ?>],

It isn't working because I don't have quotes around my values. How could I do this?

0

2 Answers 2

1

You could simply add the quotes to the date when you're converting it to another format.

Try replacing:

$display_date = date('m/d/Y', strtotime($five_history['TRADE_DATE']));

By:

$display_date = date('"m/d/Y"', strtotime($five_history['TRADE_DATE']));

Now when you do the print_r, instead of displaying:

06/08/2016, 06/07/2016, 06/06/2016, 06/05/2016, 06/04/2016, 06/03/2016

It should display:

"06/08/2016", "06/07/2016", "06/06/2016", "06/05/2016", "06/04/2016", "06/03/2016"

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, if I do that the results on the graph aren't as I expect when I use the new variable and put it in the labels as a PHP variable. However if I print_r the array and copy and paste that into the labels it outputs correctly. Any idea why it would do that and doesn't like it straight from the PHP variable?
Oops @RolandStarke ! Corrected :)
Oddly enough, I'm not sure what happened but your first answer works now.
The one where I asked your to add quotes @user1048676?
Well it's working because it's adding the double quotes to the date. But I thought you needed the date value somewhere else @user1048676?
|
1

Just encode your array to json with json_encode php command.

json_encode(['06/08/2016', '06/07/2016', '06/06/2016', '06/05/2016', '06/04/2016', '06/03/2016']);

7 Comments

If I do this, the output to the screen is null. I tried echo json_encode($five_day_output);
are you sure that your $five_day_output is an array and not empty?
yes, if I do print_r for the array I get 06/08/2016, 06/07/2016, 06/06/2016, 06/05/2016, 06/04/2016, 06/03/2016
if it is an array you shoud receive something like this: Array ( [0] => 06/08/2016 [1] => 06/07/2016 [2] => 06/06/2016 [3] => 06/05/2016 [4] => 06/04/2016 [5] => 06/03/2016 ) do you? You also can check your variable print_r(is_array($five_day_output));
Sorry, I forgot a line where I was imploding it to get the commas which I thought I needed. When I do the json_encode, it still shows up weird on the graph. Only when I type the same string in manually does the graph show up correctly.
|

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.