使用SQLSRV的PHP日期参数

使用SQLSRV的PHP日期参数,php,Php,我正在学习PHP,并让它与我们的SQL Server 2008R2 DB对话。我正在尝试运行一个查询,该查询有两个需要传入的日期参数。我知道我可以将日期参数编码到SQL中,但我想看看下面的占位符方法有什么问题: $serverName = "server"; $connectionInfo = array( "Database"=>"db", "UID"=>"user", "PWD"=>"pwd"); $conn = sqlsrv_connect( $serverName,

我正在学习PHP,并让它与我们的SQL Server 2008R2 DB对话。我正在尝试运行一个查询,该查询有两个需要传入的日期参数。我知道我可以将日期参数编码到SQL中,但我想看看下面的占位符方法有什么问题:

$serverName = "server";

$connectionInfo = array( "Database"=>"db", "UID"=>"user", "PWD"=>"pwd");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

$startDate = strtotime("2015-04-30");
$endDate = strtotime("2015-05-01");

$sql = "SELECT * FROM history
        WHERE (ts >= ? and ts < ?)
        ORDER BY ts";
$params = array($startDate, $endDate);

$stmt = sqlsrv_query($conn, $sql, $params);
if ($stmt == false) {
     die( print_r( sqlsrv_errors(), true));
}

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
  echo "<p>" . $row['vesselname'] . "</p>";
}

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
$serverName=“server”;
$connectionInfo=array(“数据库”=>“db”、“UID”=>“用户”、“PWD”=>“PWD”);
$conn=sqlsrv_connect($serverName,$connectionInfo);
如果($conn){
回显“已建立连接。
”; }否则{ echo“无法建立连接。
”; 模具(打印错误(sqlsrv_errors(),true)); } $startDate=标准时间(“2015-04-30”); $endDate=strottime(“2015-05-01”); $sql=“从历史记录中选择* 其中(ts>=?和ts<?) 由ts订购”; $params=数组($startDate,$endDate); $stmt=sqlsrv_查询($conn、$sql、$params); 如果($stmt==false){ 模具(打印错误(sqlsrv_errors(),true)); } 而($row=sqlsrv\u fetch\u数组($stmt,sqlsrv\u fetch\u ASSOC)){ echo“”$row['vesselname']。“

”; } sqlsrv_free_stmt($stmt); sqlsrv_close($conn);

这将返回错误“将表达式转换为数据类型datetime时发生算术溢出错误”。ts是一个日期时间字段。

没关系。我发现我不需要strotime,只需要传递日期字符串