2011-11-25 107 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