[英]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,它将显示整天:
您必须从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>
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);"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.