1

I am having some trouble getting a date field out of my SQLServer using PHP and have not been able to Google a solution. I know that it involves the fact that the field is a date data type in my table, but the only results I get is a 500-Internal Server Error.

Here is what I have:

$query = "SELECT EntryDate FROM Table1 WHERE UserID = 1";

$result = sqlsrv_query($link, $query);

while($row = sqlsrv_fetch_array($result)) 
{
echo $row['EntryDate'];
}

This works with every other column in my table except the date column. Any idea why?

BTW, the date in my table is formatted as yyyy-mm-dd.

4
  • Did you check your error log? What caused the 500 response? Commented Aug 26, 2013 at 19:18
  • I am very new to PHP and only use it as a go between for a PhoneGap iOS app and our SQL Server. How can I view the error? Commented Aug 26, 2013 at 19:24
  • Try putting this at the top of your script <?php ini_set('display_errors', '1'); ?> That should print errors to the browser Commented Aug 26, 2013 at 19:40
  • Thanks. This is what I got: Catchable fatal error: Object of class DateTime could not be converted to string Commented Aug 26, 2013 at 19:49

1 Answer 1

1
$row['entryDate']

seems to be an object of type DateTime which you can't print out directly. Try to convert it into a string in order to echo it.

echo $row['entryDate']->format('Y-m-d H:i:s');

or

echo date_format($row['entryDate'], 'Y-m-d H:i:s');

PHP: DateTime::format

Sign up to request clarification or add additional context in comments.

2 Comments

I used echo date_format($row['EntryDate'],'Y-m-d') since my data type is actually date, not datetime, and it worked perfectly. Thanks!
Your variable doesn't include the time but its type is still datetime. Those kinda things are important to know when searching for an appropriate method/function for the type. Thats why I linked the PHP page in ;-)

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.