开发者

how to retrieve a sql datetime object with a php $row?

For instance:

$sql = "SELECT * FROM db";
$query = sqlsrv_query($conn, $sql);

while($row = sqlsrv_fetch_array($query)){
    echo "$row[date_column]";
}

will crash

Most of the answers I've found are assuming you want to order your query by a datetime, but I just want to turn the datetime object into a string after I have all the rows.

I don't really understand how to use the php date() function, I've tried:

echo开发者_如何学Python date("m/d/Y",$row[date_column]); //prints nothing


$string=$row["date_column"]->format('Y-m-d H:i:s')


while($row=sqlsrv_fetch_array($rs,SQLSRV_FETCH_NUMERIC))
{
    $logdate=date_format($row[4],"Y-m-d H:i:s");
}

here, $row[4] contains the date object.
This would surely help.


First of all, if "date_column" is the column name, it should be in quotes, like this:

echo $row["date_column"];

Secondly, PHP has a built-in function to turn MYSQL datetime into a unix timestamp, which PHP date() can use. Try this:

echo date("m/d/Y", strtotime($row["date_column"]));

Cheers!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜