获取今天,昨天和本周的记录

时间:2014-09-21 04:22:41

标签: php mysql

我想显示今天的记录(从昨天上午12点到今天晚上11:59),昨天和本周的记录, 我有今日记录的查询

SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= NOW() AND `TimeStamp` >= ?????

我的表中有一个名为TimeStamp格式的字段是2014-09-20 12:11:20

2 个答案:

答案 0 :(得分:0)

使用strtotime()date()功能获取昨天。实施例....

$now = date('Y-m-d h:i:s', time()); 
$yesterday = date('Y-m-d h:i:s', strtotime('yesterday'));
$sql = "SELECT COUNT(*) FROM `tblpatients` WHERE `Is_Deleted` = '0' AND `TimeStamp` <= '$now' AND `TimeStamp` >= '$yesterday'";

也可以在BETWEEN字符串中使用query。即,

$sql = "SELECT * FROM tblpatients WHERE Is_Deleted = '0' AND TimeStamp BETWEEN '$now' AND '$yesterday'";

答案 1 :(得分:0)

如果您只选择日期,请根据CURDATE进行计算。

希望以下示例可以帮助您

Today: WHERE timestamp >= CURDATE()
Yesterday: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND timestamp < CURDATE()
This month: WHERE timestamp >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
Between the two dates 3 June 2013 and 7 June 2013 (note how the end date is specified as 8 June, not 7 June): WHERE timestamp >= '2013-06-03' AND timestamp < '2013-06-08'

请同时查看this ..