0

I am trying to retrieve the date only from my table and store it in a php variable so I can echo it out where and when I want. Here is how I am trying to do this:

$query3 = "select o.orderID,
o.orderDate

from orders o
join order_items i on o.orderID = i.orderID
where o.customerID = '" . $customerID . "'";

    $result3 = sqlsrv_query( $conn, $query3, array(), array( "Scrollable" => SQLSRV_CURSOR_KEYSET ));

if( $result3 === false)
{
    echo "Error in query preparation/execution.\n";
    die( print_r( sqlsrv_errors(), true));
}

     while( $row = sqlsrv_fetch_array ( $result3, SQLSRV_FETCH_ASSOC))
    {
        $data1 = $row['orderDate']; 
    }

echo $data1;

I have experimented with the date() method and I can get a date on the screen but not a specific one selected from the database. Do I need to convert this before I echo it?

The error I am getting is this:

Catchable fatal error: Object of class DateTime could not be converted to string

Thank you.

2
  • What does echo $data1 output? Commented Apr 27, 2012 at 9:21
  • At the moment nothing, I get an error as the page doesn't load past the area of where I am trying to output this variable. Commented Apr 27, 2012 at 9:27

2 Answers 2

1

Your code

     while( $row = sqlsrv_fetch_array ( $result3, SQLSRV_FETCH_ASSOC)) 
    { 
        $data1 = $row['orderDate'];  
    } 

echo $data1; 

works through each line of your result, but will only show if relevant the last one. Is this perhaps your issue?

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

2 Comments

I have more code than this, it outputs everything on an order by a customer, I just posted the code I am having issues with to keep the question simple. When I try and echo the above variable it has an issue because I don't think I can just store a date time format into an array and then into a variable. I somehow need to take the date convert it into a string then store it in the variable? Thanks
I have included the error as found in the error reporting, thank you!
1

You already have a DateTime object, as the error message suggests:

Catchable fatal error: Object of class DateTime could not be converted to string
                       ^^^^^^^^^^^^^^^^^^^^^^^^

That's a native PHP variable with a date. The driver does it for you!

4 Comments

So what is the best way to display this on the screen, just the date no time? Thank you!
The best way to display a DateTime as string is the format() method.
I have the impression that you haven't understood my answer... :(
I am sorry, I did kind of understand it, but not enough to get it to work, I did try and figure it out, but I guess I needed more help! I appreciate your help though!!!

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.