【问题标题】:PDO datetime format for MSSQL/dblibMSSQL/dblib 的 PDO 日期时间格式
【发布时间】:2012-01-06 04:32:08
【问题描述】:

MSSQL 2005 数据库具有排序规则“German_Phonebook_BIN”(但这并不重要)。与 db 的连接是通过 PDO 和 FreeTDS 完成的(在 Debian Squeeze 下使用 PHP)。当我尝试从表中选择日期时间值时,我得到如下结果:

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 以预期的格式返回日期时间列,但 MDB2 在必须与 UTF-8 和 MSSQL 一起使用时很糟糕)

好的,更多信息(仅显示重要的 sn-ps):

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

【问题讨论】:

  • 这种行为不正常。你能展示你如何选择这些值以及如何在 PHP 中输​​出它们吗?
  • 这似乎是一个 PDO “错误”,但 PDO 是目前 UTF-8 编码数据的最佳选择

标签: php sql sql-server-2005 datetime pdo


【解决方案1】:

我也遇到了这个问题,并且发现由于某种原因,当我让 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);

...然后它确实起作用了,因为它在我的 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

【讨论】:

    【解决方案2】:

    我发现将 PHP_PDO_DBLIB 与 SQL SRV 一起使用的最佳方法是将日期作为 datetime2(6) 存储在 MS SQL SERVER DB 中。反正使用symfony框架似乎解决了很多问题。

    【讨论】:

      【解决方案3】:

      检查/etc/freetds/locales.conf 中的设置或FREETDSCONF 指向的任何位置 - 示例参见https://www.centos.org/modules/newbb/viewtopic.php?topic_id=29646

      另一种选择是使用convert in your SQL statement...

      【讨论】:

      • 我因为一个关键原因投了反对票:链接现在已经失效,不幸的是 web.archive.org 没有归档该页面。作为将近 7 年后遇到相同问题的人,如果您可以将有趣的部分嵌入答案中,而不是仅提供外部链接,那么这将在未来大有帮助。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-13
      相关资源
      最近更新 更多