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

发布于 2024-11-19 12:17:55 字数 486 浏览 0 评论 0 原文

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

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

当我尝试将其提取为数组时(即 $array[arrayItem ][日期])我收到错误:

致命错误:无法将 DateTime 类型的对象用作数组

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

任何建议都将非常受欢迎,我正在为这个而抓狂!

谢谢

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:

[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:

Fatal error: Cannot use object of type DateTime as array

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

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

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

云胡 2024-11-26 12:17:56

当您创建类似于 DATETIME 类型的查询时

SELECT created FROM yourTable ...

,结果就是您所描述的对象。要将日期部分作为字符串返回,您可以在 SQL 级别解决它,请

SELECT CONVERT(varchar, created, 23) AS created FROM yourTable ...

参阅 23 含义 此处

When you create the query like

SELECT created FROM yourTable ...

and created is DATETIME type, the result is the object as you described. To return the date part as a string, you can solve it on the SQL level as

SELECT CONVERT(varchar, created, 23) AS created FROM yourTable ...

see the 23 meaning here.

挽袖吟 2024-11-26 12:17:55

这也让我困惑了好几次。

希望这会对您有所帮助,就像我自己一样:)

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

这是要点该页面的内容,以防链接断开。

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

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

示例:

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

然后您可以像常规那样使用 $conn您的 sqlsrv 数据库连接中,只有日期会以字符串形式返回,而不是 DateTime 对象。

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

$myDateTimeObject->getTimestamp();

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/

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 extension returns a string where as the Microsoft extension returns a DateTime object. So if you are expecting a string, you’ll need to adjust your code accordingly.

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 ReturnDatesAsStrings parameter, specifying 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 $conn as you would regularly for your sqlsrv database connection, only dates will return as strings, instead of DateTime objects.

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

$myDateTimeObject->getTimestamp();
罗罗贝儿 2024-11-26 12:17:55

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

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

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

$array['arrayItem']->date;
$array['arrayItem']->timezone_type;
音盲 2024-11-26 12:17:55

试试这个。它对我有用:

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

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

Try this. It works for me:

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

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

So要识趣 2024-11-26 12:17:55

由于它是一个对象,因此您无法像使用数组那样获取属性。您需要 -> 符号来访问日期。在你的问题中,似乎你 var_dumped 变量 $arrayItem,所以,像这样使用它:

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

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:

$date = strtotime($array['arrayItem']->date]);
孤者何惧 2024-11-26 12:17:55
echo $array['arrayItem']->format('Y-m-d H:i:s');
echo $array['arrayItem']->format('Y-m-d H:i:s');
时间你老了 2024-11-26 12:17:55

另一种解决方案是循环。

$_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
}

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
}
<逆流佳人身旁 2024-11-26 12:17:55

这对我有用

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

对于 MSSQL Server 上的 Adodb 连接

This worked for me

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

For Adodb connection on MSSQL Server

长不大的小祸害 2024-11-26 12:17:55

它功能齐全且易于使用。

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

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

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')
阳光下慵懒的猫 2024-11-26 12:17:55

如果您想在 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']);

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

$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']);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文