0

我有 2 台服务器,每台都运行完全相同版本的 PHP(5.3.5-1ubuntu7.8)。它们也是完全相同的 Ubuntu (11.04) 版本。我编写了一个 PHP 脚本来连接到 SQL Server 2008 R2 数据库,选择一个日期时间值,然后将其 print_r() 打印到屏幕上。

当我在一台服务器上运行脚本时,日期时间以这种格式返回:2011-08-20 00:00:00.000。当我在另一台服务器上运行它时,使用从同一数据库记录中提取的完全相同的脚本,我得到这种格式:2011 年 8 月 20 日 12:00:00.000AM。

我的问题是我无法弄清楚我的服务器上的差异在哪里导致了我的日期时间格式的差异。我已经在 Apache 和 CLI 的 php.ini 文件中使用了 mssql.datetimeconvert 设置,但无济于事。

有没有人遇到过这个问题并确定是什么原因造成的?在我决定重建服务器之前,任何想法都非常感谢。谢谢!

4

1 回答 1

0

Does PHP not have the ability to format datetime values in a specific format? This is likely to do with local regional settings on the operating system rather than anything else, but you can always request a specific format from SQL Server instead of relying on PHP or the O/S to format them the same way by coincidence.

SELECT CONVERT(CHAR(10), col, 120) + ' ' + CONVERT(CHAR(8), col, 108)
FROM dbo.table;

That said, PHP is probably the best place to do this, but I have to apologize - I'm not versed in PHP well enough to tell you how to do that (in C#, for example, you'd use .Format()).

From here:

http://php.net/manual/en/datetime.format.php

Looks like instead of just dumping the string to the screen you should be using

echo date_format($date_var_from_rs, 'Y-m-d H:i:s');

As for the default formats on your Ubuntu servers, this blog post might be useful as well:

http://ccollins.wordpress.com/2009/01/06/how-to-change-date-formats-on-ubuntu/

于 2012-06-06T15:59:04.687 回答