我试图获得今天和昨天的访客数量。
我的问题是我使用自己的日期和时间格式,它保存在我的数据库中,如下所示:HH:MM DD.MM.YYYY,现在为了获取昨天的访问者我试过这个:
$sql="SELECT COUNT(*) as count FROM lst_visitors WHERE country = '$country' AND DATE(visits) = DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
今天:
$sql="SELECT COUNT(*) as count FROM lst_visitors WHERE country = '$country' AND DATE(visits) = DATE(NOW())";
但这只是为了一切而显示0。
然后我尝试了:
$today = date("H:i d.m.Y");
$sql="SELECT COUNT(*) as count FROM lst_visitors WHERE country = '$country' AND DATE(visits) = DATE($today())";
但这不输出任何东西。
答案 0 :(得分:1)
您应该将其存储为datetime
类型,您应将其转换为此类型。与此同时,您可以使用STR_TO_DATE
:
SELECT STR_TO_DATE('15:50 27.03.2013', '%H:%i %d.%m.%Y')
<强>结果强>
| STR_TO_DATE('15:50 27.03.2013', '%H:%I %D.%M.%Y') | ----------------------------------------------------- | March, 27 2013 15:50:00+0000 |
应用您的字段:
$sql="SELECT COUNT(*) as count FROM lst_visitors WHERE country = '$country'
AND DATE(STR_TO_DATE(visits, '%H:%i %d.%m.%Y')) = DATE(NOW())";
$sql="SELECT COUNT(*) as count FROM lst_visitors WHERE country = '$country'
AND DATE(STR_TO_DATE(visits, '%H:%i %d.%m.%Y')) = DATE_SUB(CURDATE(),INTERVAL 1 DAY)";