类DateTime的对象无法转换为字符串

时间:2016-09-01 09:03:27

标签: php datetime

我有一个MSSQL数据库,我希望使用php在显示屏上写日期数据(列名Zeitstempel)。但我得到了这个错误:

  

捕获致命错误:类DateTime的对象无法转换为字符串

为什么我会收到这样的错误?

date_default_timezone_set(Europe/Berlin) ;
$connectionInfo = array( "UID"=>$uid,                              
                     "PWD"=>$pwd,                              
                     "Database"=>$databaseName);   

/* Connect using SQL Server Authentication. */    
$conn = sqlsrv_connect( $serverName, $connectionInfo);    

$tsql = "SELECT Zeitstempel FROM statistik where mpid=1 ";    

/* Execute the query. */    

$stmt = sqlsrv_query( $conn, $tsql);    

if ( $stmt )    
{    
 echo "Statement executed.<br>\n";    
}     
else     
{    
 echo "Error in statement execution.\n";    
 die( print_r( sqlsrv_errors(), true));    
}    

/* Iterate through the result set printing a row of data upon each iteration.*/    

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))    
{    
echo "Col1: ".$row[0]."\n";    
}

2 个答案:

答案 0 :(得分:1)

这是因为sqlserver date类型转换为php DateTime类型

http://msdn.microsoft.com/en-us/library/cc296193.aspx

使用format方法$row[0]->format('...')

http://php.net/manual/en/datetime.format.php

答案 1 :(得分:-1)

试试这项工作

while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC))    
{    
  echo "Col1: ".$row[0]->format('Y-m-d H:i:s')."\n";    
}