Convert mssql datetime object to PHP string
I'm grabbing some information from a database and the record is in an MSSQL DateTime format, when I return it,开发者_JS百科 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
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();
You should access them like below. the associative index arrayItem
contains an object which have some properties.
$array['arrayItem']->date;
$array['arrayItem']->timezone_type;
Try this. It works for me:
$array['arrayItem']->format('Y-m-d H:i:s');
Reference: http://php.net/manual/en/datetime.format.php
Since its an object, you can't get the properties like you do with an array. You need the ->
sign for accessing the date. In your question it seems as if you var_dumped the variable $arrayItem
, so, use it like this:
$date = strtotime($array['arrayItem']->date]);
echo $array['arrayItem']->format('Y-m-d H:i:s');
Another solution is to loop.
$_date = \DateTime::createFromFormat('D M d Y H:i:s e+', $the_date);
foreach($_dateStart as $row){
echo $row; // returns 2014-06-04 15:00
break; // stops at the first position
}
This worked for me
date('Y-m-d', strtotime($rs->Fields('time')->value)
For Adodb connection on MSSQL Server
It is functionality and simple to use.
You can convert date from date object instead of string here is the way how to use:
$obj->arrayItem->format('H:i:s')
$obj->arrayItem->format('Y-m-d')
If you want to show data in PHP page you just copy and paste in <?php ?>
code :
$serverName = "your_sqlserver";
$username = "your_username";
$password = "your_password";
$database = "your_database";
$connectionInfo = array( "UID"=>$username, "PWD"=>$password, "Database"=>$database, "ReturnDatesAsStrings"=>true);
$connection = sqlsrv_connect($serverName, $connectionInfo);
$result = sqlsrv_query( $connection,"SELECT * from table'");
$msrow = sqlsrv_fetch_array($result);
print_r($msrow['column_name']);
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.
精彩评论