php中如何查询指定时间段的数据?

有数据库 Art 里面有 lasttime INT字段 该字段内容是当前时间 time() 生成的
如我想查询 2015-03-01 00:00 到 2015-03-29 00:00 之间的数据如何写SQL语句?

下面是时间戳查询。如果数据库时间显示的是 2011-04-05 那就不需要 用 strtotime 时间戳转换函数:

$timea = strtotime($_POST['timea']);

$timeb = strtotime($_POST['timeb']);

$sq2="select * from `ecs_order_info` where add_time between '$timea' and '$timeb' and `quanxian`='$dangqian' order by `order_id` DESC limit 50";

$sql = mysql_query($sq2);

扩展资料

在php中完成

1、UNIX时间戳转换为日期用函数: date()

一般形式:date('Y-m-d H:i:s', 1156219870);

2、日期转换为UNIX时间戳用函数:strtotime()

一般形式:strtotime('2010-03-24 08:15:42');

在MySQL中完成

这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性。

1、UNIX时间戳转换为日期用函数: FROM_UNIXTIME()

一般形式:select FROM_UNIXTIME(1156219870);

2、日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()

一般形式:Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);

举例:mysql查询当天的记录数:

$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d') order by id desc”。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-15
储存时间是int整形是吗,这样的话你先在php这边把要查询的时间段转成时间戳再在数据库查,这样子:
$start_time = strtotime("2015-03-01 00:00");
$end_time = strtotime("2015-03-29 00:00");
select * from Art where lasttime > $start_time and lasttime <= $end_time;
思路大致是这样,你去写写看吧。追问

$data1=D("article")->field("id,title,createtime")
->where("createtime > '$start_time' and createtime select();
查不到数据,是不是哪里写错了?

追答

你把sql语句输出,然后拿到mysql去执行看看有数据不。用的是thinkphp框架是吗

本回答被提问者和网友采纳