开发者

PHP 5.3 echo a date object from an assoc array?

I want to echo the result from a date column after a database query, but the string will only echo to the screen if I call var_dump on the $array before I try to echo the variable.

If I uncomment "//var_dump($assocArray[0]["date"]);" then I can echo the date, otherwise nothing will be echoed...what is going on here?

 $link = sqlsrv_connect("localhost", array("UID" => 'myuser', "PWD" => 'mypass', "Database"=>'mydb'));

 $result = sqlsrv_query($link, "select date from mytable where id=1");

 $i=0;
 while($row=sqlsrv_fetch_array($result,SQLSRV_FETCH_ASSOC))
 {
  $assocArray[$i] = $row;
  $i++;
 }

 sqlsrv_free_stmt($result);

 sqlsrv_close($link);


 echo "\n Dump AssocArray: \n";
 echo "====================\n";
 //var_dump($assocArray[0]["date"]);

 echo "\n\n Echo Date: \n";
 echo "====================\n";
 echo $assocArray[0]["date"]->date; //doesnt print

 echo "\n\n Dump Date: \n";
 echo "====================\n";
 var_dump($assocArray[0]["date"]);




       /* result of Dump Date:

      object(DateTime)#1 (3) {
   ["date"]=>
   string(19) "2011-01-20 04:00:00"
   ["timezone_type"]=>开发者_如何学编程
   int(3)
   ["timezone"]=>
   string(28) "America/Indiana/Indianapolis"
 }

       */


It seems $assocArray[0]["date"] is a DateTime object.

To get the date, you need to use format().

$date = $assocArray[0]["date"]->format('Y-m-d H:i:s');


$date = $assocArray["date"]->format('Y-m-d H:i:s'); This one worked Perfect for me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜