Home  >  Article  >  Backend Development  >  php5.2调用sql server2008里由于打印出来的是DateTime Object不是正常的时间也不是时间戳

php5.2调用sql server2008里由于打印出来的是DateTime Object不是正常的时间也不是时间戳

WBOY
WBOYOriginal
2016-06-20 12:37:231107browse

头回用PHP+sqlsrv这种非主流组合 不知道怎么处理这个时间对象 有没高手指点一二
[0] => Array
        (
            [UserId] => 10
            [Account] => P6000001
            [PassWord] => 123456
            [LoginNumber] => 4
            [UpdateTime] => DateTime Object
                (
                )

            [RegistTime] => DateTime Object
                (
                )

            [EndTime] => DateTime Object
                (
                )

            [Status] => 0
            [CreateTime] => DateTime Object
                (
                )

            [MachineId] => 135
        )
怎么把这个时间对象转换成时间戳


回复讨论(解决方案)

先尝试用 date 函数转换
比如你读取的数据在 $r 数组中,则
echo date('Y-m-d H:i:s', $r[0]['UpdateTime']);
看看

如不行,则请贴出
echo serialize($r);
的结果
应该选择确实有值的记录

===源码
$sqlsrv = new Sqlsrv();
$sql = "SELECT TOP 1 [UpdateTime]
      ,[RegistTime]
      ,[EndTime]
  FROM [User] ";
$stmt=$sqlsrv->select($sql);
p($stmt);
echo date('Y-m-d H:i:s', $stmt[0]['UpdateTime']);//此行没有被打印出来
echo '
';
echo serialize($stmt);
===以下为打印结果
Array
(
    [0] => Array
        (
            [UpdateTime] => DateTime Object
                (
                )

            [RegistTime] => DateTime Object
                (
                )

            [EndTime] => DateTime Object
                (
                )

        )

)

a:1:{i:0;a:3:{s:10:"UpdateTime";O:8:"DateTime":0:{}s:10:"RegistTime";O:8:"DateTime":0:{}s:7:"EndTime";O:8:"DateTime":0:{}}}

序列化后也看不到内容,表示你的字段为空
你应该选择有数据的字段进行测试

在sqlserver里面 是有数据的 不是空的 (在百度到的帖子里 别人读取的时间不友好 我遇到的却是一个datetime object 而且 用get_object_vars()得到的是一个空数组 真不知道怎么整这个对象)
UserId Account PassWord LoginNumber UpdateTime RegistTime EndTime Status CreateTime MachineId
10 P6000001 123456 4 2013-09-27 13:16:44.503 2013-03-21 10:18:37.000 2019-04-19 17:27:53.047 0 2013-04-19 17:27:53.050 135

贴出你的代码

贴出你的代码


代码在二楼 {sql server里 这三个字段的类型就是 datetime 难道跟mysql里的datetime不一样}
我写了一个连接类
class Sqlsrv{
public $connect;
function __construct($host="(local)",$uid="sa",$pwd="123456",$dbname="tupoemanager"){
$connectionInfo =  array("UID"=>$uid,"PWD"=>$pwd,"Database"=>$dbname);
$this->connect = sqlsrv_connect( $host,$connectionInfo);
sqlsrv_query("set names 'utf8'");     
//echo "
";  <br> }  <br> //select  <br> function select($sql,$affected=2){  <br> $result = sqlsrv_query($this->connect,$sql);  <br> //var_dump($result);  <br> $resultArr = array();  <br> if($affected==2){  <br>  <br>  while($row =sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){  <br> $resultArr[] =$row;  <br> }  <br>  <br> }else{  <br> $resultArr = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC);  <br> }  <br> return $resultArr;  <br> }  <br>  <br> //query  <br> function query($sql){  <br> return sqlsrv_query($this->connect,$sql);  <br> }  <br> //  <br> function affected($stmt){  <br> return sqlsrv_rows_affected( $stmt);  <br> }  <br> }  <br> date_default_timezone_set("PRC");  <p class="sougouAnswer">  查询时用CONVERT()转换一下就OK了  <br>  <br>$sql = "SELECT TOP 1  <br> CONVERT(varchar(19),UpdateTime,120)as UpdateTime  <br>  FROM [User] ";  <br>$stmt=$sqlsrv->select($sql); </p>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn