MSSQL / dblib的PDO datetime格式 [英] PDO datetime format for MSSQL/dblib

查看:350
本文介绍了MSSQL / dblib的PDO datetime格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSSQL 2005数据库已整理German_Phonebook_BIN(但这并不重要)。通过PDO和FreeTDS(使用Debian Squeeze下的PHP)连接到db。当我尝试从表格中选择日期时间值时,我会得到如下结果:

MSSQL 2005 database has collation "German_Phonebook_BIN" (but that's not important). Connection to db is done via PDO and FreeTDS (using PHP under Debian Squeeze). When I try to select datetime values from a table I get results like:

2008年4月1日12:00:00:000

Apr 1 2008 12:00:00:000

但我希望得到

2008-01-01 00:00:00

2008-01-01 00:00:00

(注意,00:00:00的时间转换为12:00:00,不知道为什么00:00 = 12:00)
我没有办法操纵 SELECT 语句(用 CONVERT 进行转换)。在PDO中没有找到设置日期格式的选项。 SET DATEFORMAT SET LANGUAGE 在运行查询之前也不会影响此操作。
任何人都可以提供一个提示,在PDO中可以完成(只能完成) (Btw。PEAR :: MBD2以预期的格式返回datetime列,但MDB2在使用UTF-8和MSSQL时是可怕的)

(Regard, that the time 00:00:00 is transformed into 12:00:00, don't know why 00:00=12:00???) There is no way for me to manipulate the SELECT statements (for doing a conversion with CONVERT). I found no option in PDO for setting a date format. SET DATEFORMAT and SET LANGUAGE before the query runs doesn't also affect this. Can anyone give a hint where this can be done (and only be done) in PDO? (Btw. PEAR::MBD2 returns datetime columns in the expected format, but MDB2 is horrible when it has to work with UTF-8 and MSSQL)

信息(仅显示重要片段):

OK, some more information (shows only important snippets):

<?php
$this->_dsn = 'dblib:host=' . $this->_db['host'] . ';dbname=' . $this->_db['database'] . ';charset=UTF-8';
$this->_handle = new PDO($this->_dsn, $this->_db['user'], $this->_db['password']);
print_r($this->_handle->query("SELECT [date_column] FROM [some_table]"));


推荐答案

检查 /etc/freetds/locales.conf 或任何 FREETDSCONF 指向 - 例如,请参阅 https://www.centos.org /modules/newbb/viewtopic.php?topic_id=29646

check the setting in /etc/freetds/locales.conf or wherever FREETDSCONF points to - for an example see https://www.centos.org/modules/newbb/viewtopic.php?topic_id=29646.

另一个选项可能是使用 convert ...

Another option could be to use convert in your SQL statement...

这篇关于MSSQL / dblib的PDO datetime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆