简体   繁体   English

PHP:将pdo_sqlsrv datetime列作为对象返回(Linux MSSQL驱动程序)

[英]PHP: return pdo_sqlsrv datetime column as object (Linux MSSQL driver)

The pdo_sqlsrv PHP driver always returns a datetime column from MS SQL as a String. pdo_sqlsrv PHP驱动程序始终以字符串形式从MS SQL返回datetime列。 I want it to be an object (just like w/ the official Windows MSSQL driver for PHP) 我希望它是一个对象(就像带有用于PHP的官方Windows MSSQL驱动程序一样)

I explicitly set ReturnDatesAsStrings to false in the connect parameters for the DB connection, and tried setting the mssql.datetimeconvert to both On and Off in my php.ini, but none of that yields a datetime object in PHP as a result. 我在数据库连接的连接参数中明确将ReturnDatesAsStrings设置为false,并尝试在php.ini中将mssql.datetimeconvert设置为On和Off,但是没有一个结果产生PHP中的datetime对象。 Only Strings: Feb 17 2016 09:48:56:213AM 仅字符串: Feb 17 2016 09:48:56:213AM

I retrieve the data via sqlsrv_fetch_object(sqlsrv_query($connection, $querystr)) 我通过sqlsrv_fetch_object(sqlsrv_query($connection, $querystr))检索数据

Through Google, I can only find people having the opposite problem (wanting to return dates as strings, not objects)... But I want the Linux version to be compatible to the Windows version in that regard. 通过Google,我只能找到遇到相反问题的人(希望将日期作为字符串而不是对象返回)...但是我希望Linux版本在这方面与Windows版本兼容。

I've found no low-level solution, other than creating a function to ensure the date is converted to an object: 除了创建确保日期转换为对象的函数外,我没有找到任何底层解决方案:

Function cdate($dat) {
    return (gettype($dat)=="string" ? new DateTime($dat) : $dat);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM