afiori
Posts: 1
Joined: Sat Mar 31, 2012 6:07 am

UNIX Timestamp PHP JSON & Wordpress

Hello, I'm new to Highstock; it seems great! However, I did get stuck on getting the data formatted the way HighStocks prefers, specifically the UNIX_TIMESTAMP into json encode without double quotes. I hope the solution below spells it out for others who might have the same issue.

I'm using Highstock with Wordpress. I've created a plugin file that includes both the PHP, and the associated javascript to create the final chart.

UPDATE: I had been stuck with double quotes around my UNIX_TIMESTAMP after my data was json encoded, the resulting output looked like this:
["822985200000",9.99653] The UNIX_TIMESTAMP was being treated as a string.

THE FIX to remove the double quotes from the UNIX_TIMESTAMP was to convert or cast the timestamped variable to a float before the json encoding happens.

Example:

Original causing double quoted date (pay attention to $this_date):

Code: Select all

$date_array[] = array($this_date,(float)$this_value);
Version with the $this_date variable re-cast as float:

Code: Select all

$date_array[] = array((float)$this_date,(float)$this_value);
For Reference: I'm getting the UNIX_TIMESTAMP from the mysql DB like this:

Code: Select all

$query = "SELECT DISTINCT FundID, UNIX_TIMESTAMP(TDate)*1000 AS TDate, UnitValue FROM $data_table WHERE FundID IN($sFund) ORDER BY FundID, TDate";
	$result = mysql_query($query) or die ('Sorry, there was a database error.');
Within my Wordpress plugin I echo the final json encoded php variable into the javascript for the chart; This is all done inside the plugin. Please note the way I'm doing this keeps from having to do the the getJSON via HTTP routine. I'm able to output charts to the front end webpage by calling my plugin into the window like so:

jQuery snippet on the webpage:

Code: Select all

jQuery(window).load("myPlugin.php", jQuery("#div_Containing_ListUser_Selected_Stocks").serializeArray());

Return to “Highcharts Stock”