I'm running the following code:
<?php
include "php/connection.php";
$data = $_GET['data'];
$tsql = "SELECT * FROM Customer WHERE CustomerNumber = ".$data;
$result = sqlsrv_query($link, $tsql);
$row = sqlsrv_fetch_array($result);
echo $row['endDate']->format("Y-m-d H:i:s");
?>
The result set is gathered without issue, the problem is with trying to ouput it as a string as this throws the 500 error, i suspect that's because it's being returned as an object but no matter what i try i can't obtain the data as a string.
The endDate column holds data in this format:
2015-04-16 08:22:00.000
The column is 'datetime' format.
EDIT:
Code changed to reflect current code, the echo now shows '1900-01-01 00:00:00 ' but the actual data that should be showing is '2015-04-16 08:22:00.000'
Your date is saved as DateTime Object and hence its not getting displayed.
Here is an example
datetest = new DateTime();
$row = array();
$row["endDate"] = $datetest;
print_r($datetest);
echo "End Date :: ".$row["endDate"]->format("Y-m-d H:i:s");
The print_r()
statement will print something as
DateTime Object ( [date] => 2014-04-16 15:04:41 [timezone_type] => 3 [timezone] => Asia/Calcutta )
which is same in your case.
So in your case
echo $row['endDate']->format("Y-m-d H:i:s");
Issue solved by setting ReturnDatesAsStrings to true in my connection options, here's an example:
<?php
$serverName = "MyServer";
$connectionInfo = array( "Database"=>"AdventureWorks", 'ReturnDatesAsStrings '=> true);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn === false )
{
echo "Could not connect.
";
die( print_r( sqlsrv_errors(), true));
}
sqlsrv_close( $conn);
?>