Highcharts - Date format from MS SQL to PHP issue

2019-09-17 09:59发布

I've been using php to retrieve information from an MS SQL database. All been fine tile I tried to retrieve a date and time format from the table.

This is the php I've been using:

$tsql5a = "SELECT * FROM FactTime"; 
$stmt5a = sqlsrv_query( $conn, $tsql5a);

$data5a = "";
while( $row = sqlsrv_fetch_array( $stmt5a, SQLSRV_FETCH_NUMERIC)){

$data5a .= "$row[1],";
}
$data5a = substr($data5a,0,strlen($data5a)-1);

The date format I'm retrieving from the table is:

2014-10-01 00:59:00.000

I'm getting the following error:

"Catchable fatal error: Object of class DateTime could not be converted to string"

I've looked through similar issues and they suggest a conversion to a string but I'm not certain at all of how to add this into the existing php.

Can anyone help.

Thanks Rob

1条回答
干净又极端
2楼-- · 2019-09-17 10:23

Okay, I continued to search and here is the answer!

$data5a .= date_format($row[1]," Y-m-d H:i:s,");

I needed to add a date formate to the row of data I was pulling from the mssql db table, so this is what the completed section of the code looks like.

$tsql5a = "SELECT * FROM FactTime"; 
$stmt5a = sqlsrv_query( $conn, $tsql5a);

$data5a = "";
while( $row = sqlsrv_fetch_array( $stmt5a, SQLSRV_FETCH_NUMERIC)){

$data5a .= date_format($row[1]," Y-m-d H:i:s,");    // I had to add a space before the Y and a comma after the s so the php would pass the right format to the chart
}
$data5a = substr($data5a,0,strlen($data5a)-1);      // This removes the last , from the array

I hope this will help others with the same problem.

Thanks Rob

查看更多
登录 后发表回答