简体   繁体   中英

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

$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! Just use method format() on DateTime object to format datetime, like:

echo $dt->format('Y-m-d H:i:s');
# or $row['DateTime']->format('Y-m-d H:i:s'); in your example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM