not_shitashi
not_shitashi

Reputation: 291

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 date("m/d/Y",$row[date_column]); //prints nothing

Upvotes: 4

Views: 15443

Answers (3)

Sajal
Sajal

Reputation: 1216

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.

Upvotes: 1

foxiris
foxiris

Reputation: 3378

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

Upvotes: 4

Gregg
Gregg

Reputation: 121

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!

Upvotes: 0

Related Questions