[英]Display the date in PHP from a datetime field in SQLExpress
I have been crawling through the web and trying to find a solution so I am coming here.我一直在网上爬行并试图找到解决方案,所以我来到这里。
I have an MSSQL database and I am using PHP5.3 to connect and fetch data from one of the tables.我有一个 MSSQL 数据库,我使用 PHP5.3 连接并从其中一个表中获取数据。 The issue I am having has to do with dates.
我遇到的问题与日期有关。
$result = sqlsrv_query($conn, "SELECT TOP 10 * FROM TAData WHERE DateTime >= '2013-11-11T07:45:00.000'");
while($row = sqlsrv_fetch_array($result))
{
print_r ($row['DateTime']);
echo "<br />";
echo date("Y-m-d H:i:s", strtotime($row['DateTime']));
}
With the output looking like this:输出如下所示:
DateTime Object ( [date] => 2013-11-11 07:46:08 [timezone_type] => 3 [timezone] => Africa/Johannesburg )
January 1, 1970, 2:00 am
All I need is a simple date and time (YYYY-MM-DD HH:MM) that I can use.我只需要一个我可以使用的简单日期和时间 (YYYY-MM-DD HH:MM)。 Once I have it in any sort of format (or understand how it works), I can manipulate it from there.
一旦我以任何形式拥有它(或了解它是如何工作的),我就可以从那里操纵它。 But it is not displaying in any functional way.
但它没有以任何功能方式显示。
You cannot access property date
on DateTime object, like in the other answer:您无法访问 DateTime 对象上的属性
date
,就像在另一个答案中一样:
$dt = new DateTime();
echo $dt->date; # this will trigger Notice: Undefined property: DateTime::$date
Please don't use any strtotime()
or date()
functions, when you already have DateTime object!当您已经拥有 DateTime 对象时,请不要使用任何
strtotime()
或date()
函数! Just use method format()
on DateTime object to format datetime, like:只需在 DateTime 对象上使用
format()
方法来格式化日期时间,例如:
echo $dt->format('Y-m-d H:i:s');
# or $row['DateTime']->format('Y-m-d H:i:s'); in your example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.