开发者

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

开发者 https://www.devze.com 2023-04-05 19:34 出处:网络
For instance: $sql = \"SELECT * FROM db\"; $query = sqlsrv_query($conn, $sql); while($row = sqlsrv_fetch_array($query)){

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

精彩评论

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