简体   繁体   English

PHP ISO 8601格式和SQL Server-时区

[英]PHP ISO 8601 format and SQL Server - Timezone

When trying to insert data programatically using PHP, via the function 当尝试使用PHP通过函数以编程方式插入数据时

date('c')

It returns a date in the following format... 它以以下格式返回日期:

2011-12-20T19:01:03+11:00

But this is not suitable when inserting into a SQL Server database. 但这不适用于插入SQL Server数据库的情况。 It will return with the error... 它将返回错误...

Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败

Now of course there are many ways to get around this problem. 当然,现在有很多方法可以解决此问题。 One could easily go through and change the format when inserting the data and specify yyyy-mm-dd hh:mm:ss (not accurate). 在插入数据时,可以很容易地检查并更改格式,并指定yyyy-mm-dd hh:mm:ss(不准确)。 However, I would rather prefer to use the date('c') format and rely on that call to take care of my issue. 但是,我宁愿使用date('c')格式并依靠该调用来解决我的问题。

I am connecting to the database using PDO and Zend_DB. 我正在使用PDO和Zend_DB连接到数据库。

Any suggestions? 有什么建议么?

What is type of field that you use for date storage? 您用于日期存储的字段类型是什么? It won't work for datetime. 它不适用于日期时间。

Quick test: 快速测试:

-- shows error
CREATE TABLE #tmp1 (
    c1 varchar(30),
    dto1 datetimeoffset,
    dt1 datetime
)
GO

INSERT INTO #tmp1 (
    c1,
    dto1,
    dt1
) VALUES (
    '2011-12-20T19:01:03+11:00',
    '2011-12-20T19:01:03+11:00',
    '2011-12-20T19:01:03+11:00'
)
GO

SELECT * FROM #tmp1

DROP TABLE #tmp1
GO

-- works - datetime2
CREATE TABLE #tmp2 (
    c1 varchar(30),
    dto1 datetimeoffset,
    dt2 datetime2
)
GO

INSERT INTO #tmp2 (
    c1,
    dto1,
    dt2
) VALUES (
    '2011-12-20T19:01:03+11:00',
    '2011-12-20T19:01:03+11:00',
    '2011-12-20T19:01:03+11:00'
)
GO

SELECT * FROM #tmp2

DROP TABLE #tmp2
GO

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

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