data-id="1190000005009280" data-license="cc">
<code>一些变量说明:
add_time为插入的时间
to_days是sql函数,返回的是个天数
data_sub(date,INTERVAL expr type)给指定的日期减去多少天
data()函数返回日期或日期/时间表达式的日期部分。
curdate()函数返回当前的日期 y-m-d
data_format 用于以不同的格式显示日期/时间数据
period_diff(p1,p2)返回周期P1和P2之间的月数。 P1和P2格式为YYMM或YYYYMM。注意周期参数 P1 和 P2 都不是日期值
1、查询今天的所有记录:
(1)add_time字段,该字段为int(5)类型的
select * from `article` where to_days(date_format(from_UNIXTIME(`add_time`),'%Y-%m-%d')) = to_days(now());
(2)add_time字段是DATETIME类型或者TIMESTAMP类型的
select * from `article` where to_days(`add_time`) = to_days(now());
2、查询昨天的所有记录
select * from `article` where to_days(now()) <= 1 + to_days(`add_time`);
3、近7天的信息记录:
select * from `article` where date_sub(curdate(), INTERVAL 7 DAY) <= date(`add_time`);
4、近30天的信息记录:
select * from `article` where date_sub(curdate(), INTERVAL 30 DAY) <= date(`add_time`);
5、查询本月的记录
select * from `article` where date_format(`add_time`, ‘%Y%m') = date_format(curdate() , ‘%Y%m');
6、上一个月的记录
select * from `article` where period_diff(date_format(now() , ‘%Y%m') , date_format(`add_time`, ‘%Y%m')) =1;</code>以上就介绍了php查询最近30天、7天、每天、昨天、上个月的记录,包括了php,查询方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号