将 mssql 日期时间对象转换为 PHP 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6662685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:56:54  来源:igfitidea点击:

Convert mssql datetime object to PHP string

phpsql-serverdatetime

提问by Nick

I'm grabbing some information from a database and the record is in an MSSQL DateTime format, when I return it, it shows in my array as follows:

我正在从数据库中获取一些信息,并且记录采用 MSSQL DateTime 格式,当我返回它时,它在我的数组中显示如下:

[arrayItem] => DateTime Object
    (
        [date] => 2008-06-05 09:14:11
        [timezone_type] => 3
        [timezone] => Europe/London
    )

When I try to extract this as an array (ie $array[arrayItem][date]) I get an error:

当我尝试将其提取为数组(即$array[arrayItem][date])时,出现错误:

Fatal error: Cannot use object of type DateTime as array

致命错误:不能使用 DateTime 类型的对象作为数组

I have also tried formatting the data in SQL before it is passed to the PHP, and the strtotime functions and had no joy.

在将数据传递给 PHP 和 strtotime 函数之前,我还尝试在 SQL 中格式化数据,但没有任何乐趣。

Any recommendations would be very welcome, I'm tearing my hair out with this one!

任何建议都会非常受欢迎,我正在用这个撕掉我的头发!

Thanks

谢谢

回答by Brian

This has caught me out a couple of times too.

这也让我失望了几次。

Hopefully this will help you as it has myself :)

希望这对你有帮助,因为它对我自己有帮助:)

http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/

http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/

Here's the gist of what that page is saying, in case the link goes down.

这是该页面所说的要点,以防链接失效。

When querying against a column defined as a datetime, the native PHP SQL Server extensionreturns a string where as the Microsoft extensionreturns a DateTime object. So if you are expecting a string, you'll need to adjust your code accordingly.

当查询定义为 a 的列时datetime,本机 PHP SQL Server 扩展返回一个字符串,而Microsoft 扩展返回一个 DateTime 对象。因此,如果您需要一个字符串,则需要相应地调整您的代码。

It goes on to explain that you can simply add an additional parameter to your requests to the database, specifying that you want your dates to be returned as strings. Simply add the ReturnDatesAsStringsparameter, specifying true.

它继续解释说,您可以简单地向数据库请求添加一个附加参数,指定您希望日期作为字符串返回。 只需添加ReturnDatesAsStrings参数,指定true.

Example:

例子:

$connectionParams = array(
    'USR'=>'user',
    'PASS'=>'pass',
    'Database'='myDatabase',
    'ReturnDatesAsStrings'=>true  //<-- This is the important line
);
$conn = sqlsrv_connect('127.0.0.1',$connectionParams);

Then you can use $connas you would regularly for your sqlsrv database connection, only dates will return as strings, instead of DateTime objects.

然后您可以$conn像定期使用 sqlsrv 数据库连接一样使用,只有日期将作为字符串返回,而不是 DateTime 对象。

Alternately, if all you wanted was a timestamp out of the datetime you could call:

或者,如果您想要的只是日期时间之外的时间戳,您可以调用:

$myDateTimeObject->getTimestamp();

回答by Shakti Singh

You should access them like below. the associative index arrayItemcontains an object which have some properties.

您应该像下面那样访问它们。关联索引arrayItem包含一个具有某些属性的对象。

$array['arrayItem']->date;
$array['arrayItem']->timezone_type;

回答by ron

Try this. It works for me:

尝试这个。这个对我有用:

$array['arrayItem']->format('Y-m-d H:i:s');

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

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

回答by Sascha Galley

Since its an object, you can't get the properties like you do with an array. You need the ->sign for accessing the date. In your question it seems as if you var_dumped the variable $arrayItem, so, use it like this:

由于它是一个对象,因此您无法像使用数组那样获取属性。您需要->访问日期的标志。在您的问题中,您似乎对变量进行了 var_dump 处理$arrayItem,因此,请像这样使用它:

$date = strtotime($array['arrayItem']->date]);

回答by osnosn

echo $array['arrayItem']->format('Y-m-d H:i:s');

回答by Linesofcode

Another solution is to loop.

另一种解决方案是循环。

$_date = \DateTime::createFromFormat('D M d Y H:i:s e+', $the_date);
foreach($_dateStart as $row){
   echo $row; // returns 2014-06-04 15:00
   break;     // stops at the first position
}

回答by Mahabubur Rahman masum

If you want to show data in PHP page you just copy and paste in <?php ?>code :

如果要在 PHP 页面中显示数据,只需复制并粘贴<?php ?>代码:

$serverName = "your_sqlserver";
$username = "your_username";
$password = "your_password";
$database = "your_database";
$connectionInfo = array( "UID"=>$username, "PWD"=>$password, "Database"=>$database, "ReturnDatesAsStrings"=>true);
$connection = sqlsrv_connect($serverName, $connectionInfo);

$result = sqlsrv_query( $connection,"SELECT  * from table'");
$msrow = sqlsrv_fetch_array($result);

print_r($msrow['column_name']);

回答by Chintan Gor

It is functionality and simple to use.

它功能强大且易于使用。

You can convert date from date object instead of string here is the way how to use:

您可以从日期对象而不是字符串转换日期,这是使用方法:

$obj->arrayItem->format('H:i:s')
$obj->arrayItem->format('Y-m-d')

回答by Sanjay

This worked for me

这对我有用

date('Y-m-d', strtotime($rs->Fields('time')->value)

For Adodb connection on MSSQL Server

对于 MSSQL Server 上的 Adodb 连接