php查询最近30天、7天、每天、昨天、上个月的记录

来源:互联网 发布:陕西省网络作家协会 编辑:程序博客网 时间:2024/05/18 03:44
一些变量说明:add_time为插入的时间to_days是sql函数,返回的是个天数data_sub(date,INTERVAL expr type)给指定的日期减去多少天data()函数返回日期或日期/时间表达式的日期部分。curdate()函数返回当前的日期 y-m-ddata_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;
阅读全文
0 0