2011-11-25 41 views
10

MSSQL 2005数据库有整理“German_Phonebook_BIN”(但这不重要)。通过PDO和FreeTDS(在Debian Squeeze下使用PHP)连接到db。当我尝试从表中选择日期时间值我得到的结果类似:MSSQL/dblib的PDO日期时间格式

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

但我希望得到

2008-01-01 00:00 :00

(注意,时间00:00:00转换为12:00:00,不知道为什么00:00 = 12:00 ???) 我没有办法操纵SELECT声明(用于与CONVERT进行转换)。我发现在PDO中没有选择设置日期格式的选项。查询运行前的SET DATEFORMATSET LANGUAGE不会影响到这一点。 任何人都可以提供一个提示,这可以做到(而且只能做)在PDO? (顺便说一句PEAR :: MBD2返回datetime列于预期的格式,但MDB2是可怕的,当它与UTF-8和MSSQL工作)

OK,一些更多的信息(只显示了重要的片段):

<?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]")); 
+0

这种现象是不正常的。你能展示你如何选择这些值以及如何用PHP输出它们? –

+1

看看https://bugs.php.net/bug.php?id=54648 – rabudde

+0

呃,我明白了。 --- –

回答

3

我发现使用PHP_PDO_DBLIB与SQL SRV最好的办法是存储日期为DATETIME2(6)在MS SQL Server数据库。无论如何,使用symfony框架似乎解决了很多问题。

0

我也有这个问题,并发现由于某些原因,当我让freetds应用freetds.conf中的设置(而不是仅使用连接字符串中的完整主机名)时,日期显示正确。

例如,如果我用:

$link = new PDO("dblib:host=myhost.myfulldomain.com;dbname=MYDB", $user, $pass); 

...那么预期它没有工作 - 的日期是古怪的。但如果我使用:

$link = new PDO("dblib:host=myhost;dbname=MYDB", $user, $pass); 

...然后它DID工作,因为它发现我的freetds.conf文件中的“myhost”。

我freetds.conf文件:

# $Id: freetds.conf,v 1.12 2007/12/25 06:02:36 jklowden Exp $ 
# 
# This file is installed by FreeTDS if no file by the same 
# name is found in the installation directory. 
# 
# For information about the layout of this file and its settings, 
# see the freetds.conf manpage "man freetds.conf". 

# Global settings are overridden by those in a database 
# server specific section 
[global] 
     # TDS protocol version 
; tds version = 4.2 

    # Whether to write a TDSDUMP file for diagnostic purposes 
    # (setting this to /tmp is insecure on a multi-user system) 
; dump file = /tmp/freetds.log 
; debug flags = 0xffff 

    # Command and connection timeouts 
; timeout = 10 
; connect timeout = 10 

    # If you get out-of-memory errors, it may mean that your client 
    # is trying to allocate a huge buffer for a TEXT field. 
    # Try setting 'text size' to a more reasonable limit 
    text size = 5242880 

# A typical Sybase server 
[egServer50] 
    host = symachine.domain.com 
    port = 5000 
    tds version = 5.0 

# My MS SQL server 
[myhost] 
    host = myhost.mydomain.com 
    port = 1433 
    tds version = 8.0