4

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
1
  • that's more mssql question rather than php Commented Sep 19, 2011 at 22:18

3 Answers 3

4
$string=$row["date_column"]->format('Y-m-d H:i:s')
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

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!

6 Comments

Question is asking about MSSQL, not MySQL, but I don't see why this method would not work for both.
Whoops, your right! But yes, it should if MSSQL stores datetime the same.
Hi, thanks for you answer, however, what I am getting from echo date("m/d/Y", strtotime($row["date_column"]); is the date: 12/31/1969 for every entry. Also, that is not the correct date for any of the entries.
You will get that date when PHP date() is given a bad input value. So I can help you more, can I have a sample of what "echo $row["date_column"];" produces?
That's strange. It works when I try it on my server, with the format you provided. The code I am trying is: "$time = "2001-01-13 10:45:55.123"; echo date("m/d/Y", strtotime($time));" Which produces: "01/13/2001". What do you get when you just try "echo strtotime($row["date_column"]);"?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.