将 mssql 日期时间对象转换为 PHP 字符串
我正在从数据库中获取一些信息,记录采用 MSSQL DateTime 格式,当我返回它时,它在我的数组中显示如下:
[arrayItem] => DateTime Object
(
[date] => 2008-06-05 09:14:11
[timezone_type] => 3
[timezone] => Europe/London
)
当我尝试将其提取为数组时(即 $array[arrayItem ][日期]
)我收到错误:
致命错误:无法将 DateTime 类型的对象用作数组
我还尝试在将数据传递给 PHP 之前在 SQL 中格式化数据,并且使用 strtotime 函数但没有任何乐趣。
任何建议都将非常受欢迎,我正在为这个而抓狂!
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
当您创建类似于 DATETIME 类型的查询时
,结果就是您所描述的对象。要将日期部分作为字符串返回,您可以在 SQL 级别解决它,请
参阅 23 含义 此处。
When you create the query like
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 assee the 23 meaning here.
这也让我困惑了好几次。
希望这会对您有所帮助,就像我自己一样:)
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
这是要点该页面的内容,以防链接断开。
它继续解释说,您可以简单地向数据库请求添加一个附加参数,指定您希望将日期作为字符串返回。 只需添加
ReturnDatesAsStrings
参数,指定true
。示例:
然后您可以像常规那样使用
$conn
您的 sqlsrv 数据库连接中,只有日期会以字符串形式返回,而不是 DateTime 对象。或者,如果您想要的只是日期时间之外的时间戳,您可以调用:
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.
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, specifyingtrue
.Example:
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:
您应该像下面这样访问它们。关联索引
arrayItem
包含一个具有某些属性的对象。You should access them like below. the associative index
arrayItem
contains an object which have some properties.试试这个。它对我有用:
参考:http://php.net/manual/en/datetime .format.php
Try this. It works for me:
Reference: http://php.net/manual/en/datetime.format.php
由于它是一个对象,因此您无法像使用数组那样获取属性。您需要
->
符号来访问日期。在你的问题中,似乎你 var_dumped 变量$arrayItem
,所以,像这样使用它: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:另一种解决方案是循环。
Another solution is to loop.
这对我有用
对于 MSSQL Server 上的 Adodb 连接
This worked for me
For Adodb connection on MSSQL Server
它功能齐全且易于使用。
您可以从日期对象而不是字符串转换日期
下面是使用方法:
It is functionality and simple to use.
You can convert date from date object instead of string
here is the way how to use:
如果您想在 PHP 页面中显示数据,只需复制并粘贴
代码:
If you want to show data in PHP page you just copy and paste in
<?php ?>
code :