Convert mssql datetime object to PHP string

Viewed 49242

I'm grabbing some information from a database and the record is in an MSSQL DateTime format, when I return it, it shows in my array as follows:

[arrayItem] => DateTime Object
    (
        [date] => 2008-06-05 09:14:11
        [timezone_type] => 3
        [timezone] => Europe/London
    )

When I try to extract this as an array (ie $array[arrayItem][date]) I get an error:

Fatal error: Cannot use object of type DateTime as array

I have also tried formatting the data in SQL before it is passed to the PHP, and the strtotime functions and had no joy.

Any recommendations would be very welcome, I'm tearing my hair out with this one!

Thanks

10 Answers

When you create the query like

SELECT created FROM yourTable ...

and created is DATETIME type, the result is the object as you described. To return the date part as a string, you can solve it on the SQL level as

SELECT CONVERT(varchar, created, 23) AS created FROM yourTable ...

see the 23 meaning here.

Related