【www.gdgbn.com--mysql教程】

mysql教程 查询指定日期时间内容sql查询语句

数据库教程设计时必须注意时间字段最好为int(4)这样,保存在数据库的时一个数字型日期时间截,我们可以用mktime函数求出当前日期的时间截进行加减就OK了,下面看实例

$time = time();  
//一个月

$lastMonth = mktime(date("h"),date("i"),date("s"),date("m")-1,date("d"),date("y"));
$where .= "  dtime < $lastMonth";

//三个月

$lastThreeMonth = mktime(date("h"),date("i"),date("s"),date("m")-3,date("d"),date("y"));
$where .= "  dtime < $lastThreeMonth";

$sql = "select * from testtable " .$where

/*
原理是:
如果是月份就是当前的月减去你要统计的时间如我要查询数据库中从今天起往前三个月的所有记录,我们的语句如下:mktime(date("h"),date("i"),date("s"),date("m")-3,date("d"),date("y"));
七天内的:mktime(date("h"),date("i"),date("s"),date("m"),date("d")-7,date("y"));
一小时间内的:mktime(date("h")-1,date("i"),date("s"),date("m"),date("d"),date("y"));
其它的做法是一样的。

本站原创教程转载注明来源www.111cn.net
*/

本文来源:http://www.gdgbn.com/shujuku/25000/