Home Forums Chart Support Getting DateTime value from mysql db to create a chart

Getting DateTime value from mysql db to create a chart

Viewing 2 posts - 1 through 2 (of 2 total)
  • #21361

    Hi
    I am querying mysql database for two values datetime (format: 2018-04-18 11:47:53) and a number values (100 … etc) to create a chart, I am trying to use the below code but its not working, is there any other way I can create this chart, sorry I am also new to php and charting

    $vsql = “SELECT Datetime, value FROM x
    $result = mysqli_query($con,$vsql);
    $dataPoints1 = array();

    while ($row = mysqli_fetch_array($result)) {
    $entry = (strtotime($row[0])*1000) .’, ‘.$row[1];
    array_push($dataPoints1, $entry);
    }
    }

    • This topic was modified 5 years, 9 months ago by d77info.
    #21377

    @d77info,

    To display the chart containing datetime value in PHP, you need to first convert datetime string to PHP timestamp using strtotime(). Later convert PHP timestamp value to javascript timestamp and assign that value to dataPoint x-value along with the corresponding y-value in the following format –

    $dataPoints[] = array("x" => (strtotime($row[0])*1000), "y" => $row[1]);

    Then convert the array ‘$dataPoints’ into JSON representation using json_encode(). Also, set the `xValueType: “dateTime”` as timestamp values are getting assigned to the chart instead of Date objects as shown below –

    xValueType: "dateTime",
    dataPoints:  <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK) ?>

    Kindly download the sample PHP project from this link.

    The above sample shows how to pass dateTime from a database into Charts in PHP. A ReadMe file has been included for your convenience with all the instructions to set up the project.

    Chart with Date Axis in PHP

    ___________
    Indranil Deo
    Team CanvasJS

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.