php5.2调用sql server2008里由于打印出来的是DateTime Object不是正常的时间也不是时间戳
头回用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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.
