function aweek($gdate = \"\", $first = 0){if(!$gdate) $gdate = date(\"Y-m-d\");$w = date(\"w\", strtotime($gdate));//取得一周的第几天,星期天开始0-6$dn = $w ? $w - $first : 6;//要减去的天数$st = date(\"Y-m-d\", strtotime(\"$gdate -\".$dn.\" days\"));$en = date(\"Y-m-d\", strtotime(\"$st +6 days\"));return array($st, $en);//返回开始和结束日期}$weeks=implode(\"\' and \'\", aweek(\"\", 1));mysql_query(\"select * from table??where date Between \'$weeks\'\" )
我们提供的服务有:成都网站设计、网站建设、微信公众号开发、网站优化、网站认证、北仑ssl等。为1000+企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的北仑网站制作公司
思路:先关联数据库,然后执行sql语句,最后返回结果!
需要关联上数据库
?php
//创建对象并打开连接,最后一个参数是选择的数据库名称
$mysqli = new mysqli('localhost','root','','volunteer');
//检查连接是否成功
if (mysqli_connect_errno()){
//注意mysqli_connect_error()新特性
die('Unable to connect!'). mysqli_connect_error();
}
$sql = "SELECT *FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(时间字段名)";
//执行sql语句,完全面向对象的
$result = $mysqli-query($sql);
while($row = $result-fetch_array()){
echo $row[0];
}
?
执行一条sql语句
SELECT *FROM 表名 where DATE_SUB(CURDATE(), INTERVAL 7 DAY) = date(时间字段名)
3.运行结果、返回结果集:
$result=mysql_query($query);
并对结果进行处理!
echo date('Y-m-d',strtotime ( "last Monday -1 week" )), "\n" ;
echo date('Y-m-d',strtotime ( "last Sunday" )), "\n" ;
通过这个函数获取上个周的开始和结束,然后sql查询
可以研究下这个函数strtotime 。可以满足你的需求
/*今天*/
select * from 表名 where to_days(时间字段) = to_days(now());
/*昨天*/
select * from 表名 where to_days(now())-to_days(时间字段) = 1;
/*近7天*/
select * from 表名 where date_sub(curdate(), interval 7 day) = date(时间字段);
/*查询距离当前现在6个月的数据*/
select * from 表名 where 时间字段 between date_sub(now(),interval 6 month) and now();
/*查询当前这周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now());
/*查询上周的数据*/
select * from 表名 where yearweek(date_format(时间字段,'%Y-%m-%d')) = yearweek(now())-1;
/*查询当前月份的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(now(),'%Y-%m');
/*查询上个月的数据*/
select * from 表名 where date_format(时间字段,'%Y-%m')=date_format(date_sub(curdate(), interval 1 month),'%Y-%m');
其它获取类似以上的代码显示
/时间短查询
$starttime=GetMkTime($starttime);
$endtime=GetMkTime($endtime);
if($issettime==1){
if(!empty($starttime) !empty($endtime)){
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}else if(!empty($starttime) empty($endtime)){
$endtime=time();
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}else if(empty($starttime) !empty($endtime)){
$starttime=GetMkTime("2012-1-1");
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}
}else if($issettime==2){
if(!empty($starttime) !empty($endtime)){
$wheresql .=" AND shousu_time BETWEEN $starttime AND ".$endtime."";
}else if(!empty($starttime) empty($endtime)){
$endtime=time();
$wheresql .=" AND shousu_time BETWEEN $starttime AND ".$endtime."";
}else if(empty($starttime) !empty($endtime)){
$starttime=GetMkTime("2012-1-1");
$$wheresql .=" AND shousu_time BETWEEN $starttime AND ".$endtime."";
}
}else
{
if(!empty($starttime)){
if($isset=="jr"){
$starttime=GetDateMK($starttime);
$wheresql .=" AND datediff('".$starttime."',FROM_UNIXTIME(`paidan_time`))=0 ";
}else{
//$endtime=time();
$wheresql .=" AND paidan_time BETWEEN $starttime AND ".$endtime."";
}
/*$nowtime = time();
$passtime=$nowtime-$starttime;
$tianshu=floor($passtime/(24*60*60));
echo floor($passtime/(24*60*60)); //天数
//echo 'br/';
$starttime = $nowtime - ($tianshu*24*3600);
$wheresql .= " AND paidan_time='$starttime' ";*/
}
}