Convert mssql datetime object to PHP string
You should access them like below. the associative index arrayItem
contains an object which have some properties.
$array['arrayItem']->date;
$array['arrayItem']->timezone_type;
This has caught me out a couple of times too.
Hopefully this will help you as it has myself :)
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
Here's the gist of what that page is saying, in case the link goes down.
When querying against a column defined as a
datetime
, the native PHP SQL Server extension returns a string where as the Microsoft extension returns a DateTime object. So if you are expecting a string, you’ll need to adjust your code accordingly.
It goes on to explain that you can simply add an additional parameter to your requests to the database, specifying that you want your dates to be returned as strings. Simply add the ReturnDatesAsStrings
parameter, specifying true
.
Example:
$connectionParams = array(
'USR'=>'user',
'PASS'=>'pass',
'Database'='myDatabase',
'ReturnDatesAsStrings'=>true //<-- This is the important line
);
$conn = sqlsrv_connect('127.0.0.1',$connectionParams);
Then you can use $conn
as you would regularly for your sqlsrv database connection, only dates will return as strings, instead of DateTime objects.
Alternately, if all you wanted was a timestamp out of the datetime you could call:
$myDateTimeObject->getTimestamp();