PHP 从 SQL Server 2008 日期时间字段中选择今天的记录


PHP select records for today from SQL Server 2008 datetime field

如果我使用以下代码:

//Query and connect
$query4 = "SELECT jobno, cdate FROM delivery_comments;"?>
<?php $results4 = sqlsrv_query($connIntranet, $query4);?>
<?php echo $query4;?>
<table>
<?php //Loop through array and display results in table
while ($row4 = sqlsrv_fetch_array($results4)){
$date2 = $row4['cdate'];
$jobno2 = $row4['jobno'];
?>
<tr>
<td><?php echo $jobno2;?>, </td>
<td><?php echo date_format($date2, 'Y-m-d');?></td>
</tr>
<?php } ?>
</table>

我得到这些结果:

87722,  2016-02-16
87704,  2016-02-16
87698,  2016-02-16
92334,  2016-02-16
92447,  2016-02-17

如何编写 WHERE 子句,使其仅返回今天的结果

_____更新_____

我已经尝试了以下所有选项并继续获得:

Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given in C:'inetpub'wwwroot'portal2'short_delivery'delivery_short4.php on line 22

这是我当前的代码:

<?php
//Query and connect
$query4 = "SELECT jobno, cdate FROM delivery_comments WHERE date(cdate) = CURDATE()" ;
$results4 = sqlsrv_query($connIntranet, $query4);?>
<?php echo $query4;?>
<table>
<?php //Loop through array and display results in table
while ($row4 = sqlsrv_fetch_array($results4)){
$date2 = $row4['cdate'];
$jobno2 = $row4['jobno'];
?>
<tr>
<td><?php echo $jobno2;?>, </td>
<td><?php echo $date2;?></td>
</tr>
<?php } ?>
</table>

我试过:

SELECT jobno, cdate FROM delivery_comments WHERE date(cdate) = CURDATE()
SELECT jobno, date(cdate) FROM delivery_comments where date(cdate) = 'CURDATE()'
SELECT jobno, cdate FROM delivery_comments where DATE_FORMAT(cdate,'%Y-%m-%d')=CURDATE()  

这些不会返回错误,但也不会返回结果

SELECT jobno, cdate FROM delivery_comments where cdate = cast(getdate() as date)
SELECT jobno, cdate FROM delivery_comments where cdate = GETDATE()

如果我摆脱了 where 子句,我会收到此错误:

Catchable fatal error: Object of class DateTime could not be converted to string in C:'inetpub'wwwroot'portal2'short_delivery'delivery_short4.php on line 28

如果我在没有 where 子句的情况下重新添加 date_format,它的工作原理是全天显示:

MS SQL Server提供了一个获取当前GETDATE()日期的功能,以便您可以将查询定义为

"SELECT jobno, cdate FROM delivery_comments where cdate = GETDATE();"

> 您可以获取当前日期作为CURDATE()函数。将查询修改为:

"SELECT jobno, cdate FROM delivery_comments where WHERE date(cdate) = CURDATE();"

试试这个查询:

"SELECT jobno, cdate FROM delivery_comments where cdate = cast (GETDATE() as DATE);"
您必须

cdate中删除时间部分2016-02-17 09:10:56因为 不会等于2016-02-17 。试试这个:

 "SELECT jobno, date(cdate) FROM delivery_comments where CONVERT(date,cdate)date() = CONVERT(date,GETDATE()) ;"

像使用它一样

<?php $query4 = "SELECT jobno, CONVERT(date,cdate) as cdate1 FROM delivery_comments where CONVERT(date,cdate) = CONVERT(date,GETDATE()) " ;?>
<?php $results4 = sqlsrv_query($connIntranet, $query4);
if(!$results4)
{
   print_r( sqlsrv_errors(), true);
}
?>
<?php echo $query4;?>
<table>
<?php //Loop through array and display results in table
while ($row4 = sqlsrv_fetch_array($results4)){
$date2 = $row4['cdate1'];
$jobno2 = $row4['jobno'];
?>
<tr>
<td><?php echo $jobno2;?>, </td>
<td><?php echo $date2;?></td>
</tr>
<?php } ?>
</table>